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.
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
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
19. Plugins
** included
Registry
- reduce circular references
less of an issue with es6
Virtuals
- calculated attributes
Visibility
- whitelist/blacklist attributes
19
20. Agenda
1. What it is?
2. What it provides?
3. How to use it?
4. How we use it?
20
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});
});
}
};
It’s lean
Simple to read and understand and extend
Flexible - validation
First class transaction support
Not just Models
represents additional tools available
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
Common Rel DB platforms
If familiar with Backbone - easy
Used internally on Fetch/Load, etc similar to Backbone
Registry helpful if using AMD style of Dependancy loading
Typical Con object, passed to Bookshelf and Bingo
Red lines = Polymorphic relationships
Show Demo
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.