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
0
240
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
210
CreativeMornings Melbourne
aaronmoodie
2
140
Other Decks in Technology
See All in Technology
Operating Operator
shhnjk
1
620
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
3
460
マネジメントって難しい、けどおもしろい / Management is tough, but fun! #em_findy
ar_tama
7
1.2k
インフラ寄りSREの生存戦略
sansantech
PRO
7
2.7k
Delta airlines Customer®️ USA Contact Numbers: Complete 2025 Support Guide
deltahelp
0
930
Reach American Airlines®️ Instantly: 19 Calling Methods for Fast Support in the USA
flyamerican
1
180
How to Quickly Call American Airlines®️ U.S. Customer Care : Full Guide
flyaahelpguide
0
150
How Do I Contact HP Printer Support? [Full 2025 Guide for U.S. Businesses]
harrry1211
0
130
Lufthansa ®️ USA Contact Numbers: Complete 2025 Support Guide
lufthanahelpsupport
0
220
ゼロからはじめる採用広報
yutadayo
3
990
サイバーエージェントグループのSRE10年の歩みとAI時代の生存戦略
shotatsuge
4
440
AWS CDK 入門ガイド これだけは知っておきたいヒント集
anank
3
300
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
95
14k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
Agile that works and the tools we love
rasmusluckow
329
21k
Music & Morning Musume
bryan
46
6.6k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Fireside Chat
paigeccino
37
3.5k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
The Invisible Side of Design
smashingmag
301
51k
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