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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
aaronmoodie
October 15, 2015
Technology
0
270
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
SaaSに宿る21g
kanyamaguc
2
180
AI時代のIssue駆動開発のススメ
moongift
PRO
0
290
Why we keep our community?
kawaguti
PRO
0
340
The Rise of Browser Automation: AI-Powered Web Interaction in 2026
marcthompson_seo
0
310
開発チームとQAエンジニアの新しい協業モデル -年末調整開発チームで実践する【QAリード施策】-
kaomi_wombat
0
270
Sansanの認証基盤を支えるアーキテクチャとその振り返り
sansantech
PRO
1
120
Oracle AI Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
4
1.3k
GitHub Actions侵害 — 相次ぐ事例を振り返り、次なる脅威に備える
flatt_security
8
6.5k
Network Firewall Proxyで 自前プロキシを消し去ることができるのか
gusandayo
0
110
ハーネスエンジニアリング×AI適応開発
aictokamiya
1
740
開発チームとQAエンジニアの新しい協業モデル -年末調整開発チームで実践する【QAリード施策】-
qa
0
430
ThetaOS - A Mythical Machine comes Alive
aslander
0
220
Featured
See All Featured
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
150
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.4k
Exploring anti-patterns in Rails
aemeredith
2
300
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
180
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.2k
Paper Plane
katiecoart
PRO
0
48k
WCS-LA-2024
lcolladotor
0
500
Being A Developer After 40
akosma
91
590k
Are puppies a ranking factor?
jonoalderson
1
3.2k
Chasing Engaging Ingredients in Design
codingconduct
0
150
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Odyssey Design
rkendrick25
PRO
2
560
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