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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
aaronmoodie
October 15, 2015
Technology
0
260
Intro to Brookshelf ORM
A simple intro the the Bookshelf ORM that I gave at Brooklyn JS #24
aaronmoodie
October 15, 2015
Tweet
Share
More Decks by aaronmoodie
See All by aaronmoodie
Etsy Patterns
aaronmoodie
1
230
CreativeMornings Melbourne
aaronmoodie
2
150
Other Decks in Technology
See All in Technology
全自動で回せ!Claude Codeマーケットプレイス運用術
yukyu30
3
160
Webアクセシビリティ技術と実装の実際
tomokusaba
0
190
「使いにくい」も「運用疲れ」も卒業する UIデザイナーとエンジニアが創る持続可能な内製開発
nrinetcom
PRO
1
770
Serverless Agent Architecture on Azure / serverless-agent-on-azure
miyake
1
130
Master Dataグループ紹介資料
sansan33
PRO
1
4.4k
Microsoft Fabric のワークスペースと容量の設計原則
ryomaru0825
2
230
LLM活用の壁を超える:リクルートR&Dの戦略と打ち手
recruitengineers
PRO
1
200
EMからVPoEを経てCTOへ:マネジメントキャリアパスにおける葛藤と成長
kakehashi
PRO
5
510
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
10k
マルチロールEMが実践する「組織のレジリエンス」を高めるための組織構造と人材配置戦略
coconala_engineer
2
220
オンプレとGoogle Cloudを安全に繋ぐための、セキュア通信の勘所
waiwai2111
3
1.1k
Secure Boot 2026 - Aggiornamento dei certificati UEFI e piano di adozione in azienda
memiug
0
130
Featured
See All Featured
The SEO Collaboration Effect
kristinabergwall1
0
380
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
200
It's Worth the Effort
3n
188
29k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
230
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
71
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
450
Deep Space Network (abreviated)
tonyrice
0
85
How GitHub (no longer) Works
holman
316
140k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
190
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
190
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