KSDaemon/sails-hook-sequelize

SailsJS v1 - Using custom datastore

sgates3414 opened this issue · 1 comments

I am having some issues with Sails v1 getting any datastore other than 'default' to function.

I have configured config\datastores.js as follows:

module.exports.datastores = {

  default: {

    user: 'user',
    password: 'pass',
    database: 'db',
    dialect: 'mssql',
    options: {
      dialect: 'mssql',
      host   : 'host.domain.com',
      port   : 1433,
      logging: console.log        // or specify sails log level to use ('info', 'warn', 'verbose', etc)
    }
  },


  test: {
    user: 'user',
    password: 'pass',
    database: 'db2',
    dialect: 'mssql',
    options: {
      dialect: 'mssql',
      host   : 'host2.domain.com',
      port   : 1433,
      logging: console.log        // or specify sails log level to use ('info', 'warn', 'verbose', etc)
    }
  }

};

I have configured my model as follows:

module.exports = {
  attributes: {
    id: {
      type: Sequelize.STRING,
      allowNull: false,
      primaryKey: true,
      field: 'proid'
    },
    name: {
      type: Sequelize.STRING,
      field: 'name'
    }
  },
  options: {
    tableName: 'inwork',
    classMethods: {},
    instanceMethods: {},
    hooks: {},
    scopes: {},
    connection: 'test',    // Can be omitted, so default sails.config.models.connection will be used
    timestamps: false
  }
};

A custom controller action and route is in place, which is working as expected, however the query fails since the default datastore is the only one being used. Due to the 'connection' option within models, it would appear that we should be able to use custom datastores, I'm just not sure what to change so that I stop reading the default store only, or if this is a bug with the current version.

Hi! I've just looked. There is mistake in docs: connection attribute should be specified one level up :)
Like this:

module.exports = {
  attributes: {
    id: {
      type: Sequelize.STRING,
      allowNull: false,
      primaryKey: true,
      field: 'proid'
    },
    name: {
      type: Sequelize.STRING,
      field: 'name'
    }
  },
  options: {
    tableName: 'inwork',
    classMethods: {},
    instanceMethods: {},
    hooks: {},
    scopes: {},
    timestamps: false
  },
  connection: 'test'
};