diegoaguilar
diegoaguilar

Reputation: 8376

How to make a Sails model point to distinct databases on the go?

I'm creating a Sails application for which I got to do a panel administration for transactions to a model database. The point is, the backend architecture and data administration requires distinct databases for for same model in Sails application.

So, my model looks like this:

module.exports = {

  connection: 'seasonDB',
  autoPK: false,

  attributes: {
    id: {
      type: 'string',
      primaryKey: true,
      required: true
    },
    avatar: {
      type: 'string',
      defaultsTo: ''
    },
    price: {
      type: 'integer',
      defaultsTo: function () {
        var prices = [ 7000000, 5000000, 4000000, 2500000, 1000000 ];
        return prices[_.random(prices.length -1)];
      }
    },
    description: {
      type: 'string',
      defaultsTo: ''
    },
    status: {
      type: 'string',
      enum: ['suspended', 'inSquad','injured','out']
    }
  }
};

Where 'seasonDB' is defined at config/connections.js:

  seasonDB: {
    adapter: process.env.SEASON_DB_ADAPTER,
    host: process.env.SEASON_DB_HOST,
    port: process.env.SEASON_DB_PORT,
    user: process.env.SEASON_DB_USER,
    password: process.env.SEASON_DB_PASSWORD,
    database: process.env.SEASON_DB_DB
  }

So, is there any way to change the environmental variables and somehow reload the models with out reloading whole application?

Upvotes: 1

Views: 117

Answers (1)

Konstantin Zolotarev
Konstantin Zolotarev

Reputation: 622

There is a hook for sails.js named sails-hook-autoreload. It tracks changes into controllers, models and apply changes without manual reload. You could try to use it or create something similar only for models.

Upvotes: 3

Related Questions