Skip to content

Commit

Permalink
Deprecation warnigns and cleaup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Oct 7, 2013
1 parent 604bb54 commit dfbcfdb
Showing 1 changed file with 48 additions and 27 deletions.
75 changes: 48 additions & 27 deletions lib/hooks/orm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ module.exports = function(sails) {

// Backwards compatibilty for `Model.adapter`
if (modelDef.adapter) {
sails.log.verbose('Deprecation warning :: Replacing `config.adapters.default` with `config.model.connections`....');
sails.log.verbose(
'Deprecation warning :: ' +
'Replacing `' + modelDef.globalId + '.adapter` ' +
'with `' + modelDef.globalId + '.connections`....');
modelDef.connections = modelDef.adapter;
delete modelDef.adapter;
}

// Ensure that Model.connections is an array
Expand All @@ -70,7 +74,19 @@ module.exports = function(sails) {

// Go through each of this models' connections-- if it is a string (i.e. named connection),
// dereference it to get the complete connection config object
modelDef.connections = util.map( modelDef.connections, _normalizeConnection);
modelDef.connections = util.map( modelDef.connections, function (connection) {

// If `connection` is not an object yet, try to look-up connection by name
if ( util.isString(connection) ) {
return _lookupConnection(connection);
}
// `connection` defined inline in model-- just need to normalize it
if ( util.isObject(connection) ) {
return _normalizeConnection(connection);
}
// Invalid connection found, throw fatal error.
return FatalError.__InvalidConnection__ (connection, modelDef.identity);
});

// Save modified model definition back to sails.models
sails.models[mid] = modelDef;
Expand Down Expand Up @@ -176,43 +192,48 @@ module.exports = function(sails) {


/**
* Lookup/normalize one of a model's connections
* Lookup a connection
*
* @api private
*/

function _normalizeConnection (connection) {

// This is already a connection config object-- use it
if ( util.isObject(connection) ) {
return connection;
}
function _lookupConnection (connection) {
var connectionID = connection;
connection = sails.config.connections[connectionID];

// Try to look-up connection by name
if ( util.isString(connection) ) {
var connectionID = connection;
connection = sails.config.connections[connectionID];
// If this is not a known connection
if (!connection) {
throw new Error('Adapters not allowed to be referenced directly in models for now ahh its complicated');

// If this is not a known connection
if (!connection) {
throw new Error('Adapters not allowed to be referenced directly in models for now ahh its complicated');
// If nothing makes sense, throw a fatal error
return FatalError.__UnknownConnection__ (connection, modelDef.identity);
// Try it out as the name of an adapter
// var adapterModuleName = connectionID;

// Try it out as the name of an adapter
// var adapterModuleName = connectionID;
// // Search `sails.adapters` as well as node_modules
// if (sails.adapters[adapterModuleName]) {}

// // Search `sails.adapters` as well as node_modules
// if (sails.adapters[adapterModuleName]) {}
// // TODO: FINISH THIS!!!

// // TODO: FINISH THIS!!!

// // If nothing makes sense, throw a fatal error
// return FatalError.__UnknownConnection__ (connection, modelDef.identity);
}
return connection;
}
return connection;
}

function _normalizeConnection (connection, modelID) {
// Connection is not formatted properly, throw a fatal error.
return FatalError.__InvalidConnection__ (connection, modelDef.identity);
if ( !util.isObject(connection) ) {
return FatalError.__InvalidConnection__ (connection, modelID);
}

// Backwards compatibilty for `connection.module`
if ( connection.module ) {
sails.log.verbose(
'Deprecation warning :: In model `' + modelID + '`\'s `connections` config, ' +
'replacing `module` with `adapter`....');
connection.adapter = connection.module;
delete connection.module;
}
return connection;
}

};

0 comments on commit dfbcfdb

Please sign in to comment.