diff --git a/index.js b/index.js index 6595d25..1cc89af 100644 --- a/index.js +++ b/index.js @@ -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: {}, } + //================================================================ + }, diff --git a/lib/initialize.js b/lib/initialize.js index c78279f..977921f 100644 --- a/lib/initialize.js +++ b/lib/initialize.js @@ -208,6 +208,55 @@ 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. @@ -215,7 +264,7 @@ module.exports = function initialize(hook, sails, done){ // 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) {