Working with Bookshelf.

Bookshelf is an awesome ORM.
http://bookshelfjs.org/
But the documentation is lacking in examples like non other.

I’m new to the Node world and almost all of these docs have the assumptions you know everything about Node outside of just this small function that the library provides.

First, since node is async, Bookshelf is also. Bookshelf uses promises (while others use callbacks).
This is great for a website but not great of a utility tool since you are dependent on the data being there before the next step.
So you have to wait for the ‘.then’ or the ‘fetched’ event (still figuring out how to use that…)

Example Code:

//js
var newUser = {
'name': 'Joe',
'email': 'joe@joe.com'
}

new self.models.User(newUser).save().then(function (model) {
console.log(model);
});

That looks nice and all, BUT, now you want to send an email, well you have to do that inside of the .save or have some kind of callback.
A way around this is emit another event.
http://www.wekeroad.com/2012/04/05/cleaning-up-deep-callback-nesting-with-nodes-eventemitter/

Dealing with .fetchAll and collection.fetch

Getting Started with Bookshelf.js

Continue reading Working with Bookshelf.