/** * Normalize properties of a 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 */ function _normalizeConnection (connection, modelID) { // Connection is not formatted properly, throw a fatal error. if ( !util.isObject(connection) ) { return Err.fatal.__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; } // Adapter is required for a connection if ( !connection.adapter ) { // Invalid connection found, throw fatal error. return Err.fatal.__InvalidConnection__ (connection, 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[connection.adapter]) { // (Format adapter name to make sure we make the best attempt we can) var moduleName = connection.adapter; 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...'); var modulePath = sails.config.appPath + '/node_modules/' + moduleName; if ( !fs.existsSync (modulePath) ) { // If adapter doesn't exist, log an error and exit return Err.fatal.__UnknownAdapter__ (connection.adapter, modelID, sails.majorVersion, sails.minorVersion); } // Since the module seems to exist, try to require it (execute the code) try { sails.adapters[moduleName] = require(modulePath); } catch (err) { return Err.fatal.__InvalidAdapter__ (moduleName, err); } } // Defaults connection object to its adapter's defaults var desAdapters = sails.adapters[connection.adapter]; connection = util.merge({}, desAdapters.defaults, connection); // Success- connection normalized and validated // (any missing adapters were either acquired, or the loading process was stopped w/ a fatal error) return connection; }
/** * Lookup a connection (e.g., `{ adapter: 'sails-disk' }`) * by name (e.g., 'devDB') * * @param {String} connectionName * * @param {String} modelID * // Optional, improves quality of error messages * * @global sails * sails.config * sails.config.connections {} * * @throws {Err.fatal} __UnknownConnection__ * @api private */ function _lookupConnection (connectionName, modelID) { var connection = sails.config.connections[connectionName]; // If this is not a known connection, throw a fatal error. if (!connection) { return Err.fatal.__UnknownConnection__ (connectionName, modelID); } return connection; }
modelDef.connections = util.map( modelDef.connections, function (connection) { if ( util.isString(connection) ) { connection = _lookupConnection(connection, modelID); connection = _normalizeConnection(connection, modelID); return connection; } if ( util.isObject(connection) ) { return _normalizeConnection(connection, modelID); } return Err.fatal.__InvalidConnection__ (connection, modelDef.identity); });
lookupFn: function (policyId, referencedIn) { // Case-insensitive policyId = policyId.toLowerCase(); // Policy doesn't exist if ( !this.middleware[policyId] ) { return Err.fatal.__UnknownPolicy__ (policyId, referencedIn, sails.config.paths.policies); } // Policy found return this.middleware[policyId]; }