Skip to content

Commit

Permalink
Added production check with warning log, and 'sails.config.orm.skipPr…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Mar 15, 2016
1 parent 4970bf4 commit 9a0d46e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,20 @@ module.exports = function (sails) {
// Default model/adapter definitions to automatically attach
// to `sails.hooks.orm.adapters` and/or `sails.hooks.orm.models`.
orm: {

// By default, relevant warnings are shown when NODE_ENV is "production".
skipProductionWarnings: false,

//================================================================
// Experimental
// (may change at any time!)
//================================================================
moduleDefinitions: {
models: {},
adapters: {},
}
//================================================================

},


Expand Down
51 changes: 50 additions & 1 deletion lib/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,63 @@ module.exports = function initialize(hook, sails, done){
}],



// ╔═╗╦═╗╔═╗╔╦╗╦ ╦╔═╗╔╦╗╦╔═╗╔╗╔ ╔═╗╦ ╦╔═╗╔═╗╦╔═
// ╠═╝╠╦╝║ ║ ║║║ ║║ ║ ║║ ║║║║ ║ ╠═╣║╣ ║ ╠╩╗
// ╩ ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩ ╩╚═╝╝╚╝ ╚═╝╩ ╩╚═╝╚═╝╩ ╩
// ┌─ ┬ ┬┌─┐┬─┐┌┐┌┬┌┐┌┌─┐┌─┐ ─┐
// │───│││├─┤├┬┘││││││││ ┬└─┐ ───│
// └─ └┴┘┴ ┴┴└─┘└┘┴┘└┘└─┘└─┘ ─┘
//
// If NODE_ENV is "production", check if any models are using
// a datastore running on `sails-disk`. If so, show a warning.
_productionCheck: ['_normalizeModelDefs', function (next) {

// We use `process.env.NODE_ENV` instead of `sails.config.environment`
// to allow for the environment to be set to e.g. "staging" while the
// NODE_ENV is set to "production".
if (process.env.NODE_ENV === 'production') {
// > **Remember:**
// > In a production environment, regardless of your logical `environment`
// > config, the NODE_ENV environment variable should be set. Setting
// > `sails.config.environment` to production does this automatically.

// e.g. ['localDiskDb', 'foobar']
var datastoresUsingSailsDisk = _.reduce(sails.config.connections, function(memo, datastoreConf, identity){
if (datastoreConf.adapter === 'sails-disk') {
memo.push(identity);
}
return memo;
}, []);

// e.g. ['user', 'product']
var modelsUsingSailsDisk = _.reduce(hook.models, function(memo, normalizedModelDef, identity){
if (_.contains(datastoresUsingSailsDisk, normalizedModelDef.connection)) {
memo.push(identity);
}
return memo;
}, []);

if (modelsUsingSailsDisk.length > 0) {
sails.log.warn('The default `sails-disk` adapter is not designed for use as a production database;');
sails.log.warn('(it stores the entire contents of your database in memory)');
sails.log.warn('Instead, please use another adapter; e.g. sails-postgresql or sails-mongo.');
sails.log.warn('For more info, see: http://sailsjs.org/documentation/concepts/deployment');
sails.log.warn('To hide this warning message, enable `sails.config.orm.skipProductionWarnings`.');
}
}

return next();
}],

// Before continuing any further to actually start up the ORM,
// check the migrate settings for each model to prompt the user
// to make a decision if no migrate configuration is present.
//
// Note that, if this is a production environment, the `migrate`
// setting has already been forced to "safe" when the model
// definitions were validated/normalized.
_doubleCheckMigration: ['_normalizeModelDefs', function (next) {
_doubleCheckMigration: ['_productionCheck', function (next) {

// If there are no models, we're good.
if (_.keys(hook.models).length === 0) {
Expand Down

0 comments on commit 9a0d46e

Please sign in to comment.