Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Node.jsのライブラリ周りでハマった話
Search
KSK
June 29, 2018
2
660
Node.jsのライブラリ周りでハマった話
主にSequelizeのScopeの話
KSK
June 29, 2018
Tweet
Share
More Decks by KSK
See All by KSK
Supernova Studio 使いはじめてみたら割と良さそう / supernova_studio
ksk1030m
1
760
CSV出力 - Viewからやるか? 他からやるか? / How to output CSV
ksk1030m
1
560
4画面出力とレシート印刷で作る体験型デジタルサイネージ / Unity signage
ksk1030m
1
2.9k
実録リファラル採用-僕と副社長の1827日- / my referral
ksk1030m
0
1.1k
GitLabを仕事で使っていた私が思うことをつらつらと / about GitLab
ksk1030m
1
920
エンジニア目線で考えるPR戦略 / PR by Engineer
ksk1030m
1
1.3k
Metabase ライブ環境構築 / metabase_live_coding
ksk1030m
0
300
昔の自分に伝えたい容量回復のためのDockerの仕組み / docker_tips
ksk1030m
1
490
Capistranoの罠と他人がハマった事例を見て思うこと / capistrano-trap
ksk1030m
0
280
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Designing Experiences People Love
moore
139
23k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Raft: Consensus for Rubyists
vanstee
137
6.7k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
Mobile First: as difficult as doing things right
swwweet
222
9k
Optimizing for Happiness
mojombo
376
70k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
Done Done
chrislema
182
16k
Transcript
Node.js ͷ ϥΠϒϥϦपΓͰ ϋϚͬͨ ksk1030 / 2018.06.29 @NodeֶԂ 31࣌ݶ
ࣗݾհ ɾKeisuke Nakatane (@ksk1030) ɾORSO Inc. ৽نࣄۀ։ൃࣨ ɾαʔόɿΞϓϦɿϑϩϯτ ʹɹ̓ɹɿɹ̍ɹɿɹ̎ɹɹ͘Β͍ ɾલ৬ژͷήʔϜͰਓࣄ
Node.js ͷ ϥΠϒϥϦपΓͰ ϋϚͬͨ
None
Sequelize ɾNode.js ༻ͷOR Mapper ɹ- http://docs.sequelizejs.com/ ɹ- Rails Ͱ͍͏Active Record
ɾҰ൪ϝδϟʔͰΓ͍ͨ͜ͱͰ͖ͦ͏ ɾݹ͍ใώοτ͢ΔͷͰҙ (ver 3.x) ɹ- ࠷৽ ver 4.38 ͱ ver 5 Beta (2018/06/24)
ߏͬ͘͟Γ͜Μͳײ͡ ཧπʔϧ ΞϓϦ DB ΞϓϦAPI ཧπʔϧAPI
Ζ͏ͱͨ͜͠ͱ
λΠϜγϑτػೳ ɾσόοά༻ʹ staging ҎԼͰݱࡏ࣌ࠁΛ ɹِ͢Δػೳ ɾཧπʔϧ͔Β࣌ࠁΛߋ৽ ɾΞϓϦଆͷAPIͰ࣌ࠁࠩΛద༻͢Δ
None
Moment.js ɾΈΜͳେ͖ૢ࡞ܥϥΠϒϥϦ ɹ- σϑΝΫτελϯμʔυͬΆ͍ ɾ࣌ؒͷॲཧશͯ͜Ε wrap ͨ͠ͷʹ౷Ұ
ɾ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
⚠ൃੜ⚠
Model ͷ Scope ͕ ػೳͯ͠ͳ͍ʁ
ɾSequelize ͷػೳ ɹ- http://docs.sequelizejs.com/manual/tutorial/scopes.html ɾڞ௨Խ͍ͨ͠ΫΤϦΛ model ʹఆ͓ٛͯ͘͠ ɹ- ʮϦϦʔεΛա͗ͨΧʔυʯͱ͔ Scope
ɾ$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() // ϦϦʔεޙͷΧʔυͷΈ
ɾmodel ΞϓϦέʔγϣϯͷىಈ࣌ʹ ɹInitialize͞ΕΔ ɾScope ͦͷ࣌ͷݱࡏ࣌ࠁΛݩʹੜ ɾىಈޙʹ shiftSec ͕มΘͬͯߋ৽͞Εͳ͍ ݪҼ
Ͳ͏͠Α͏
ຖճҾ͢ʁ ɾείʔϓͷҾͰͦͷʹݱࡏ࣌ࠁͤ ɹରԠͰ͖Δ ɾ͕ɺ staging ҎԼͰ͔͠Θͳ͍ػೳͷ ɹ͍ͤͰԽ͢ΔͷΠϠͩ
Ϟσϧߋ৽ ɾ࣌ࠁ͕ࠩߋ৽͞ΕͨΒϞσϧߋ৽͢Δ ɾͨͩ͠ɺཧπʔϧ༻APIͱΞϓϦ༻API ɹผαʔόʹஔ͔Ε͍ͯΔ
ߏ͓͞Β͍ ཧπʔϧ ΞϓϦ DB ΞϓϦAPI ཧπʔϧAPI ߋ৽ϦΫΤετΛड͚Δ ϞσϧΛߋ৽͍ͨ͠
࠷ऴతʹ ɾDBͷϨίʔυͱɺϩʔΧϧϑΝΠϧʹ ɹshiftSec ʢݱࡏ࣌ࠁͱͷඵࠩʣΛ࣋ͭ ɾཧπʔϧͰߋ৽ͨ࣌͠ͰDBͷ͚ͩ ɹॻ͖Θ͍ͬͯΔ ɾΞΫηε͝ͱʹɺϩʔΧϧͱDBͷ shiftSec Λ ɹνΣοΫɺ͕ࠩ͋Εߋ৽
ͳΜͱ͔ͳͬͨ
՝ ɾϞσϧͷߋ৽ͱΞΫηε͝ͱͷࠩνΣοΫ ɹͪ͠ΌͬͯΔ͕ॏ͍ ɹ- staging ҎԼͳͷͰʹͳΓʹ͍͘ ɾͬͱ͍͍ߏ͕͋Γͦ͏ ɹ- Λ2ஈ֊Ͱ࣋ͬͯΔͷϒαΠΫ… ɹ-
ϞσϧΛͨ͠ϚΠΫϩαʔϏεԽʁ ɾ͔ͯύοέʔδͱ͔ͳ͍ͷ͔…ʁ
ͪͳΈʹ
defaultScope
defaultScope ɾ໌ࣔతʹॲཧ͠ͳ͚ΕࣗಈͰద༻ const Card = sequelize.define('card', { // Attributes },
{ defaultScope: { where: { isActive: true, }, }, }) Card.findAll() // scope ͷࢦఆͳ͠Ͱ acitve ͷΈ
ศརʹݟ͑Δ͕ Rails ք۾Ͱ
"ېஅͷՌ࣮"
None
None
"ېஅͷՌ࣮" ېढ
Active Record Ͱ ɾ෭࡞༻͕ଟ͍ ɹ- Β͵ؒʹϑΟϧλϦϯά͞ΕΔ ɹ- unscoped ͢Δͱଞͷ݅Ϧηοτ ɹ-
Πϯελϯεੜ࣌ʹ default ͕ೖΔ
͜Μͳײ͡ ʢ࣌ؒΒͣݕূෆेͰ͢ɻɻɻʣ 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 … > ࠷ॳ͔ΒΠέϝϯ
Sequelize Ͱ Ͳ͏ͳͷ͔ʁ
ҙ֎ͱେৎͬΆ͍͔…ʁ ɾunscope ͱ defaultͷΫϦΞ // ϢʔβͷߜΓࠐΈޮ͍ͨ·· const firstUser = await
User.findOne() firstUser.findAll({ include: [{ model: Friend.unscoped() }], }) const newFriend = Friend.build() // { id: null }
݁ہͲ͏ͳͷʁ ɾΒ͵ؒʹϑΟϧλϦϯά͞ΕΔɺͱ͍͏ ɹέʔεग़ͯ͘Δ ɹ- Γཚ༻ݫېʂʂ❌ ɹ- ͏ͱͨ͠ΒɺϦϦʔε࣌ࠁ·Ͱຊ൪ڥʹ ɹ ઈରग़ͪ͠Ό͍͚ͳ͍ʹ Soft
Delete తʹ ʢ- Soft Delete ࣗମͷੋඇ͋Δʣ ɾͷΒͳ͍᠘͕·ͩ·ͩ͋Γͦ͏ͳ ɹؾ͢ΔͷͰੋඇڭ͍͑ͯͩ͘͞…$
Enjoy your ɹ Node.js life …