Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.jsのライブラリ周りでハマった話

1741ebf45ce415e49fb0340da9e1ca15?s=47 KSK
June 29, 2018
510

 Node.jsのライブラリ周りでハマった話

主にSequelizeのScopeの話

1741ebf45ce415e49fb0340da9e1ca15?s=128

KSK

June 29, 2018
Tweet

More Decks by KSK

Transcript

  1. Node.js ͷ ϥΠϒϥϦपΓͰ ϋϚͬͨ࿩ ksk1030 / 2018.06.29 @NodeֶԂ 31࣌ݶ໨

  2. ࣗݾ঺հ ɾKeisuke Nakatane (@ksk1030) ɾORSO Inc. ৽نࣄۀ։ൃࣨ ɾαʔόɿΞϓϦɿϑϩϯτ ʹɹ̓ɹɿɹ̍ɹɿɹ̎ɹɹ͘Β͍ ɾલ৬͸ژ౎ͷήʔϜ԰Ͱਓࣄ

  3. Node.js ͷ ϥΠϒϥϦपΓͰ ϋϚͬͨ࿩

  4. None
  5. Sequelize ɾNode.js ༻ͷOR Mapper ɹ- http://docs.sequelizejs.com/ ɹ- Rails Ͱ͍͏Active Record

    ɾҰ൪ϝδϟʔͰ΍Γ͍ͨ͜ͱ͸Ͱ͖ͦ͏ ɾݹ͍৘ใ΋ώοτ͢ΔͷͰ஫ҙ (ver 3.x) ɹ- ࠷৽͸ ver 4.38 ͱ ver 5 Beta (2018/06/24)
  6. ߏ੒͸ͬ͘͟Γ͜Μͳײ͡ ؅ཧπʔϧ ΞϓϦ DB ΞϓϦAPI ؅ཧπʔϧAPI

  7. ΍Ζ͏ͱͨ͜͠ͱ

  8. λΠϜγϑτػೳ ɾσόοά༻ʹ staging ҎԼͰݱࡏ࣌ࠁΛ ɹِ૷͢Δػೳ ɾ؅ཧπʔϧ͔Β࣌ࠁΛߋ৽ ɾΞϓϦଆͷAPIͰ࣌ࠁࠩ෼Λద༻͢Δ

  9. None
  10. Moment.js ɾΈΜͳେ޷͖೔෇ૢ࡞ܥϥΠϒϥϦ ɹ- ΋͸΍σϑΝΫτελϯμʔυͬΆ͍ ɾ࣌ؒͷॲཧ͸શͯ͜Ε wrap ͨ͠΋ͷʹ౷Ұ

  11. ɾshiftSec ͕࣌ࠁͷࠩ෼ ͜Μͳײ͡Ͱwrapͨ͠ import moment from 'moment-timezone' import shiftSec from

    '../config/timeShift' if (process.env.NODE_ENV !== 'production') { moment.now = () => { return new Date().getTime() + shiftSec * 1000 } } export default moment
  12. ⚠໰୊ൃੜ⚠

  13. Model ͷ Scope ͕ ػೳͯ͠ͳ͍ʁ

  14. ɾSequelize ͷػೳ ɹ- http://docs.sequelizejs.com/manual/tutorial/scopes.html ɾڞ௨Խ͍ͨ͠ΫΤϦΛ model ʹఆ͓ٛͯ͘͠ ɹ- ʮϦϦʔε೔Λա͗ͨΧʔυʯͱ͔ Scope

  15. ɾ$lte ͸ ” ≦ ”ʢLess Than or Equalʣ Scope const

    now = moment() const Card = sequelize.define('card', { // Attributes }, { scopes: { released: { where: { releasedAt: { $lte: now }, }, }, }, }) Card.scope(‘releasedAt').findAll() // ϦϦʔεޙͷΧʔυͷΈ
  16. ɾmodel ͸ΞϓϦέʔγϣϯͷىಈ࣌ʹ ɹInitialize͞ΕΔ ɾScope ͸ͦͷ࣌఺ͷݱࡏ࣌ࠁΛݩʹੜ੒ ɾىಈޙʹ shiftSec ͕มΘͬͯ΋ߋ৽͞Εͳ͍ ݪҼ

  17. Ͳ͏͠Α͏

  18. ຖճҾ਺౉͢ʁ ɾείʔϓͷҾ਺Ͱͦͷ౓ʹݱࡏ࣌ࠁ౉ͤ͹ ɹରԠͰ͖Δ ɾ͕ɺ staging ҎԼͰ͔͠࢖Θͳ͍ػೳͷ ɹ͍ͤͰ৑௕Խ͢Δͷ΋ΠϠͩ

  19. Ϟσϧߋ৽ ɾ࣌ࠁࠩ෼͕ߋ৽͞ΕͨΒϞσϧ΋ߋ৽͢Δ ɾͨͩ͠ɺ؅ཧπʔϧ༻APIͱΞϓϦ༻API͸ ɹผαʔόʹஔ͔Ε͍ͯΔ

  20. ߏ੒͓͞Β͍ ؅ཧπʔϧ ΞϓϦ DB ΞϓϦAPI ؅ཧπʔϧAPI ߋ৽ϦΫΤετΛड͚Δ ϞσϧΛߋ৽͍ͨ͠

  21. ࠷ऴతʹ ɾDBͷϨίʔυͱɺϩʔΧϧϑΝΠϧʹ ɹshiftSec ʢݱࡏ࣌ࠁͱͷඵ਺ࠩʣΛ࣋ͭ ɾ؅ཧπʔϧͰߋ৽ͨ࣌͠఺Ͱ͸DBͷ஋͚ͩ ɹॻ͖׵Θ͍ͬͯΔ ɾΞΫηε͝ͱʹɺϩʔΧϧͱDBͷ shiftSec Λ ɹνΣοΫɺ͕ࠩ͋Ε͹ߋ৽

  22. ͳΜͱ͔ͳͬͨ

  23. ՝୊ ɾϞσϧͷߋ৽ͱΞΫηε͝ͱͷࠩ෼νΣοΫ ɹͪ͠ΌͬͯΔ͕ॏ͍ ɹ- staging ҎԼͳͷͰ໰୊ʹ͸ͳΓʹ͍͘ ɾ΋ͬͱ͍͍ߏ੒͕͋Γͦ͏ ɹ- ஋Λ2ஈ֊Ͱ࣋ͬͯΔͷϒαΠΫ… ɹ-

    ϞσϧΛ෼཭ͨ͠ϚΠΫϩαʔϏεԽʁ ɾ͔ͯύοέʔδͱ͔ͳ͍ͷ͔…ʁ
  24. ͪͳΈʹ

  25. defaultScope

  26. defaultScope ɾ໌ࣔతʹॲཧ͠ͳ͚Ε͹ࣗಈͰద༻ const Card = sequelize.define('card', { // Attributes },

    { defaultScope: { where: { isActive: true, }, }, }) Card.findAll() // scope ͷࢦఆͳ͠Ͱ acitve ͷΈ
  27. ศརʹݟ͑Δ͕ Rails ք۾Ͱ͸

  28. "ېஅͷՌ࣮"

  29. None
  30. None
  31. "ېஅͷՌ࣮" ېढ

  32. Active Record Ͱ͸ ɾ෭࡞༻͕ଟ͍ ɹ- ஌Β͵ؒʹϑΟϧλϦϯά͞ΕΔ ɹ- unscoped ͢Δͱଞͷ৚݅΋Ϧηοτ ɹ-

    Πϯελϯεੜ੒࣌ʹ default ஋͕ೖΔ
  33. ͜Μͳײ͡ ʢ࣌ؒ଍Βͣݕূෆे෼Ͱ͢ɻɻɻʣ class User < ApplicationRecord has_many :friends end class

    Friend < ApplicationRecord belongs_to :user default_scope -> { where(is_goodlooking: true) } end User.first.friends # ͨͩ͠ΠέϝϯʹݶΔ User.first.friends.unscoped # “.first” ͷߜΓࠐΈ͕ແࢹ͞ΕΔ Friend.new # <id:nil, is_goodlooking:true … > ࠷ॳ͔ΒΠέϝϯ
  34. Sequelize Ͱ͸ Ͳ͏ͳͷ͔ʁ

  35. ҙ֎ͱେৎ෉ͬΆ͍͔…ʁ ɾunscope ͱ default஋ͷ໰୊͸ΫϦΞ // ϢʔβͷߜΓࠐΈ͸ޮ͍ͨ·· const firstUser = await

    User.findOne() firstUser.findAll({ include: [{ model: Friend.unscoped() }], }) const newFriend = Friend.build() // { id: null }
  36. ݁ہͲ͏ͳͷʁ ɾ஌Β͵ؒʹϑΟϧλϦϯά͞ΕΔɺͱ͍͏ ɹέʔε͸ग़ͯ͘Δ ɹ- ΍͸Γཚ༻͸ݫېʂʂ❌ ɹ- ࢖͏ͱͨ͠ΒɺϦϦʔε࣌ࠁ·Ͱຊ൪؀ڥʹ ɹ ઈରग़ͪ͠Ό͍͚ͳ͍෺ʹ Soft

    Delete తʹ ʢ- Soft Delete ࣗମͷੋඇ΋͋Δʣ ɾ๻ͷ஌Βͳ͍᠘͕·ͩ·ͩ͋Γͦ͏ͳ ɹؾ΋͢ΔͷͰੋඇڭ͍͑ͯͩ͘͞…$
  37. Enjoy your ɹ Node.js life …