

Knex and Bookshelf – ORM for JavaScript - poseid
http://thinkingonthinking.com/Bookshelf-an-ORM-for-Node/

======
bergie
Knex is pretty cool... clean API for constructing queries, and promises to
manage them. I think one wouldn't even need an ORM on top of that.

(Disclaimer: I'm just now in process of writing some components to wrap the
Knex API into NoFlo graphs)

~~~
enen
How does Knex compare to [https://github.com/brianc/node-
sql](https://github.com/brianc/node-sql)

------
mmorris
There's also Sequelize, another Node.js SQL ORM:
[http://sequelizejs.com/](http://sequelizejs.com/)

~~~
nilgradisnik
I'm glad to see more alternatives to Sequelize which I'm using. I was just
wondering how does Bookshelf compare to Sequelize in terms of maturity and
feature set, if anyone here has used both?

~~~
tgriesser
I'd actually created Bookshelf after seeing a lack of good support for SQL
abstractions in javascript, especially comparison to other languages and
frameworks.

In terms of maturity, Bookshelf & Knex are still definitely young and a work
in progress, but fairly mature in the sense that they are both heavily
inspired (and in Knex's case, mostly copied) from the excellent Laravel PHP
framework's query builder and ORM. Bookshelf also adapts different conventions
I found useful from Backbone.js' Models, Collections, and Events - I'm also
aiming to eventually make the libraries usable outside of Node (webSQL or
otherwise).

I've been looking to take the best from other languages' data interfaces and
try to adapt them into a few great packages for javascript, as opposed to
completely re-inventing the wheel.

Some of the differentiating features include eager-loading and nested eager-
loading relations, the ability to constrain those relations[1], polymorphic
relations, a distinct separation between the ORM and the query builder layer,
and until recently transactions (which from what I've heard, sequelize has
begun to add to the library).

Some things that Bookshelf doesn't have out of the box are explicit
typecasting (hooks exist to handle this if you need, but most times the db
takes care of this for you) or validations (IMO out of scope for
Bookshelf/Knex, but something I'll have incorporated in my next database
package), and static finder methods `User.find(1)`, `User.findOrCreate({...})`
- easily added but these will also come in the next package in progress.

Bookshelf aims to be a simple Data-Mapper, rather than a full out Rails
ActiveRecord style ORM... This is something that will be a different project
entirely which I'm actively working on building out. Sitting atop of
Bookshelf, it will include scopes, validators, callbacks, helper methods, etc.

[1]:
[https://github.com/tgriesser/bookshelf/issues/69#issuecommen...](https://github.com/tgriesser/bookshelf/issues/69#issuecomment-25053860)

------
antjanus
I opt to bypass Bookshelf entirely and go for the Knex query builder (with my
own wrapper) especially when it comes to API creation.

The best thing? By using custom selector names (whatever.what AS
something:else) and using
[Treeize]([https://npmjs.org/package/treeize](https://npmjs.org/package/treeize)),
you can achieve a deep-object graph effect. Which is really neat.

------
hayksaakian
The closest analogue for client side is brian.io/lawnchair

I haven't been keeping up with it, but is there anything better now?

------
yoloswagger
Any thoughts on migrations with Bookshelf?

~~~
bergie
Bookshelf is running on top of Knex.js, so I suppose you can use its migration
features:

[https://github.com/tgriesser/knex/blob/master/bin/readme.md](https://github.com/tgriesser/knex/blob/master/bin/readme.md)

There is also a Grunt plugin:

[https://npmjs.org/package/grunt-knex-
migrate](https://npmjs.org/package/grunt-knex-migrate)

