forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Broke out submodules in orm hook for maintainability.
- Loading branch information
1 parent
71bbcdf
commit 29bc578
Showing
7 changed files
with
422 additions
and
344 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
lib/hooks/orm/backwards-compatibility/upgrade-datastore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var path = require('path'); | ||
var fs = require('fs'); | ||
var Err = require('../../../../errors'); | ||
|
||
|
||
/** | ||
* Normalize properties of a datastore/connection | ||
* (handles deprecation warnings / validation errors and making types consistent) | ||
* | ||
* @param {Object} connection | ||
* connection.adapter // Name of adapter module used by this connection | ||
* connection.module // Deprecated- equivalent to `connection.adapter` | ||
* | ||
* @param {String} modelID | ||
* // Optional, improves quality of error messages | ||
* // Identity of the model this connection came from | ||
* | ||
* @throws {Err.fatal} __UnknownConnection__ | ||
* @throws {Err.fatal} __InvalidConnection__ | ||
* @throws {Err.fatal} __InvalidAdapter__ | ||
* @api private | ||
*/ | ||
|
||
module.exports = function howto_normalizeDatastore(sails){ | ||
return function normalizeDatastore(connection, modelID) { | ||
|
||
// Connection specified has not been configured | ||
var connectionObject = sails.config.connections[connection]; | ||
if (!connectionObject) return Err.fatal.__InvalidConnection__(connection, modelID); | ||
|
||
// Backwards compatibilty for `connection.module` | ||
if (connectionObject.module) { | ||
sails.log.verbose( | ||
'Deprecation warning :: In model `' + modelID + '`\'s `connection` config, ' + | ||
'replacing `module` with `adapter`....'); | ||
connectionObject.adapter = connectionObject.module; | ||
delete connectionObject.module; | ||
} | ||
|
||
var moduleName = connectionObject.adapter; | ||
|
||
// Adapter is required for a connection | ||
if (!connectionObject.adapter) { | ||
// Invalid connection found, throw fatal error. | ||
return Err.fatal.__InvalidConnection__(connectionObject, modelID); | ||
} | ||
|
||
// Verify that referenced adapter has been loaded | ||
// If it doesn't, try and load it as a dependency from `node_modules` | ||
if (!sails.adapters[connectionObject.adapter]) { | ||
|
||
// (Format adapter name to make sure we make the best attempt we can) | ||
if (!moduleName.match(/^(sails-|waterline-)/)) { | ||
moduleName = 'sails-' + moduleName; | ||
} | ||
|
||
// Since it is unknown so far, try and load the adapter from `node_modules` | ||
sails.log.verbose('Loading adapter (', moduleName, ') for ' + modelID, ' from `node_modules` directory...'); | ||
|
||
// Before trying to actually require the adapter, make sure we know the real module path: | ||
var node_modules = path.resolve(sails.config.appPath, 'node_modules'); | ||
var modulePath = path.join(node_modules, moduleName); | ||
|
||
// Then make sure the module exists | ||
if (!fs.existsSync(modulePath)) { | ||
|
||
// If adapter doesn't exist, log an error and exit | ||
return Err.fatal.__UnknownAdapter__(connectionObject.adapter, modelID, sails.majorVersion, sails.minorVersion); | ||
} | ||
|
||
// Since the module seems to exist, try to require it from the appPath (execute the code) | ||
try { | ||
sails.adapters[moduleName] = require(modulePath); | ||
} catch (e) { | ||
return Err.fatal.__InvalidAdapter__(moduleName, e); | ||
} | ||
} | ||
|
||
// Defaults connection object to its adapter's defaults | ||
// TODO: pull this out into waterline core | ||
var itsAdapter = sails.adapters[connectionObject.adapter]; | ||
connection = _.merge({}, itsAdapter.defaults, connectionObject); | ||
|
||
// If the adapter has a `registerCollection` method, it must be a v0.9.x adapter | ||
if (itsAdapter.registerCollection) { | ||
sails.log.warn('The adapter `' + connectionObject.adapter + '` appears to be designed for an earlier version of Sails.'); | ||
sails.log.warn('(it has a `registerCollection()` method.)'); | ||
sails.log.warn('Since you\'re running Sails v0.10.x, it probably isn\'t going to work.'); | ||
sails.log.warn('To attempt to install the updated version of this adapter, run:'); | ||
sails.log.warn('npm install ' + connectionObject.adapter + '@0.10.x'); | ||
return Err.fatal.__InvalidAdapter__(moduleName, 'Adapter is not compatible with the current version of Sails.'); | ||
} | ||
|
||
|
||
// Success- connection normalized and validated | ||
// (any missing adapters were either acquired, or the loading process was stopped w/ a fatal error) | ||
return connection; | ||
}; | ||
}; |
48 changes: 48 additions & 0 deletions
48
lib/hooks/orm/backwards-compatibility/upgrade-model-definition.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var _ = require('lodash'); | ||
var Err = require('../../../../errors'); | ||
|
||
module.exports = function (sails) { | ||
return function (modelDef) { | ||
|
||
// Backwards compatibilty for `Model.adapter` | ||
if (modelDef.adapter) { | ||
sails.log.verbose( | ||
'Deprecation warning :: ' + | ||
'Replacing `' + modelDef.globalId + '.adapter` ' + | ||
'with `' + modelDef.globalId + '.connection`....'); | ||
modelDef.connection = modelDef.adapter; | ||
delete modelDef.adapter; | ||
} | ||
|
||
// Backwards compatiblity for lifecycle callbacks | ||
if (modelDef.beforeValidation) { | ||
sails.log.verbose( | ||
'Deprecation warning :: the `beforeValidation()` model lifecycle callback is now `beforeValidate()`.\n' + | ||
'For now, I\'m replacing it for you (in `' + modelDef.globalId + '`)...'); | ||
modelDef.beforeValidate = modelDef.beforeValidation; | ||
} | ||
if (modelDef.afterValidation) { | ||
sails.log.verbose( | ||
'Deprecation warning :: the `afterValidation()` model lifecycle callback is now `afterValidate()`.\n' + | ||
'For now, I\'m replacing it for you (in `' + modelDef.globalId + '`)...'); | ||
modelDef.afterValidate = modelDef.afterValidation; | ||
} | ||
|
||
// If no connection can be determined (even by using app-level defaults [config.models]) | ||
// throw a fatal error. | ||
if (!modelDef.connection) { | ||
return Err.fatal.__ModelIsMissingConnection__(modelDef.globalId); | ||
} | ||
|
||
// Coerce `Model.connection` to an array | ||
if (!_.isArray(modelDef.connection)) { | ||
modelDef.connection = [modelDef.connection]; | ||
} | ||
|
||
return modelDef; | ||
}; | ||
}; |
85 changes: 85 additions & 0 deletions
85
lib/hooks/orm/backwards-compatibility/upgrade-sails.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var _ = require('lodash'); | ||
var STRINGFILE = require('sails-stringfile'); | ||
|
||
|
||
|
||
module.exports = function (sails) { | ||
return function (config) { | ||
|
||
////////////////////////////////////////////////////////////////////////////////////////// | ||
// Backwards compat. for `config.adapters` | ||
////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
// `config.adapters` is now `config.connections` | ||
if (config.adapters) { | ||
|
||
// `config.adapters.default` is being replaced with `config.models.connection` | ||
if (config.adapters['default']) { | ||
|
||
sails.after('lifted', function() { | ||
|
||
STRINGFILE.logDeprecationNotice( | ||
'config.adapters.default', | ||
STRINGFILE.get('links.docs.migrationGuide.connections'), | ||
sails.log.debug) && | ||
STRINGFILE.logUpgradeNotice( | ||
STRINGFILE.get('upgrade.config.models.connection'), [], sails.log.debug); | ||
|
||
}); | ||
|
||
config.models.connection = config.models.connection || config.adapters['default']; | ||
} | ||
|
||
// Merge `config.adapters` into `config.connections` | ||
sails.after('lifted', function() { | ||
|
||
STRINGFILE.logDeprecationNotice( | ||
'config.adapters', | ||
STRINGFILE.get('links.docs.migrationGuide.connections'), | ||
sails.log.debug) && | ||
STRINGFILE.logUpgradeNotice( | ||
STRINGFILE.get('upgrade.config.connections'), [], sails.log.debug); | ||
|
||
}); | ||
_.each(config.adapters, function(legacyAdapterConfig, connectionName) { | ||
|
||
// Ignore `default` | ||
// (it was a special case in Sails versions <= v0.10) | ||
if (connectionName === 'default') { | ||
return; | ||
} | ||
|
||
// Normalize `module` to `adapter` | ||
var connection = _.clone(legacyAdapterConfig); | ||
connection.adapter = connection.module; | ||
delete connection.module; | ||
|
||
sails.after('lifted', function() { | ||
|
||
STRINGFILE.logDeprecationNotice( | ||
'config.adapters.*.module', | ||
STRINGFILE.get('links.docs.migrationGuide.connections'), | ||
sails.log.debug) && | ||
STRINGFILE.logUpgradeNotice( | ||
STRINGFILE.get('upgrade.config.connections.*.adapter'), [connectionName], sails.log.debug); | ||
|
||
}); | ||
config.connections[connectionName] = config.connections[connectionName] = connection; | ||
}); | ||
|
||
// If there was default adapter in config.adapters, migrate it to be the default connection. | ||
if (config.adapters && config.adapters.default) { | ||
config.models.connection = config.adapters.default; | ||
} | ||
|
||
delete config.adapters; | ||
|
||
} // </if (config.adapters) > | ||
|
||
return config; | ||
}; | ||
}; |
Oops, something went wrong.