Bookshelf.js: An ORM for Node.js with transactions, eager loading, promises
bookshelfjs.orgWe've been using this in our app. So far it's been awesome. We've completely overhauled our server in a few weeks.
This looks really great! One thing really bothers me however: a dependency on all of Backbone. Pulling in a bunch a view-related code for an ORM has a certain code smell.
Is there any effort to modularize Backbone itself, so that Bookshelf can only depend on the model and collection pieces?
Thanks for checking it out! While the view/history/router related code should be okay just sitting there unused, I agree that does seem like a bit of a code smell. I know Jeremy has been against modularizing Backbone in the past, and there is something to be said about keeping everything packaged nicely together in a single file - though maybe something like this would change his mind.
It's definitely something I'll begin to look into a bit, possibly splitting up the Backbone components into a separate library specifically for Bookshelf, similar to lodash's custom build process. If you have any ideas for a good way to go about this, feel free to open a ticket.
Demis Bellot took a crack at modularizing jQuery, perhaps there is inspiration there: https://github.com/mythz/jquip
Awesome! How does it compare with Sequel.js? http://www.sequelizejs.com/
Bookshelf only focuses on the relational aspect of the ORM, sitting on top of an full featured underlying query builder, http://knexjs.org. It handles eager/nested eager loading properly, as you'd see in something like Ruby's DataMapper or Active Record. It also defines relationships with named methods on the model, which allows the creation of really dynamic relationship with different constraints, etc.
It also has support for handling database transactions, which seemed to be missing in any other libraries I had looked at, and also doesn't force you into a particular way of doing other things like validations or typecasting (though it does provide hooks/events for handling both).
The ORM is also just a single ~900 line file that takes a lot of inspiration from Backbone as being a library you can easily read through and understand, and focus on the important parts of database interaction, rather than trying to provide the kitchen sink.
Single 900 line file does not seem like a pro - non-modular code is a con
I feel the same. Why would it be a good thing?
Looks awesome! Nice work.
Thanks, you might also like to checkout the underlying query builder library at http://knexjs.org.
This is a port of Eloquent from Laravel [0], which is brilliant. I haven't used this, but if it's a faithful port it'll be awesome.
[0] https://twitter.com/laravelphp/status/333971780886728705
The underlying query builder - http://knexjs.org - is much more of a 1-to-1 port. Bookshelf shares a number of similar features with eloquent, such as the process of defining relations, and handling eager/nested eager loading, but it also draws a lot of inspiration (and code) from the Model & Collection patterns of Backbone.js.
I owe a lot of thanks to Taylor for writing Eloquent, he has been really helpful with any questions I've had in putting this together.
I love Eloquent and I love Backbone. It's awesome to see that you've harmonized the two for Node. I've only used SQL with Node once, and wrote raw queries for the most part. I'm looking forward to refactoring everything to use this.
Nice work!
Ah, should have checked sorry, didn't notice you were the author submitting it. This looks great, cheers! :)
wow this looks great!