BOOKSHELF.JS
http://bookshelfjs.org
sh-3.2# whoami
greg mcdowell
2
1. What it is?
2. What it provides?
3. How to use it?
4. How we use it?
3
“... library for common tasks
relating to querying, and
managing relations in, a
Relational Database.”
source: http://bookshelfjs.org
4
Models
Views
Controllers
5
Dependencies
6
lodash
1. What it is?
2. What it provides?
3. How to use it?
4. How we use it?
7
DB Abstraction
● MySQL
● Postgres
● SQLite3
● MSSQL
8
Models &
Collections
● Individual database record
● Domain-specific methods &
relations
● Instance & Class properties
9
** (mostly) follows
Backbone conventions
Models &
Collections
● Ordered sets of Models
● Proxies Lodash
● Instance & Class
properties
10
** (mostly) follows
Backbone conventions
Entity
Relationships
// containment
hasOne()
hasMany()
// membership
belongsTo()
belongsToMany()
11
Entity
Relationships
// joins (bridge relations)
through(joinModel)
// joinModel methods
attach()
detach()
withPivot()
updatePivot()
12
Polymorphism
13
“.. polymorphism refers to
a languages ability to
process objects
differently depending on
their data type or class.”
source: http://webopedia.com/TERM/P/polymorphism.html
Polymorphic
Relationships
// containment
morphOne()
morphMany()
// membership
morphTo()
14
Eager &
Lazy
// eager load on instantiation of object
new M.fetch({withRelated: ‘related’})
// lazy load related data on existing object
obj.load(‘related’)
15
Events
// event handlers
on()
off()
16
// register listeners
once()
// promised triggers
triggerThen()
// event method
trigger()
Promises
or
Callbacks
or
Streams
Just use Promises cause
they’re AWESOME!
17
Utilities
Knex:
- Query Builder
- Schema Builder
- Migrations & Seeds
Transactions
- alias Knex.Transaction
18
Plugins
** included
Registry
- reduce circular references
less of an issue with es6
Virtuals
- calculated attributes
Visibility
- whitelist/blacklist attributes
19
Agenda
1. What it is?
2. What it provides?
3. How to use it?
4. How we use it?
20
// orm.js – establish connection
import Knex from 'knex’;
import Bookshelf from ‘bookshelf’;
const DB = new Knex(); // knexfile.js
const Orm = new Bookshelf(DB);
export default Orm;
Access
21
// user.js – define a model for table
import Orm from ‘./orm’;
let User = Orm.Model.extend({
tableName:'users’}
);
export default User;
// users.js – user controller
import User from ‘./user’;
export default {
list(req, res) {
User.fetchAll()
.then(records => {
res.status(200).json(records);
})
.catch(err => {
res.status(400).json({error:err});
})
},
find(req, res){
User.forge(req.params.id)
.fetch()
.then(record => {
res.status(200).json(record);
})
.catch(err => {
res.status(400).json({error:err});
});
}
};
22
Demo
Agenda
1. What it is?
2. What it provides?
3. How to use it?
4. How we use it?
23
24
25
Sample App:
Demo repo:
https://github.com/gmcdowell/bookshelf-demo
Questions ?

Bookshelf JS Buenos Aires NodeJS Meetup jul 2016

Editor's Notes

  • #5 It’s lean Simple to read and understand and extend Flexible - validation First class transaction support
  • #6 Not just Models represents additional tools available
  • #7 Backbone - gives structure to Web Apps -> provides key-value binding, events and RESTful Api connection Underscore - JS Lib of useful functions/helpers = tool kit analogy Promises - represents state of async operation - intuitive chaining. Knex - batteries included SQL query builder
  • #9 Common Rel DB platforms
  • #10 If familiar with Backbone - easy
  • #17 Used internally on Fetch/Load, etc similar to Backbone
  • #20 Registry helpful if using AMD style of Dependancy loading
  • #22 Typical Con object, passed to Bookshelf and Bingo
  • #23 Red lines = Polymorphic relationships Show Demo
  • #25 Replaced RoR API for Mobile App Rails Web for Admins Required 2 skill sets in Biz: App build on MarionetteJS Implemented API for Mobile App & Admins (decoupled) Est. API for Integration partners Opens possibility for Electron Apps in future & others.