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
Intro to Brookshelf ORM
Search
aaronmoodie
October 15, 2015
Technology
270
0
Share
Intro to Brookshelf ORM
A simple intro the the Bookshelf ORM that I gave at Brooklyn JS #24
aaronmoodie
October 15, 2015
More Decks by aaronmoodie
See All by aaronmoodie
Etsy Patterns
aaronmoodie
1
240
CreativeMornings Melbourne
aaronmoodie
2
160
Other Decks in Technology
See All in Technology
AI飲み会幹事エージェントを作っただけなのに
ykimi
0
230
2026-05-14 要件定義からソース管理まで!IBM Bob基礎ハンズオン
yutanonaka
0
160
React Compiler導入の効果と運用の工夫
kakehashi
PRO
3
190
(きっとたぶん)人材育成や教育のような何かの話
sejima
0
750
GCASアップデート(202603-202605)
techniczna
0
190
クラウドネイティブ DB はいかにして制約を 克服したか? 〜進化歴史から紐解く、スケーラブルアーキテクチャ設計指針〜
hacomono
PRO
6
1k
社内RAGの導入で気を付けたポイント
yakumo
1
110
20260516_SecJAWS_Days
takuyay0ne
2
430
Gaussian Splattingの実用化 - 映像制作への展開
gpuunite_official
0
200
エムスリーテクノロジーズ株式会社 エンジニア向け紹介資料 / M3 Technologies Company Deck
m3_engineering
0
170
Oracle AI Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
4
2.6k
"うちにはまだ早い"は本当? ─ 小さく始めるPlatform Engineering入門
harukasakihara
6
610
Featured
See All Featured
Crafting Experiences
bethany
1
140
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
210
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.2k
Amusing Abliteration
ianozsvald
1
170
Optimizing for Happiness
mojombo
378
71k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
23k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
SEO for Brand Visibility & Recognition
aleyda
0
4.5k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
55k
Transcript
Hello! I’m Aaron Moodie, a product designer at Etsy. I
like JavaScript.
A quick overview of Bookshelf.js, an ORM for Node.
slime.io
Bookshelf? Node ORM2 Sequelize? Node ORM?
- Backbone patterns - Knex queries and migrations - Some
Docs (at the time) Bookshelf.js ✔
Model & Collections
var Track = bookshelf.Model.extend({ tableName: 'tracks', hasTimestamps: true, user: function()
{ return this.belongsTo('User'); } }); var Tracks = bookshelf.Collection.extend({ model: Track }); Models and Collections define relationship
Saving and Updating
Saving and Updating // Create a new track new Track({'title':'Never
Gonna Give You Up'}) .save() .then(function(track) { console.log(track.get('title')); }); // Update existing track new Track({'title':'Never Gonna Give You Up'}) .save({'artist':'Rick Astley'}, {patch: true}) .then(function(track) { console.log(track.get('title')); });
Queries
Querying new Track({'title':'Never Gonna Give You Up'}) .fetch() .then(function(track) {
console.log(track.get('title')); }) .catch(function(err) { console.log(err); }); Model Fetch on attribute catch promise errors
Querying new Tracks() .query(function(qb) { qb.orderBy('created_at','DESC'); }) .fetch({withRelated: ['user']}) .then(function(tracks)
{ res.status(200).json(tracks); }); Collection Knex query builder related models as an array
Querying Model with fetchAll() new Track() .query(function(qb) { qb.orderBy('created_at','DESC'); })
.fetchAll({ withRelated: [ {'user': function(qb) { qb.column('id', 'username'); }}, 'votes', 'tags' ] }) .then(function(tracks) {}); ‘user’ mapped to query callback
Model (extending) var Track = bookshelf.Model.extend({ // Model attributes },
{ getRecent: Promise.method(function() { return new Track() .query({ /* build query */ }) .fetchAll({ /* fetch params */ }) .then(function(tracks) { return tracks; }) }); } });
router.get('/tracks', function(req, res, next) { Track.getRecent() .then(function(tracks) { res.status(200).send(JSON.stringify(tracks)); })
.catch(function(err) { return next(err); }); }); Much cleaner
- Knex migrations - Awesome Docs - Active updates More
Bookshelf Goodness
Thanks! @aaronmoodie