Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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://book...
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
**...
Models &
Collections
● Ordered sets of Models
● Proxies Lodash
● Instance & Class
properties
10
** (mostly) follows
Backbo...
Entity
Relationships
// containment
hasOne()
hasMany()
// membership
belongsTo()
belongsToMany()
11
Entity
Relationships
// joins (bridge relations)
through(joinModel)
// joinModel methods
attach()
detach()
withPivot()
upd...
Polymorphism
13
“.. polymorphism refers to
a languages ability to
process objects
differently depending on
their data type...
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 e...
Events
// event handlers
on()
off()
16
// register listeners
once()
// promised triggers
triggerThen()
// event method
tri...
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
Visib...
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(); // kne...
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 ?
Upcoming SlideShare
Loading in …5
×

Bookshelf JS Buenos Aires NodeJS Meetup jul 2016

321 views

Published on

Slides used for talk to Buenos Aires NodeJS Meetup.
Covers Node library BookshelfJS, which I have used for 4+ years.
Bookshelf-demo repo will be partnered with front-end (using MarionetteJS) repo to demonstrate complete working system.

Aspiration is to provide working example of full Express4 API with Sockets support.

Aim is to provide those interested in getting to know Bookshelf with a less trivial example than typical Todo application.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Bookshelf JS Buenos Aires NodeJS Meetup jul 2016

  1. 1. BOOKSHELF.JS http://bookshelfjs.org
  2. 2. sh-3.2# whoami greg mcdowell 2
  3. 3. 1. What it is? 2. What it provides? 3. How to use it? 4. How we use it? 3
  4. 4. “... library for common tasks relating to querying, and managing relations in, a Relational Database.” source: http://bookshelfjs.org 4
  5. 5. Models Views Controllers 5
  6. 6. Dependencies 6 lodash
  7. 7. 1. What it is? 2. What it provides? 3. How to use it? 4. How we use it? 7
  8. 8. DB Abstraction ● MySQL ● Postgres ● SQLite3 ● MSSQL 8
  9. 9. Models & Collections ● Individual database record ● Domain-specific methods & relations ● Instance & Class properties 9 ** (mostly) follows Backbone conventions
  10. 10. Models & Collections ● Ordered sets of Models ● Proxies Lodash ● Instance & Class properties 10 ** (mostly) follows Backbone conventions
  11. 11. Entity Relationships // containment hasOne() hasMany() // membership belongsTo() belongsToMany() 11
  12. 12. Entity Relationships // joins (bridge relations) through(joinModel) // joinModel methods attach() detach() withPivot() updatePivot() 12
  13. 13. 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
  14. 14. Polymorphic Relationships // containment morphOne() morphMany() // membership morphTo() 14
  15. 15. Eager & Lazy // eager load on instantiation of object new M.fetch({withRelated: ‘related’}) // lazy load related data on existing object obj.load(‘related’) 15
  16. 16. Events // event handlers on() off() 16 // register listeners once() // promised triggers triggerThen() // event method trigger()
  17. 17. Promises or Callbacks or Streams Just use Promises cause they’re AWESOME! 17
  18. 18. Utilities Knex: - Query Builder - Schema Builder - Migrations & Seeds Transactions - alias Knex.Transaction 18
  19. 19. Plugins ** included Registry - reduce circular references less of an issue with es6 Virtuals - calculated attributes Visibility - whitelist/blacklist attributes 19
  20. 20. Agenda 1. What it is? 2. What it provides? 3. How to use it? 4. How we use it? 20
  21. 21. // 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. 22. 22 Demo
  23. 23. Agenda 1. What it is? 2. What it provides? 3. How to use it? 4. How we use it? 23
  24. 24. 24
  25. 25. 25 Sample App: Demo repo: https://github.com/gmcdowell/bookshelf-demo Questions ?

×