Skip to content

KSDaemon/sails-hook-sequelize

Repository files navigation

sails-hook-sequelize

Sails.js hook to use sequelize ORM

NPM version Build Status Code coverage MIT License Known Vulnerabilities

Installation

Install this hook with:

$ npm install sails-hook-sequelize --save

Configuration

.sailsrc

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

Also you can set some parameters in config/sequelize.js to override defaults.

module.exports.sequelize = {
    "clsNamespace": "myAppCLSNamespace",
    "exposeToGlobal": true
};

Connections

Sequelize connection.

Important note: dialect keyword MUST be present in connection or connection.options.

somePostgresqlServer: {
  user: 'postgres',
  password: '',
  database: 'sequelize',
  dialect: 'postgres',
  options: {
    dialect: 'postgres',
    host   : 'localhost',
    port   : 5432,
    logging: console.log        // or specify sails log level to use ('info', 'warn', 'verbose', etc)
  }
}

Models

Sequelize model definition models/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
      }
    });
  },
  defaultScope: function() {
    return {
      include: [
        {model: image, as: 'images'}
      ]
    }
  },
  options: {                                  // Options must exists (even if empty) in order to consider this model a Sequelize model
    tableName: 'user',
    classMethods: {},
    instanceMethods: {},
    hooks: {},
    scopes: {},
  },
  connection: 'NotDefaultModelsConnection'    // Can be omitted, so default sails.config.models.connection will be used
};

Contributors

This project was originally created by Gergely Munkácsy (@festo). Now is maintained by Konstantin Burkalev (@KSDaemon).

License

MIT

Thanks JetBrains for support! Best IDEs for every language!

JetBrains