12

Waterline is an excellent ORM but I noticed that there are many features that are not present yet on waterline but Sequelize already have. So I have decided to switch to sequelize but still using Sails for the others things. I have search tutorial how to switch to sequelize but nothing. How can I replace Waterline for sequelize in sails Js?

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Miguel
  • 1,577
  • 2
  • 15
  • 29
  • Check this post https://groups.google.com/d/msg/sailsjs/ALMxbKfnCIo/H2RcRUnnFGEJ by Manuel Darveau. – Dário Jun 12 '15 at 10:33

1 Answers1

16

I've moved forward with sequelize as well, there are two project that came out really recently, so i would like to announce them.

sails-hook-sequelize:

It follows the answer by Manuel Darveau, it will fetch all your models, import through sequelize and serves your models as a global variables, you can force the sequelize syncronization with the same way with migrate: 'drop'

sails-hook-sequelize-blueprints

Sails blueprints has saved me a LOT of time, so i've wrote a fork to work with sequelize, it work the same way than original blueprints, and you'll still have the same blueprints configurations such as rest, shortcuts, prefix and so on, since waterline populate models with populateEach() function, it uses include: [{ all: true }] which the result is the same.

A full example:

$ npm install sails-hook-sequelize
$ npm install sails-hook-sequelize-blueprints
$ npm install sequelize
$ npm install pg pg-hstore
$ npm install continuation-local-storage

.sailsrc

"hooks": {
    "blueprints": false,
    "orm": false,
    "pubsub": false
}

connections.js

somePostgresqlServer: {
    user: 'postgres',
    password: '',
    database: 'database',
    dialect: 'postgres',
    options: {
        dialect: 'postgres',
        host   : 'localhost',
        port   : 5432,
        logging: true
   }
}

Your model definition

// user.js
module.exports = {
  attributes: {
    name: {
      type: Sequelize.STRING,
      allowNull: false
    },
    age: {
      type: Sequelize.INTEGER
    }
  },
  associations: function() {
    user.hasMany(image, {
      foreignKey: {
        name: 'owner',
        allowNull: false
      }
    });
  },
  options: {
    tableName: 'user',
    classMethods: {},
    instanceMethods: {},
    hooks: {}
  }
};

That's it.

FXCesinha
  • 393
  • 3
  • 16
  • 1
    Does this use sequelize migrations as well? – nahtnam Sep 04 '15 at 21:17
  • Sequelize migrations actually works with CLI, so you can do migrations in anywhere, you just need a config/config.json containing the database connections, generate the migration file, add some columns and it's done. – FXCesinha Sep 06 '15 at 05:04
  • @FXCesinha, what do I do when I want to use multiple connections. http://stackoverflow.com/questions/36565379/use-multiple-datastore-connections-with-sequelize?noredirect=1#comment60735265_36565379 – Milkncookiez Apr 12 '16 at 09:24
  • This example doesn't not work appropriately. Your "User" model return a error: "ReferenceError: user is not defined". – alvaropaco Sep 13 '16 at 19:41
  • @alvaropaco take a look on the sample app https://github.com/cesardeazevedo/sails-hook-sequelize-blueprints/tree/master/test/fixtures/sampleapp – FXCesinha Sep 13 '16 at 23:00
  • 1
    I'd just like to add to this excellent answer, that if you'd like to decrypt and inject your secrets (username and pswd) for the db user at runtime, you'd have to reload (re-initialize) the hook. And if you'd like to simultaneously connect to multiple DBs (that are supported by Sequelize), you can't do that with the current version of the hook. Fortunately, I've made a PR that fixes both of these issues (it is tested and working in production for multiple months now): https://github.com/festo/sails-hook-sequelize/pull/22 – Milkncookiez Sep 19 '16 at 09:59