Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which ORM for node.js? [closed]

I know this is a common question but i've done tests and i need some particular features!

The features i need are:

  • map properties to column names
  • use a table name different from the model name
  • support for soft-deletes (paranoid mode on sequelize)
  • support for record timestamping (with the ability to specify, for every different model, the column name)
  • support for foreign keys
  • must support mysql and sqlite
  • the architecture must support a model per file

Optional features:

  • cache (support for redis/memcache)
  • command line tool to generate models from database

I've tested:

  1. node-orm
    • to handle own column names you need a workaround
    • soft deletes aren't supported and can't be supported even using an external plugin (i tried to wrote one using beforeRemove hook, but i can "stop" it from removing the record)
    • don't support a model per file (you need a workaround)
  2. sequelize
    • don't create foreign keys
    • can't map properties to column names
    • support a model per file but it doesn't work very well (you need to put relationship in the file that include the models)
  3. node-persist
    • i don't like the need to specify the connection instance for everything

Right now i'm going to test JugglingDB and Bookshelf.js (but i don't like too much the last one).

like image 429
Daniele Salvatore Albano Avatar asked Oct 18 '13 10:10

Daniele Salvatore Albano


1 Answers

Bookshelf should currently support all of those:

  1. The mapping of columns to property names with the format and parse methods.
  2. Using a different table name with the tableName attribute.
  3. Timestamping can take custom columns with the hasTimestamp attribute.
  4. Foreign keys can be defined with knex schema builder... they're not well documented but you can see an example in the tests here
  5. Supports mysql, sqlite and postgres
  6. Definitely supports one model per file... the relations are defined in methods, so you can do:

    var Classroom = Bookshelf.Model.extend({
      tableName: 'classrooms',
      student: function() {
        // Relating to a model from a file in the same directory.
        return this.hasMany(require('./student'));
      }
    });
    
    new Classroom({id: 1})
      .fetch({withRelated: ['students'])
      .then(function(classroom) {
         console.log(JSON.stringify(classroom));
      });
    

Official soft delete support is in the works, but is definitely easily achieved by extending the model and providing a new destroy method like so:

destroy: function(options) {
   if (options.softDelete) {
     return this.save({'deleted_at': new Date});
   }
   return bookshelf.Model.prototype.destroy.call(this, arguments);
}

It doesn't hook into a cache yet, because cache invalidation on relations is pretty tricky, definitely something under consideration.

If you see anything that seems to be missing, feel free to open a ticket.

like image 175
tgriesser Avatar answered Oct 06 '22 07:10

tgriesser