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
650
Node.jsのライブラリ周りでハマった話
主にSequelizeのScopeの話
KSK
June 29, 2018
Tweet
Share
More Decks by KSK
See All by KSK
Supernova Studio 使いはじめてみたら割と良さそう / supernova_studio
ksk1030m
1
740
CSV出力 - Viewからやるか? 他からやるか? / How to output CSV
ksk1030m
1
540
4画面出力とレシート印刷で作る体験型デジタルサイネージ / Unity signage
ksk1030m
1
2.9k
実録リファラル採用-僕と副社長の1827日- / my referral
ksk1030m
0
1.1k
GitLabを仕事で使っていた私が思うことをつらつらと / about GitLab
ksk1030m
1
900
エンジニア目線で考えるPR戦略 / PR by Engineer
ksk1030m
1
1.3k
Metabase ライブ環境構築 / metabase_live_coding
ksk1030m
0
290
昔の自分に伝えたい容量回復のためのDockerの仕組み / docker_tips
ksk1030m
1
470
Capistranoの罠と他人がハマった事例を見て思うこと / capistrano-trap
ksk1030m
0
270
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
What's new in Ruby 2.0
geeforr
343
31k
Happy Clients
brianwarren
98
6.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
A Tale of Four Properties
chriscoyier
156
23k
Gamification - CAS2011
davidbonilla
80
5k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
A Modern Web Designer's Workflow
chriscoyier
693
190k
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 …