Choosing an ORM for node ? (Sequelize / Waterline / Bookshelf)

Posted on: 2018-03-11

Having tested those three ORMs recently, here are the elements that helped me made my choice. Hope it will help you too šŸ˜‰.

Migrations / Tables creation

  • Waterline: No built-in migration system. Tables are created automaticaly. There 2 settings though: one where the tables are created based on the model's definitions (the tables are re-created when the models change), the second (called "safe") where nothing is done automatically. This is the mode you want to use in production.
  • Sequelize : It has a built-in migration system (up, down, create...). There is also a mode where tables are created automatically but it's safer than waterline's: tables are created only if they do not exist (you can force sequelize to re-create tables when your models change, therefore making it work like waterline).
  • Bookshelf : Bookshelf is based on the knex query builder so if you want to create migrations for your models you can just use knex. Its a great library, but you will have to write the migration manually. Better than waterline but more time consuming than Sequelize.

āœ… Sequelize wins

Models definitions

For waterline, we have something like :

const project = { tableName : `project`, // ... attributes : { id : { type: `string`, uuidv4 : true, primaryKey: true, defaultsTo : () => uuid.v4() }, // ... team : { model : `team`, required : true }, } };

For Sequelize :

const Project = sequelize.define(`project`, { uid : { type: Sequelize.UUID, primaryKey: true, defaultValue: Sequelize.UUIDV4, }, }, { // ... } ); Project.belongsTo(Team);

And for Bookshelf, you don't have to list the attributes and their types :

const Project = bookshelf.Model.extend({ tableName: 'project', team () { return this.belongsTo(Team, `team_id`, `id`); } });

Bookshelf seems to win on that point, the model declaration is short and straightforward. Sequelize is the most verbose.

āœ… Bookshelf wins

Joins

With Waterline, we can defin joins pretty easily :

waterline.collections.project.find({ name : 'my project'}).populate('team');

But we can not retrieve nested relations, wich is possible with Sequelize :

Project.find({ where : { name : 'my project' } include : [ { model : Team, as : 'team', include : [ { model : User, as : 'users', } ], }, ], });

It seems to be possible with Bookshelf also (though I have to admit I didn't try personnaly) :

Project.fetch({withRelated: ['team.users'])

āœ… Bookshelf wins (Sequelize close second)

Performances

Check out this little test I made on github. Seems waterline is really behind Bookshelf and Sequelize in terms of performances.

āœ… Bookshelf and Sequelize win

conclusion

I would definitely not choose Waterline but both Sequelize and Bookshelf seem to be good choices. Sequelize is a bit more verbose but also it's older and might be more mature. Bookshelf is simpler yet still very powerfull and uses knex, so that means it may also be easy for some critical part of your application to build you own queries.

Anyway, writing this article also convinced me that sometimes, when you are are really concerned about perfomances and having control on what you are doing, a good query builder like knex instead of an ORM is really something to consider. šŸ¤”