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
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
150
Other Decks in Technology
See All in Technology
インターネットの技術 / Internet technology
ks91
PRO
0
210
Eight Engineering Unit 紹介資料
sansan33
PRO
3
7.3k
Practical TypeProf: Lessons from Analyzing Optcarrot
mame
0
350
AI時代のガードレールとしてのAPIガバナンス
nagix
0
280
Do Ruby::Box dream of Modular Monolith?
joker1007
1
340
AndroidアプリとCopilot Studioの統合
nakasho
0
100
サイボウズ 開発本部採用ピッチ / Cybozu Engineer Recruit
cybozuinsideout
PRO
10
78k
Claude Code を安全に使おう勉強会 / Claude Code Security Basics
masahirokawahara
11
33k
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
16k
Master Dataグループ紹介資料
sansan33
PRO
1
4.6k
クラウドネイティブな開発 ~ 認知負荷に立ち向かうためのコンテナ活用
literalice
0
130
Choose your own adventure in agentic design patterns
glaforge
0
140
Featured
See All Featured
Building Adaptive Systems
keathley
44
3k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
170
Measuring & Analyzing Core Web Vitals
bluesmoon
9
810
The Cost Of JavaScript in 2023
addyosmani
55
9.8k
Context Engineering - Making Every Token Count
addyosmani
9
830
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
Faster Mobile Websites
deanohume
310
31k
GraphQLとの向き合い方2022年版
quramy
50
15k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
64
55k
30 Presentation Tips
portentint
PRO
1
270
YesSQL, Process and Tooling at Scale
rocio
174
15k
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