2016-05-04 00:10:46 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-06-07 14:48:28 +00:00
|
|
|
var g = require('strong-globalize')();
|
2014-06-06 09:47:25 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
var extend = require('util')._extend;
|
2014-07-10 20:46:06 +00:00
|
|
|
var juggler = require('loopback-datasource-juggler');
|
2014-08-26 15:58:59 +00:00
|
|
|
var debug = require('debug')('loopback:registry');
|
2014-07-10 20:46:06 +00:00
|
|
|
var DataSource = juggler.DataSource;
|
|
|
|
var ModelBuilder = juggler.ModelBuilder;
|
2014-06-06 09:47:25 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
module.exports = Registry;
|
2014-06-06 09:47:25 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
/**
|
|
|
|
* Define and reference `Models` and `DataSources`.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
2014-06-06 09:47:25 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
function Registry() {
|
|
|
|
this.defaultDataSources = {};
|
|
|
|
this.modelBuilder = new ModelBuilder();
|
|
|
|
require('./model')(this);
|
|
|
|
require('./persisted-model')(this);
|
|
|
|
|
|
|
|
// Set the default model base class.
|
|
|
|
this.modelBuilder.defaultModelBaseClass = this.getModel('Model');
|
|
|
|
}
|
2014-07-10 20:46:06 +00:00
|
|
|
|
2014-06-06 09:47:25 +00:00
|
|
|
/**
|
|
|
|
* Create a named vanilla JavaScript class constructor with an attached
|
|
|
|
* set of properties and options.
|
|
|
|
*
|
2014-06-09 09:18:52 +00:00
|
|
|
* This function comes with two variants:
|
|
|
|
* * `loopback.createModel(name, properties, options)`
|
|
|
|
* * `loopback.createModel(config)`
|
|
|
|
*
|
|
|
|
* In the second variant, the parameters `name`, `properties` and `options`
|
|
|
|
* are provided in the config object. Any additional config entries are
|
|
|
|
* interpreted as `options`, i.e. the following two configs are identical:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* { name: 'Customer', base: 'User' }
|
|
|
|
* { name: 'Customer', options: { base: 'User' } }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* **Example**
|
|
|
|
*
|
|
|
|
* Create an `Author` model using the three-parameter variant:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* loopback.createModel(
|
|
|
|
* 'Author',
|
|
|
|
* {
|
|
|
|
* firstName: 'string',
|
2014-11-04 00:02:48 +00:00
|
|
|
* lastName: 'string'
|
2014-06-09 09:18:52 +00:00
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* relations: {
|
|
|
|
* books: {
|
|
|
|
* model: 'Book',
|
|
|
|
* type: 'hasAndBelongsToMany'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Create the same model using a config object:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* loopback.createModel({
|
|
|
|
* name: 'Author',
|
|
|
|
* properties: {
|
|
|
|
* firstName: 'string',
|
2014-11-04 00:02:48 +00:00
|
|
|
* lastName: 'string'
|
2014-06-09 09:18:52 +00:00
|
|
|
* },
|
|
|
|
* relations: {
|
|
|
|
* books: {
|
|
|
|
* model: 'Book',
|
|
|
|
* type: 'hasAndBelongsToMany'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
2014-06-06 09:47:25 +00:00
|
|
|
* @param {String} name Unique name.
|
|
|
|
* @param {Object} properties
|
|
|
|
* @param {Object} options (optional)
|
|
|
|
*
|
|
|
|
* @header loopback.createModel
|
|
|
|
*/
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.createModel = function(name, properties, options) {
|
|
|
|
|
2014-06-09 09:18:52 +00:00
|
|
|
if (arguments.length === 1 && typeof name === 'object') {
|
|
|
|
var config = name;
|
|
|
|
name = config.name;
|
|
|
|
properties = config.properties;
|
|
|
|
options = buildModelOptionsFromConfig(config);
|
|
|
|
|
|
|
|
assert(typeof name === 'string',
|
|
|
|
'The model-config property `name` must be a string');
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:47:25 +00:00
|
|
|
options = options || {};
|
|
|
|
var BaseModel = options.base || options.super;
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (typeof BaseModel === 'string') {
|
2014-06-06 09:47:25 +00:00
|
|
|
var baseName = BaseModel;
|
|
|
|
BaseModel = this.getModel(BaseModel);
|
|
|
|
|
|
|
|
if (BaseModel === undefined) {
|
|
|
|
if (baseName === 'DataModel') {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('Model `%s` is extending deprecated `DataModel. ' +
|
2015-08-13 23:49:27 +00:00
|
|
|
'Use `PersistedModel` instead.', name);
|
|
|
|
BaseModel = this.getModel('PersistedModel');
|
2014-06-06 09:47:25 +00:00
|
|
|
} else {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('Model `%s` is extending an unknown model `%s`. ' +
|
2014-06-06 09:47:25 +00:00
|
|
|
'Using `PersistedModel` as the base.', name, baseName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
BaseModel = BaseModel || this.getModel('PersistedModel');
|
2014-06-06 09:47:25 +00:00
|
|
|
var model = BaseModel.extend(name, properties, options);
|
2015-04-01 21:50:36 +00:00
|
|
|
model.registry = this;
|
2014-06-06 09:47:25 +00:00
|
|
|
|
2015-05-06 08:04:06 +00:00
|
|
|
this._defineRemoteMethods(model, options.methods);
|
|
|
|
|
2014-06-06 09:47:25 +00:00
|
|
|
// try to attach
|
|
|
|
try {
|
|
|
|
this.autoAttachModel(model);
|
2014-10-30 20:49:47 +00:00
|
|
|
} catch (e) {}
|
2014-06-06 09:47:25 +00:00
|
|
|
|
|
|
|
return model;
|
|
|
|
};
|
|
|
|
|
|
|
|
function buildModelOptionsFromConfig(config) {
|
|
|
|
var options = extend({}, config.options);
|
|
|
|
for (var key in config) {
|
|
|
|
if (['name', 'properties', 'options'].indexOf(key) !== -1) {
|
|
|
|
// Skip items which have special meaning
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options[key] !== undefined) {
|
|
|
|
// When both `config.key` and `config.options.key` are set,
|
|
|
|
// use the latter one
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
options[key] = config[key];
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2014-11-07 19:14:40 +00:00
|
|
|
/*
|
|
|
|
* Add the acl entry to the acls
|
|
|
|
* @param {Object[]} acls
|
|
|
|
* @param {Object} acl
|
|
|
|
*/
|
|
|
|
function addACL(acls, acl) {
|
|
|
|
for (var i = 0, n = acls.length; i < n; i++) {
|
|
|
|
// Check if there is a matching acl to be overriden
|
|
|
|
if (acls[i].property === acl.property &&
|
|
|
|
acls[i].accessType === acl.accessType &&
|
|
|
|
acls[i].principalType === acl.principalType &&
|
|
|
|
acls[i].principalId === acl.principalId) {
|
|
|
|
acls[i] = acl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acls.push(acl);
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:47:25 +00:00
|
|
|
/**
|
|
|
|
* Alter an existing Model class.
|
|
|
|
* @param {Model} ModelCtor The model constructor to alter.
|
2014-06-09 05:46:32 +00:00
|
|
|
* @options {Object} config Additional configuration to apply
|
2014-06-06 09:47:25 +00:00
|
|
|
* @property {DataSource} dataSource Attach the model to a dataSource.
|
|
|
|
* @property {Object} [relations] Model relations to add/update.
|
|
|
|
*
|
|
|
|
* @header loopback.configureModel(ModelCtor, config)
|
|
|
|
*/
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.configureModel = function(ModelCtor, config) {
|
2014-06-06 09:47:25 +00:00
|
|
|
var settings = ModelCtor.settings;
|
2014-11-07 19:14:40 +00:00
|
|
|
var modelName = ModelCtor.modelName;
|
2014-06-06 09:47:25 +00:00
|
|
|
|
2014-11-07 19:14:40 +00:00
|
|
|
// Relations
|
|
|
|
if (typeof config.relations === 'object' && config.relations !== null) {
|
2014-06-06 09:47:25 +00:00
|
|
|
var relations = settings.relations = settings.relations || {};
|
|
|
|
Object.keys(config.relations).forEach(function(key) {
|
2014-11-07 19:14:40 +00:00
|
|
|
// FIXME: [rfeng] We probably should check if the relation exists
|
2014-06-06 09:47:25 +00:00
|
|
|
relations[key] = extend(relations[key] || {}, config.relations[key]);
|
|
|
|
});
|
2014-11-07 19:14:40 +00:00
|
|
|
} else if (config.relations != null) {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('The relations property of `%s` configuration ' +
|
2014-11-07 19:14:40 +00:00
|
|
|
'must be an object', modelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACLs
|
|
|
|
if (Array.isArray(config.acls)) {
|
|
|
|
var acls = settings.acls = settings.acls || [];
|
|
|
|
config.acls.forEach(function(acl) {
|
|
|
|
addACL(acls, acl);
|
|
|
|
});
|
|
|
|
} else if (config.acls != null) {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('The acls property of `%s` configuration ' +
|
2014-11-07 19:14:40 +00:00
|
|
|
'must be an array of objects', modelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings
|
|
|
|
var excludedProperties = {
|
|
|
|
base: true,
|
|
|
|
'super': true,
|
|
|
|
relations: true,
|
|
|
|
acls: true,
|
|
|
|
dataSource: true
|
|
|
|
};
|
|
|
|
if (typeof config.options === 'object' && config.options !== null) {
|
|
|
|
for (var p in config.options) {
|
|
|
|
if (!(p in excludedProperties)) {
|
|
|
|
settings[p] = config.options[p];
|
|
|
|
} else {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('Property `%s` cannot be reconfigured for `%s`',
|
2014-11-07 19:14:40 +00:00
|
|
|
p, modelName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (config.options != null) {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('The options property of `%s` configuration ' +
|
2014-11-07 19:14:40 +00:00
|
|
|
'must be an object', modelName);
|
2014-06-06 09:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// It's important to attach the datasource after we have updated
|
|
|
|
// configuration, so that the datasource picks up updated relations
|
|
|
|
if (config.dataSource) {
|
|
|
|
assert(config.dataSource instanceof DataSource,
|
2016-08-12 11:33:14 +00:00
|
|
|
'Cannot configure ' + ModelCtor.modelName +
|
|
|
|
': config.dataSource must be an instance of DataSource');
|
2014-06-06 09:47:25 +00:00
|
|
|
ModelCtor.attachTo(config.dataSource);
|
2014-08-26 15:58:59 +00:00
|
|
|
debug('Attached model `%s` to dataSource `%s`',
|
2014-11-07 19:14:40 +00:00
|
|
|
modelName, config.dataSource.name);
|
2015-04-24 15:32:17 +00:00
|
|
|
} else if (config.dataSource === null || config.dataSource === false) {
|
2014-08-26 15:58:59 +00:00
|
|
|
debug('Model `%s` is not attached to any DataSource by configuration.',
|
2014-11-07 19:14:40 +00:00
|
|
|
modelName);
|
2014-08-26 15:58:59 +00:00
|
|
|
} else {
|
|
|
|
debug('Model `%s` is not attached to any DataSource, possibly by a mistake.',
|
2014-11-07 19:14:40 +00:00
|
|
|
modelName);
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn(
|
|
|
|
'The configuration of `%s` is missing {{`dataSource`}} property.\n' +
|
2014-08-26 15:58:59 +00:00
|
|
|
'Use `null` or `false` to mark models not attached to any data source.',
|
2014-11-07 19:14:40 +00:00
|
|
|
modelName);
|
2014-06-06 09:47:25 +00:00
|
|
|
}
|
2015-05-06 08:04:06 +00:00
|
|
|
|
|
|
|
// Remote methods
|
|
|
|
this._defineRemoteMethods(ModelCtor, config.methods);
|
|
|
|
};
|
|
|
|
|
|
|
|
Registry.prototype._defineRemoteMethods = function(ModelCtor, methods) {
|
|
|
|
if (!methods) return;
|
|
|
|
if (typeof methods !== 'object') {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('Ignoring non-object "methods" setting of "%s".',
|
2015-05-06 08:04:06 +00:00
|
|
|
ModelCtor.modelName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(methods).forEach(function(key) {
|
|
|
|
var meta = methods[key];
|
|
|
|
if (typeof meta.isStatic !== 'boolean') {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('Remoting metadata for "%s.%s" is missing "isStatic" ' +
|
2015-05-06 08:04:06 +00:00
|
|
|
'flag, the method is registered as an instance method.',
|
|
|
|
ModelCtor.modelName,
|
|
|
|
key);
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('This behaviour may change in the next major version.');
|
2015-05-06 08:04:06 +00:00
|
|
|
}
|
|
|
|
ModelCtor.remoteMethod(key, meta);
|
|
|
|
});
|
2014-06-06 09:47:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up a model class by name from all models created by
|
|
|
|
* `loopback.createModel()`
|
2015-04-01 21:50:36 +00:00
|
|
|
* @param {String|Function} modelOrName The model name or a `Model` constructor.
|
2014-06-06 09:47:25 +00:00
|
|
|
* @returns {Model} The model class
|
|
|
|
*
|
2014-06-16 14:13:24 +00:00
|
|
|
* @header loopback.findModel(modelName)
|
|
|
|
*/
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.findModel = function(modelName) {
|
2015-11-09 15:01:43 +00:00
|
|
|
if (typeof modelName === 'function') return modelName;
|
2014-07-16 16:09:07 +00:00
|
|
|
return this.modelBuilder.models[modelName];
|
2014-06-16 14:13:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up a model class by name from all models created by
|
2015-04-01 21:50:36 +00:00
|
|
|
* `loopback.createModel()`. **Throw an error when no such model exists.**
|
2014-06-16 14:13:24 +00:00
|
|
|
*
|
2015-04-01 21:50:36 +00:00
|
|
|
* @param {String} modelOrName The model name or a `Model` constructor.
|
2014-06-16 14:13:24 +00:00
|
|
|
* @returns {Model} The model class
|
|
|
|
*
|
2014-06-06 09:47:25 +00:00
|
|
|
* @header loopback.getModel(modelName)
|
|
|
|
*/
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.getModel = function(modelName) {
|
2014-06-16 14:13:24 +00:00
|
|
|
var model = this.findModel(modelName);
|
|
|
|
if (model) return model;
|
|
|
|
|
2016-06-07 14:48:28 +00:00
|
|
|
throw new Error(g.f('Model not found: %s', modelName));
|
2014-06-06 09:47:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up a model class by the base model class.
|
|
|
|
* The method can be used by LoopBack
|
|
|
|
* to find configured models in models.json over the base model.
|
|
|
|
* @param {Model} modelType The base model class
|
|
|
|
* @returns {Model} The subclass if found or the base class
|
|
|
|
*
|
|
|
|
* @header loopback.getModelByType(modelType)
|
|
|
|
*/
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.getModelByType = function(modelType) {
|
|
|
|
var type = typeof modelType;
|
|
|
|
var accepted = ['function', 'string'];
|
|
|
|
|
|
|
|
assert(accepted.indexOf(type) > -1,
|
|
|
|
'The model type must be a constructor or model name');
|
|
|
|
|
|
|
|
if (type === 'string') {
|
|
|
|
modelType = this.getModel(modelType);
|
|
|
|
}
|
|
|
|
|
2014-07-10 20:46:06 +00:00
|
|
|
var models = this.modelBuilder.models;
|
2014-10-30 20:49:47 +00:00
|
|
|
for (var m in models) {
|
|
|
|
if (models[m].prototype instanceof modelType) {
|
2014-06-06 09:47:25 +00:00
|
|
|
return models[m];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modelType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a data source with passing the provided options to the connector.
|
|
|
|
*
|
|
|
|
* @param {String} name Optional name.
|
2014-06-09 05:46:32 +00:00
|
|
|
* @options {Object} options Data Source options
|
2014-06-06 09:47:25 +00:00
|
|
|
* @property {Object} connector LoopBack connector.
|
2014-06-09 05:46:32 +00:00
|
|
|
* @property {*} [*] Other connector properties.
|
|
|
|
* See the relevant connector documentation.
|
2014-06-06 09:47:25 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.createDataSource = function(name, options) {
|
2014-07-10 20:46:06 +00:00
|
|
|
var self = this;
|
2015-04-01 21:50:36 +00:00
|
|
|
|
2014-07-10 20:46:06 +00:00
|
|
|
var ds = new DataSource(name, options, self.modelBuilder);
|
2014-10-30 20:49:47 +00:00
|
|
|
ds.createModel = function(name, properties, settings) {
|
2014-07-20 18:52:12 +00:00
|
|
|
settings = settings || {};
|
|
|
|
var BaseModel = settings.base || settings.super;
|
|
|
|
if (!BaseModel) {
|
|
|
|
// Check the connector types
|
2014-07-27 07:41:43 +00:00
|
|
|
var connectorTypes = ds.getTypes();
|
2014-07-20 18:52:12 +00:00
|
|
|
if (Array.isArray(connectorTypes) && connectorTypes.indexOf('db') !== -1) {
|
|
|
|
// Only set up the base model to PersistedModel if the connector is DB
|
|
|
|
BaseModel = self.PersistedModel;
|
|
|
|
} else {
|
|
|
|
BaseModel = self.Model;
|
|
|
|
}
|
|
|
|
settings.base = BaseModel;
|
|
|
|
}
|
2014-07-10 20:46:06 +00:00
|
|
|
var ModelCtor = self.createModel(name, properties, settings);
|
2014-06-06 09:47:25 +00:00
|
|
|
ModelCtor.attachTo(ds);
|
|
|
|
return ModelCtor;
|
|
|
|
};
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (ds.settings && ds.settings.defaultForType) {
|
2014-06-06 09:47:25 +00:00
|
|
|
this.setDefaultDataSourceForType(ds.settings.defaultForType, ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ds;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an in-memory data source. Use one if it already exists.
|
|
|
|
*
|
|
|
|
* @param {String} [name] The name of the data source.
|
|
|
|
* If not provided, the `'default'` is used.
|
|
|
|
*/
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.memory = function(name) {
|
2014-06-06 09:47:25 +00:00
|
|
|
name = name || 'default';
|
|
|
|
var memory = (
|
|
|
|
this._memoryDataSources || (this._memoryDataSources = {})
|
|
|
|
)[name];
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (!memory) {
|
2014-06-06 09:47:25 +00:00
|
|
|
memory = this._memoryDataSources[name] = this.createDataSource({
|
2014-06-25 19:37:30 +00:00
|
|
|
connector: 'memory'
|
2014-06-06 09:47:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return memory;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default `dataSource` for a given `type`.
|
2014-10-15 07:07:30 +00:00
|
|
|
* @param {String} type The datasource type.
|
2014-06-06 09:47:25 +00:00
|
|
|
* @param {Object|DataSource} dataSource The data source settings or instance
|
2014-10-15 07:07:30 +00:00
|
|
|
* @returns {DataSource} The data source instance.
|
2014-06-06 09:47:25 +00:00
|
|
|
*
|
|
|
|
* @header loopback.setDefaultDataSourceForType(type, dataSource)
|
|
|
|
*/
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.setDefaultDataSourceForType = function(type, dataSource) {
|
2014-06-09 14:15:56 +00:00
|
|
|
var defaultDataSources = this.defaultDataSources;
|
2014-06-06 09:47:25 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (!(dataSource instanceof DataSource)) {
|
2014-06-06 09:47:25 +00:00
|
|
|
dataSource = this.createDataSource(dataSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultDataSources[type] = dataSource;
|
|
|
|
return dataSource;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default `dataSource` for a given `type`.
|
2014-10-15 07:07:30 +00:00
|
|
|
* @param {String} type The datasource type.
|
2015-04-01 21:50:36 +00:00
|
|
|
* @returns {DataSource} The data source instance
|
2014-06-06 09:47:25 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.getDefaultDataSourceForType = function(type) {
|
2014-06-06 09:47:25 +00:00
|
|
|
return this.defaultDataSources && this.defaultDataSources[type];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach any model that does not have a dataSource to
|
|
|
|
* the default dataSource for the type the Model requests
|
|
|
|
*/
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.autoAttach = function() {
|
2014-07-10 20:46:06 +00:00
|
|
|
var models = this.modelBuilder.models;
|
2014-06-06 09:47:25 +00:00
|
|
|
assert.equal(typeof models, 'object', 'Cannot autoAttach without a models object');
|
|
|
|
|
|
|
|
Object.keys(models).forEach(function(modelName) {
|
|
|
|
var ModelCtor = models[modelName];
|
|
|
|
|
|
|
|
// Only auto attach if the model doesn't have an explicit data source
|
2014-10-30 20:49:47 +00:00
|
|
|
if (ModelCtor && (!(ModelCtor.dataSource instanceof DataSource))) {
|
2014-06-06 09:47:25 +00:00
|
|
|
this.autoAttachModel(ModelCtor);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
};
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Registry.prototype.autoAttachModel = function(ModelCtor) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (ModelCtor.autoAttach) {
|
2014-06-06 09:47:25 +00:00
|
|
|
var ds = this.getDefaultDataSourceForType(ModelCtor.autoAttach);
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
assert(
|
|
|
|
ds instanceof DataSource,
|
|
|
|
'cannot autoAttach model "' + ModelCtor.modelName +
|
|
|
|
'". No dataSource found of type ' + ModelCtor.autoAttach
|
|
|
|
);
|
2014-06-06 09:47:25 +00:00
|
|
|
|
|
|
|
ModelCtor.attachTo(ds);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// temporary alias to simplify migration of code based on <=2.0.0-beta3
|
2015-04-01 21:50:36 +00:00
|
|
|
Object.defineProperty(Registry.prototype, 'DataModel', {
|
2014-06-06 09:47:25 +00:00
|
|
|
get: function() {
|
|
|
|
var stackLines = new Error().stack.split('\n');
|
|
|
|
console.warn('loopback.DataModel is deprecated, ' +
|
|
|
|
'use loopback.PersistedModel instead.');
|
|
|
|
// Log the location where loopback.DataModel was called
|
|
|
|
console.warn(stackLines[2]);
|
|
|
|
return this.PersistedModel;
|
|
|
|
}
|
|
|
|
});
|