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
190
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
140
CreativeMornings Melbourne
aaronmoodie
2
110
Other Decks in Technology
See All in Technology
web-application-security
matsuihidetoshi
1
190
よく聞くけど使ったことないソフトウェアNo.1 KafkaとSnowflake
foursue
4
510
Android Target SDK 35 (Android 15) 対応の概要
akkie76
0
160
Babylon.jsと色々なものを組み合わせる:ブラウザのAPIやガジェットや2D描画ライブラリなど / Babylon.js 勉強会 vol.3
you
PRO
0
170
一生覚えておきたい「システム開発=コミュニケーション」〜初めての実務案件振り返りLT〜
maimyyym
2
320
ワールドカフェI /チューターを改良する / World Café I and Improving the Tutors
ks91
PRO
0
150
Tellus の衛星データを見てみよう #mf_fukuoka
kongmingstrap
0
280
Cracking the KubeCon CfP
inductor
2
270
中年男性がメインフレームから クラウドへキャリアシフトしてみた
uechishingo
0
210
データベース02: データベースの概念
trycycle
0
180
Autonomous Database Cloud 技術詳細 / adb-s_technical_detail_jp
oracle4engineer
PRO
15
35k
【SORACOM UG 東海】あらゆるモノがつながる社会へ、IoT と SORACOM
soracom
PRO
1
150
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
30
6.4k
The Language of Interfaces
destraynor
151
23k
Debugging Ruby Performance
tmm1
70
11k
Side Projects
sachag
451
41k
GraphQLとの向き合い方2022年版
quramy
33
12k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
The Pragmatic Product Professional
lauravandoore
26
5.8k
Design by the Numbers
sachag
274
18k
The Power of CSS Pseudo Elements
geoffreycrofte
62
5k
Building Your Own Lightsaber
phodgson
100
5.7k
Why Our Code Smells
bkeepers
PRO
331
56k
4 Signs Your Business is Dying
shpigford
176
21k
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