2016-05-03 22:50:21 +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
|
|
|
|
|
2013-12-20 01:49:47 +00:00
|
|
|
/*!
|
2013-07-15 18:32:00 +00:00
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
2016-06-07 14:48:28 +00:00
|
|
|
|
2016-09-16 19:31:48 +00:00
|
|
|
var g = require('./globalize');
|
2013-12-10 23:57:55 +00:00
|
|
|
var assert = require('assert');
|
2014-08-28 18:56:43 +00:00
|
|
|
var RemoteObjects = require('strong-remoting');
|
2014-05-19 22:56:26 +00:00
|
|
|
var SharedClass = require('strong-remoting').SharedClass;
|
2014-08-04 16:27:50 +00:00
|
|
|
var extend = require('util')._extend;
|
2016-08-15 09:06:05 +00:00
|
|
|
var format = require('util').format;
|
2013-07-15 18:32:00 +00:00
|
|
|
|
2016-09-19 16:52:34 +00:00
|
|
|
var deprecated = require('depd')('loopback');
|
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
module.exports = function(registry) {
|
|
|
|
/**
|
|
|
|
* The base class for **all models**.
|
|
|
|
*
|
|
|
|
* **Inheriting from `Model`**
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* var properties = {...};
|
|
|
|
* var options = {...};
|
|
|
|
* var MyModel = loopback.Model.extend('MyModel', properties, options);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* **Options**
|
|
|
|
*
|
|
|
|
* - `trackChanges` - If true, changes to the model will be tracked. **Required
|
|
|
|
* for replication.**
|
|
|
|
*
|
|
|
|
* **Events**
|
|
|
|
*
|
|
|
|
* #### Event: `changed`
|
|
|
|
*
|
|
|
|
* Emitted after a model has been successfully created, saved, or updated.
|
|
|
|
* Argument: `inst`, model instance, object
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* MyModel.on('changed', function(inst) {
|
|
|
|
* console.log('model with id %s has been changed', inst.id);
|
|
|
|
* // => model with id 1 has been changed
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* #### Event: `deleted`
|
|
|
|
*
|
|
|
|
* Emitted after an individual model has been deleted.
|
|
|
|
* Argument: `id`, model ID (number).
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* MyModel.on('deleted', function(id) {
|
|
|
|
* console.log('model with id %s has been deleted', id);
|
|
|
|
* // => model with id 1 has been deleted
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* #### Event: `deletedAll`
|
|
|
|
*
|
|
|
|
* Emitted after an individual model has been deleted.
|
|
|
|
* Argument: `where` (optional), where filter, JSON object.
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* MyModel.on('deletedAll', function(where) {
|
|
|
|
* if (where) {
|
|
|
|
* console.log('all models where ', where, ' have been deleted');
|
|
|
|
* // => all models where
|
|
|
|
* // => {price: {gt: 100}}
|
|
|
|
* // => have been deleted
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* #### Event: `attached`
|
|
|
|
*
|
|
|
|
* Emitted after a `Model` has been attached to an `app`.
|
|
|
|
*
|
|
|
|
* #### Event: `dataSourceAttached`
|
|
|
|
*
|
|
|
|
* Emitted after a `Model` has been attached to a `DataSource`.
|
|
|
|
*
|
|
|
|
* #### Event: set
|
|
|
|
*
|
|
|
|
* Emitted when model property is set.
|
|
|
|
* Argument: `inst`, model instance, object
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* MyModel.on('set', function(inst) {
|
|
|
|
* console.log('model with id %s has been changed', inst.id);
|
|
|
|
* // => model with id 1 has been changed
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
|
|
|
* @property {String} Model.modelName The name of the model. Static property.
|
|
|
|
* @property {DataSource} Model.dataSource Data source to which the model is connected, if any. Static property.
|
|
|
|
* @property {SharedClass} Model.sharedMethod The `strong-remoting` [SharedClass](http://apidocs.strongloop.com/strong-remoting/#sharedclass) that contains remoting (and http) metadata. Static property.
|
|
|
|
* @property {Object} settings Contains additional model settings.
|
|
|
|
* @property {string} settings.http.path Base URL of the model HTTP route.
|
|
|
|
* @property [{string}] settings.acls Array of ACLs for the model.
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
var Model = registry.modelBuilder.define('Model');
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
Model.registry = registry;
|
|
|
|
|
2015-04-28 17:06:14 +00:00
|
|
|
/**
|
|
|
|
* The `loopback.Model.extend()` method calls this when you create a model that extends another model.
|
2015-05-04 16:19:13 +00:00
|
|
|
* Add any setup or configuration code you want executed when the model is created.
|
2015-04-28 17:06:14 +00:00
|
|
|
* See [Setting up a custom model](http://docs.strongloop.com/display/LB/Extending+built-in+models#Extendingbuilt-inmodels-Settingupacustommodel).
|
2015-04-03 07:26:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
Model.setup = function() {
|
|
|
|
var ModelCtor = this;
|
2015-04-01 21:50:36 +00:00
|
|
|
var Parent = this.super_;
|
|
|
|
|
|
|
|
if (!ModelCtor.registry && Parent && Parent.registry) {
|
|
|
|
ModelCtor.registry = Parent.registry;
|
|
|
|
}
|
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var options = this.settings;
|
|
|
|
var typeName = this.modelName;
|
|
|
|
|
|
|
|
var remotingOptions = {};
|
|
|
|
extend(remotingOptions, options.remoting || {});
|
|
|
|
|
|
|
|
// create a sharedClass
|
|
|
|
var sharedClass = ModelCtor.sharedClass = new SharedClass(
|
|
|
|
ModelCtor.modelName,
|
|
|
|
ModelCtor,
|
|
|
|
remotingOptions
|
|
|
|
);
|
|
|
|
|
|
|
|
// support remoting prototype methods
|
|
|
|
ModelCtor.sharedCtor = function(data, id, fn) {
|
|
|
|
var ModelCtor = this;
|
2013-07-15 18:32:00 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
if (typeof data === 'function') {
|
|
|
|
fn = data;
|
|
|
|
data = null;
|
|
|
|
id = null;
|
|
|
|
} else if (typeof id === 'function') {
|
|
|
|
fn = id;
|
2014-05-16 19:32:54 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
if (typeof data !== 'object') {
|
|
|
|
id = data;
|
|
|
|
data = null;
|
|
|
|
} else {
|
|
|
|
id = null;
|
|
|
|
}
|
|
|
|
}
|
2014-10-11 14:36:29 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
if (id && data) {
|
|
|
|
var model = new ModelCtor(data);
|
|
|
|
model.id = id;
|
|
|
|
fn(null, model);
|
|
|
|
} else if (data) {
|
|
|
|
fn(null, new ModelCtor(data));
|
|
|
|
} else if (id) {
|
|
|
|
ModelCtor.findById(id, function(err, model) {
|
|
|
|
if (err) {
|
|
|
|
fn(err);
|
|
|
|
} else if (model) {
|
|
|
|
fn(null, model);
|
|
|
|
} else {
|
2016-08-04 21:26:01 +00:00
|
|
|
err = new Error(g.f('could not find a model with {{id}} %s', id));
|
2015-04-03 07:26:19 +00:00
|
|
|
err.statusCode = 404;
|
|
|
|
err.code = 'MODEL_NOT_FOUND';
|
|
|
|
fn(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2016-08-04 21:26:01 +00:00
|
|
|
fn(new Error(g.f('must specify an {{id}} or {{data}}')));
|
2015-04-03 07:26:19 +00:00
|
|
|
}
|
|
|
|
};
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var idDesc = ModelCtor.modelName + ' id';
|
|
|
|
ModelCtor.sharedCtor.accepts = [
|
2016-04-01 09:14:26 +00:00
|
|
|
{ arg: 'id', type: 'any', required: true, http: { source: 'path' },
|
|
|
|
description: idDesc },
|
2015-04-03 07:26:19 +00:00
|
|
|
// {arg: 'instance', type: 'object', http: {source: 'body'}}
|
|
|
|
];
|
|
|
|
|
|
|
|
ModelCtor.sharedCtor.http = [
|
2016-04-01 09:14:26 +00:00
|
|
|
{ path: '/:id' },
|
2015-04-03 07:26:19 +00:00
|
|
|
];
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
ModelCtor.sharedCtor.returns = { root: true };
|
2015-04-03 07:26:19 +00:00
|
|
|
|
|
|
|
// before remote hook
|
|
|
|
ModelCtor.beforeRemote = function(name, fn) {
|
2015-04-03 08:04:05 +00:00
|
|
|
var className = this.modelName;
|
|
|
|
this._runWhenAttachedToApp(function(app) {
|
|
|
|
var remotes = app.remotes();
|
2015-04-03 07:26:19 +00:00
|
|
|
remotes.before(className + '.' + name, function(ctx, next) {
|
2016-09-15 21:33:56 +00:00
|
|
|
return fn(ctx, ctx.result, next);
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2015-04-03 08:04:05 +00:00
|
|
|
});
|
2015-04-03 07:26:19 +00:00
|
|
|
};
|
2014-08-28 18:56:43 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
// after remote hook
|
|
|
|
ModelCtor.afterRemote = function(name, fn) {
|
2015-04-03 08:04:05 +00:00
|
|
|
var className = this.modelName;
|
|
|
|
this._runWhenAttachedToApp(function(app) {
|
|
|
|
var remotes = app.remotes();
|
2015-04-03 07:26:19 +00:00
|
|
|
remotes.after(className + '.' + name, function(ctx, next) {
|
2016-09-15 21:33:56 +00:00
|
|
|
return fn(ctx, ctx.result, next);
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2015-04-03 08:04:05 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-03 08:31:03 +00:00
|
|
|
ModelCtor.afterRemoteError = function(name, fn) {
|
|
|
|
var className = this.modelName;
|
|
|
|
this._runWhenAttachedToApp(function(app) {
|
|
|
|
var remotes = app.remotes();
|
|
|
|
remotes.afterError(className + '.' + name, fn);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-03 08:04:05 +00:00
|
|
|
ModelCtor._runWhenAttachedToApp = function(fn) {
|
|
|
|
if (this.app) return fn(this.app);
|
|
|
|
var self = this;
|
|
|
|
self.once('attached', function() {
|
|
|
|
fn(self.app);
|
|
|
|
});
|
2015-04-03 07:26:19 +00:00
|
|
|
};
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
// resolve relation functions
|
|
|
|
sharedClass.resolve(function resolver(define) {
|
|
|
|
var relations = ModelCtor.relations || {};
|
|
|
|
|
|
|
|
// get the relations
|
|
|
|
for (var relationName in relations) {
|
|
|
|
var relation = relations[relationName];
|
|
|
|
if (relation.type === 'belongsTo') {
|
|
|
|
ModelCtor.belongsToRemoting(relationName, relation, define);
|
|
|
|
} else if (
|
|
|
|
relation.type === 'hasOne' ||
|
|
|
|
relation.type === 'embedsOne'
|
|
|
|
) {
|
|
|
|
ModelCtor.hasOneRemoting(relationName, relation, define);
|
|
|
|
} else if (
|
|
|
|
relation.type === 'hasMany' ||
|
|
|
|
relation.type === 'embedsMany' ||
|
|
|
|
relation.type === 'referencesMany') {
|
|
|
|
ModelCtor.hasManyRemoting(relationName, relation, define);
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
// handle scopes
|
|
|
|
var scopes = ModelCtor.scopes || {};
|
|
|
|
for (var scopeName in scopes) {
|
|
|
|
ModelCtor.scopeRemoting(scopeName, scopes[scopeName], define);
|
2013-07-16 02:45:27 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return ModelCtor;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Get the reference to ACL in a lazy fashion to avoid race condition in require
|
|
|
|
*/
|
|
|
|
var _aclModel = null;
|
|
|
|
Model._ACL = function getACL(ACL) {
|
2015-04-01 21:50:36 +00:00
|
|
|
var registry = this.registry;
|
2015-04-03 07:26:19 +00:00
|
|
|
if (ACL !== undefined) {
|
|
|
|
// The function is used as a setter
|
|
|
|
_aclModel = ACL;
|
|
|
|
}
|
|
|
|
if (_aclModel) {
|
|
|
|
return _aclModel;
|
2013-07-16 02:45:27 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
var aclModel = registry.getModel('ACL');
|
|
|
|
_aclModel = registry.getModelByType(aclModel);
|
|
|
|
return _aclModel;
|
|
|
|
};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
/**
|
|
|
|
* Check if the given access token can invoke the specified method.
|
|
|
|
*
|
|
|
|
* @param {AccessToken} token The access token.
|
|
|
|
* @param {*} modelId The model ID.
|
|
|
|
* @param {SharedMethod} sharedMethod The method in question.
|
|
|
|
* @param {Object} ctx The remote invocation context.
|
|
|
|
* @callback {Function} callback The callback function.
|
|
|
|
* @param {String|Error} err The error object.
|
|
|
|
* @param {Boolean} allowed True if the request is allowed; false otherwise.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model.checkAccess = function(token, modelId, sharedMethod, ctx, callback) {
|
|
|
|
var ANONYMOUS = registry.getModel('AccessToken').ANONYMOUS;
|
|
|
|
token = token || ANONYMOUS;
|
|
|
|
var aclModel = Model._ACL();
|
|
|
|
|
|
|
|
ctx = ctx || {};
|
|
|
|
if (typeof ctx === 'function' && callback === undefined) {
|
|
|
|
callback = ctx;
|
|
|
|
ctx = {};
|
2013-07-16 02:45:27 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
|
|
|
|
aclModel.checkAccessForContext({
|
|
|
|
accessToken: token,
|
|
|
|
model: this,
|
|
|
|
property: sharedMethod.name,
|
|
|
|
method: sharedMethod.name,
|
|
|
|
sharedMethod: sharedMethod,
|
|
|
|
modelId: modelId,
|
|
|
|
accessType: this._getAccessTypeForMethod(sharedMethod),
|
2016-04-01 09:14:26 +00:00
|
|
|
remotingContext: ctx,
|
2015-04-03 07:26:19 +00:00
|
|
|
}, function(err, accessRequest) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
callback(null, accessRequest.isAllowed());
|
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-07-16 02:45:27 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
/*!
|
|
|
|
* Determine the access type for the given `RemoteMethod`.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
* @param {RemoteMethod} method
|
|
|
|
*/
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
Model._getAccessTypeForMethod = function(method) {
|
|
|
|
if (typeof method === 'string') {
|
2016-04-01 09:14:26 +00:00
|
|
|
method = { name: method };
|
2015-04-03 07:26:19 +00:00
|
|
|
}
|
|
|
|
assert(
|
|
|
|
typeof method === 'object',
|
|
|
|
'method is a required argument and must be a RemoteMethod object'
|
|
|
|
);
|
|
|
|
|
|
|
|
var ACL = Model._ACL();
|
|
|
|
|
|
|
|
// Check the explicit setting of accessType
|
|
|
|
if (method.accessType) {
|
|
|
|
assert(method.accessType === ACL.READ ||
|
2015-04-03 14:41:32 +00:00
|
|
|
method.accessType === ACL.REPLICATE ||
|
2015-04-03 07:26:19 +00:00
|
|
|
method.accessType === ACL.WRITE ||
|
|
|
|
method.accessType === ACL.EXECUTE, 'invalid accessType ' +
|
|
|
|
method.accessType +
|
2015-04-03 14:41:32 +00:00
|
|
|
'. It must be "READ", "REPLICATE", "WRITE", or "EXECUTE"');
|
2015-04-03 07:26:19 +00:00
|
|
|
return method.accessType;
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
// Default GET requests to READ
|
|
|
|
var verb = method.http && method.http.verb;
|
|
|
|
if (typeof verb === 'string') {
|
|
|
|
verb = verb.toUpperCase();
|
|
|
|
}
|
|
|
|
if (verb === 'GET' || verb === 'HEAD') {
|
|
|
|
return ACL.READ;
|
|
|
|
}
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
switch (method.name) {
|
2016-04-01 09:14:26 +00:00
|
|
|
case 'create':
|
2015-04-03 07:26:19 +00:00
|
|
|
return ACL.WRITE;
|
|
|
|
case 'updateOrCreate':
|
|
|
|
return ACL.WRITE;
|
2016-09-02 12:40:47 +00:00
|
|
|
case 'upsertWithWhere':
|
|
|
|
return ACL.WRITE;
|
2015-04-03 07:26:19 +00:00
|
|
|
case 'upsert':
|
|
|
|
return ACL.WRITE;
|
|
|
|
case 'exists':
|
|
|
|
return ACL.READ;
|
|
|
|
case 'findById':
|
|
|
|
return ACL.READ;
|
|
|
|
case 'find':
|
|
|
|
return ACL.READ;
|
|
|
|
case 'findOne':
|
|
|
|
return ACL.READ;
|
|
|
|
case 'destroyById':
|
|
|
|
return ACL.WRITE;
|
|
|
|
case 'deleteById':
|
|
|
|
return ACL.WRITE;
|
|
|
|
case 'removeById':
|
|
|
|
return ACL.WRITE;
|
|
|
|
case 'count':
|
|
|
|
return ACL.READ;
|
|
|
|
default:
|
|
|
|
return ACL.EXECUTE;
|
2013-07-16 02:45:27 +00:00
|
|
|
}
|
2013-11-20 21:31:30 +00:00
|
|
|
};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
/**
|
|
|
|
* Get the `Application` object to which the Model is attached.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback Callback function called with `(err, app)` arguments.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Application} app Attached application object.
|
|
|
|
* @end
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model.getApp = function(callback) {
|
2015-04-28 10:46:02 +00:00
|
|
|
var self = this;
|
|
|
|
self._runWhenAttachedToApp(function(app) {
|
|
|
|
assert(self.app);
|
|
|
|
assert.equal(app, self.app);
|
|
|
|
callback(null, app);
|
2015-04-03 08:04:05 +00:00
|
|
|
});
|
2013-11-20 21:31:30 +00:00
|
|
|
};
|
2013-08-16 23:39:13 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
/**
|
2015-07-07 18:01:50 +00:00
|
|
|
* Enable remote invocation for the specified method.
|
|
|
|
* See [Remote methods](http://docs.strongloop.com/display/LB/Remote+methods) for more information.
|
2015-04-03 07:26:19 +00:00
|
|
|
*
|
|
|
|
* Static method example:
|
|
|
|
* ```js
|
|
|
|
* Model.myMethod();
|
|
|
|
* Model.remoteMethod('myMethod');
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {String} name The name of the method.
|
2015-07-08 23:53:51 +00:00
|
|
|
* @param {Object} options The remoting options.
|
2015-07-07 18:01:50 +00:00
|
|
|
* See [Remote methods - Options](http://docs.strongloop.com/display/LB/Remote+methods#Remotemethods-Options).
|
2015-04-03 07:26:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
Model.remoteMethod = function(name, options) {
|
|
|
|
if (options.isStatic === undefined) {
|
2016-03-28 15:11:22 +00:00
|
|
|
var m = name.match(/^prototype\.(.*)$/);
|
|
|
|
options.isStatic = !m;
|
|
|
|
name = options.isStatic ? name : m[1];
|
2014-05-19 22:56:26 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
this.sharedClass.defineMethod(name, options);
|
|
|
|
};
|
2014-08-08 22:55:47 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
/**
|
|
|
|
* Disable remote invocation for the method with the given name.
|
|
|
|
*
|
|
|
|
* @param {String} name The name of the method.
|
|
|
|
* @param {Boolean} isStatic Is the method static (eg. `MyModel.myMethod`)? Pass
|
|
|
|
* `false` if the method defined on the prototype (eg.
|
|
|
|
* `MyModel.prototype.myMethod`).
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model.disableRemoteMethod = function(name, isStatic) {
|
2016-09-19 16:52:34 +00:00
|
|
|
deprecated('Model.disableRemoteMethod is deprecated. ' +
|
|
|
|
'Use Model.disableRemoteMethodByName instead.');
|
|
|
|
var key = this.sharedClass.getKeyFromMethodNameAndTarget(name, isStatic);
|
|
|
|
this.sharedClass.disableMethodByName(key);
|
|
|
|
this.emit('remoteMethodDisabled', this.sharedClass, key);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable remote invocation for the method with the given name.
|
|
|
|
*
|
|
|
|
* @param {String} name The name of the method (include "prototype." if the method is defined on the prototype).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model.disableRemoteMethodByName = function(name) {
|
|
|
|
this.sharedClass.disableMethodByName(name);
|
2016-04-27 11:15:24 +00:00
|
|
|
this.emit('remoteMethodDisabled', this.sharedClass, name);
|
2015-04-03 07:26:19 +00:00
|
|
|
};
|
2013-12-07 01:04:47 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
Model.belongsToRemoting = function(relationName, relation, define) {
|
|
|
|
var modelName = relation.modelTo && relation.modelTo.modelName;
|
|
|
|
modelName = modelName || 'PersistedModel';
|
|
|
|
var fn = this.prototype[relationName];
|
|
|
|
var pathName = (relation.options.http && relation.options.http.path) || relationName;
|
|
|
|
define('__get__' + relationName, {
|
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'get', path: '/' + pathName },
|
|
|
|
accepts: { arg: 'refresh', type: 'boolean', http: { source: 'query' }},
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'READ',
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Fetches belongsTo relation %s.', relationName),
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: relationName, type: modelName, root: true },
|
2015-04-03 07:26:19 +00:00
|
|
|
}, fn);
|
|
|
|
};
|
2014-12-12 16:50:17 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
function convertNullToNotFoundError(toModelName, ctx, cb) {
|
|
|
|
if (ctx.result !== null) return cb();
|
2014-12-12 16:50:17 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var fk = ctx.getArgByName('fk');
|
2016-06-07 14:48:28 +00:00
|
|
|
var msg = g.f('Unknown "%s" id "%s".', toModelName, fk);
|
2015-04-03 07:26:19 +00:00
|
|
|
var error = new Error(msg);
|
|
|
|
error.statusCode = error.status = 404;
|
|
|
|
error.code = 'MODEL_NOT_FOUND';
|
|
|
|
cb(error);
|
2013-12-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
Model.hasOneRemoting = function(relationName, relation, define) {
|
|
|
|
var pathName = (relation.options.http && relation.options.http.path) || relationName;
|
|
|
|
var toModelName = relation.modelTo.modelName;
|
2014-02-21 03:43:50 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
define('__get__' + relationName, {
|
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'get', path: '/' + pathName },
|
|
|
|
accepts: { arg: 'refresh', type: 'boolean', http: { source: 'query' }},
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Fetches hasOne relation %s.', relationName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'READ',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: relationName, type: relation.modelTo.modelName, root: true },
|
|
|
|
rest: { after: convertNullToNotFoundError.bind(null, toModelName) },
|
2014-02-21 03:43:50 +00:00
|
|
|
});
|
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
define('__create__' + relationName, {
|
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'post', path: '/' + pathName },
|
2016-09-02 12:40:11 +00:00
|
|
|
accepts: { arg: 'data', type: 'object', model: toModelName, http: { source: 'body' }},
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Creates a new instance in %s of this model.', relationName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'WRITE',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: 'data', type: toModelName, root: true },
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2014-09-29 20:05:31 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
define('__update__' + relationName, {
|
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'put', path: '/' + pathName },
|
2016-09-02 12:40:11 +00:00
|
|
|
accepts: { arg: 'data', type: 'object', model: toModelName, http: { source: 'body' }},
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Update %s of this model.', relationName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'WRITE',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: 'data', type: toModelName, root: true },
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
define('__destroy__' + relationName, {
|
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'delete', path: '/' + pathName },
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Deletes %s of this model.', relationName),
|
2016-04-01 09:14:26 +00:00
|
|
|
accessType: 'WRITE',
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
|
|
|
};
|
2014-08-01 09:25:28 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
Model.hasManyRemoting = function(relationName, relation, define) {
|
|
|
|
var pathName = (relation.options.http && relation.options.http.path) || relationName;
|
|
|
|
var toModelName = relation.modelTo.modelName;
|
2014-09-01 05:52:14 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var findByIdFunc = this.prototype['__findById__' + relationName];
|
|
|
|
define('__findById__' + relationName, {
|
2014-07-16 23:35:43 +00:00
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'get', path: '/' + pathName + '/:fk' },
|
|
|
|
accepts: { arg: 'fk', type: 'any',
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Foreign key for %s', relationName),
|
2016-06-07 14:48:28 +00:00
|
|
|
required: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { source: 'path' }},
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Find a related item by id for %s.', relationName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'READ',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: 'result', type: toModelName, root: true },
|
|
|
|
rest: { after: convertNullToNotFoundError.bind(null, toModelName) },
|
2015-04-03 07:26:19 +00:00
|
|
|
}, findByIdFunc);
|
2014-07-16 23:35:43 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var destroyByIdFunc = this.prototype['__destroyById__' + relationName];
|
|
|
|
define('__destroyById__' + relationName, {
|
2014-07-16 23:35:43 +00:00
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'delete', path: '/' + pathName + '/:fk' },
|
|
|
|
accepts: { arg: 'fk', type: 'any',
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Foreign key for %s', relationName),
|
2016-06-07 14:48:28 +00:00
|
|
|
required: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { source: 'path' }},
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Delete a related item by id for %s.', relationName),
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: [],
|
2015-04-03 07:26:19 +00:00
|
|
|
}, destroyByIdFunc);
|
2014-07-16 23:35:43 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var updateByIdFunc = this.prototype['__updateById__' + relationName];
|
|
|
|
define('__updateById__' + relationName, {
|
2014-07-16 23:35:43 +00:00
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'put', path: '/' + pathName + '/:fk' },
|
2015-04-03 07:26:19 +00:00
|
|
|
accepts: [
|
2016-04-01 09:14:26 +00:00
|
|
|
{ arg: 'fk', type: 'any',
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Foreign key for %s', relationName),
|
2016-06-07 14:48:28 +00:00
|
|
|
required: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { source: 'path' }},
|
2016-09-02 12:40:11 +00:00
|
|
|
{ arg: 'data', type: 'object', model: toModelName, http: { source: 'body' }},
|
2015-04-03 07:26:19 +00:00
|
|
|
],
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Update a related item by id for %s.', relationName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'WRITE',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: 'result', type: toModelName, root: true },
|
2015-04-03 07:26:19 +00:00
|
|
|
}, updateByIdFunc);
|
|
|
|
|
|
|
|
if (relation.modelThrough || relation.type === 'referencesMany') {
|
|
|
|
var modelThrough = relation.modelThrough || relation.modelTo;
|
|
|
|
|
|
|
|
var accepts = [];
|
|
|
|
if (relation.type === 'hasMany' && relation.modelThrough) {
|
|
|
|
// Restrict: only hasManyThrough relation can have additional properties
|
2016-09-02 12:40:11 +00:00
|
|
|
accepts.push({
|
|
|
|
arg: 'data', type: 'object', model: modelThrough.modelName,
|
|
|
|
http: { source: 'body' },
|
|
|
|
});
|
2014-07-28 16:37:50 +00:00
|
|
|
}
|
2014-07-16 23:35:43 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var addFunc = this.prototype['__link__' + relationName];
|
|
|
|
define('__link__' + relationName, {
|
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'put', path: '/' + pathName + '/rel/:fk' },
|
|
|
|
accepts: [{ arg: 'fk', type: 'any',
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Foreign key for %s', relationName),
|
2016-06-07 14:48:28 +00:00
|
|
|
required: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { source: 'path' }}].concat(accepts),
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Add a related item by id for %s.', relationName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'WRITE',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: relationName, type: modelThrough.modelName, root: true },
|
2015-04-03 07:26:19 +00:00
|
|
|
}, addFunc);
|
|
|
|
|
|
|
|
var removeFunc = this.prototype['__unlink__' + relationName];
|
|
|
|
define('__unlink__' + relationName, {
|
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'delete', path: '/' + pathName + '/rel/:fk' },
|
|
|
|
accepts: { arg: 'fk', type: 'any',
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Foreign key for %s', relationName),
|
2016-06-07 14:48:28 +00:00
|
|
|
required: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { source: 'path' }},
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Remove the %s relation to an item by id.', relationName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'WRITE',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: [],
|
2015-04-03 07:26:19 +00:00
|
|
|
}, removeFunc);
|
|
|
|
|
|
|
|
// FIXME: [rfeng] How to map a function with callback(err, true|false) to HEAD?
|
|
|
|
// true --> 200 and false --> 404?
|
|
|
|
var existsFunc = this.prototype['__exists__' + relationName];
|
|
|
|
define('__exists__' + relationName, {
|
|
|
|
isStatic: false,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'head', path: '/' + pathName + '/rel/:fk' },
|
|
|
|
accepts: { arg: 'fk', type: 'any',
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Foreign key for %s', relationName),
|
2016-06-07 14:48:28 +00:00
|
|
|
required: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { source: 'path' }},
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Check the existence of %s relation to an item by id.', relationName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'READ',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: 'exists', type: 'boolean', root: true },
|
2015-04-03 07:26:19 +00:00
|
|
|
rest: {
|
|
|
|
// After hook to map exists to 200/404 for HEAD
|
|
|
|
after: function(ctx, cb) {
|
|
|
|
if (ctx.result === false) {
|
|
|
|
var modelName = ctx.method.sharedClass.name;
|
|
|
|
var id = ctx.getArgByName('id');
|
2016-08-04 21:26:01 +00:00
|
|
|
var msg = g.f('Unknown "%s" {{id}} "%s".', modelName, id);
|
2015-04-03 07:26:19 +00:00
|
|
|
var error = new Error(msg);
|
|
|
|
error.statusCode = error.status = 404;
|
|
|
|
error.code = 'MODEL_NOT_FOUND';
|
|
|
|
cb(error);
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-03 07:26:19 +00:00
|
|
|
}, existsFunc);
|
|
|
|
}
|
|
|
|
};
|
2014-10-30 20:49:47 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
Model.scopeRemoting = function(scopeName, scope, define) {
|
|
|
|
var pathName =
|
|
|
|
(scope.options && scope.options.http && scope.options.http.path) || scopeName;
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var isStatic = scope.isStatic;
|
|
|
|
var toModelName = scope.modelTo.modelName;
|
2014-11-17 17:44:20 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
// https://github.com/strongloop/loopback/issues/811
|
|
|
|
// Check if the scope is for a hasMany relation
|
|
|
|
var relation = this.relations[scopeName];
|
|
|
|
if (relation && relation.modelTo) {
|
|
|
|
// For a relation with through model, the toModelName should be the one
|
|
|
|
// from the target model
|
|
|
|
toModelName = relation.modelTo.modelName;
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
define('__get__' + scopeName, {
|
|
|
|
isStatic: isStatic,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'get', path: '/' + pathName },
|
|
|
|
accepts: { arg: 'filter', type: 'object' },
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Queries %s of %s.', scopeName, this.modelName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'READ',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: scopeName, type: [toModelName], root: true },
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
define('__create__' + scopeName, {
|
|
|
|
isStatic: isStatic,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'post', path: '/' + pathName },
|
2016-09-02 12:40:11 +00:00
|
|
|
accepts: { arg: 'data', type: 'object', model: toModelName, http: { source: 'body' }},
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Creates a new instance in %s of this model.', scopeName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'WRITE',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: 'data', type: toModelName, root: true },
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
define('__delete__' + scopeName, {
|
|
|
|
isStatic: isStatic,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'delete', path: '/' + pathName },
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Deletes all %s of this model.', scopeName),
|
2016-04-01 09:14:26 +00:00
|
|
|
accessType: 'WRITE',
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
define('__count__' + scopeName, {
|
|
|
|
isStatic: isStatic,
|
2016-04-01 09:14:26 +00:00
|
|
|
http: { verb: 'get', path: '/' + pathName + '/count' },
|
2016-08-04 21:26:01 +00:00
|
|
|
accepts: { arg: 'where', type: 'object',
|
2016-08-15 09:06:05 +00:00
|
|
|
description: 'Criteria to match model instances' },
|
|
|
|
description: format('Counts %s of %s.', scopeName, this.modelName),
|
2015-04-03 07:26:19 +00:00
|
|
|
accessType: 'READ',
|
2016-04-01 09:14:26 +00:00
|
|
|
returns: { arg: 'count', type: 'number' },
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
|
|
|
};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-12-01 22:37:25 +00:00
|
|
|
/**
|
|
|
|
* Enabled deeply-nested queries of related models via REST API.
|
|
|
|
*
|
|
|
|
* @param {String} relationName Name of the nested relation.
|
|
|
|
* @options {Object} [options] It is optional. See below.
|
|
|
|
* @param {String} pathName The HTTP path (relative to the model) at which your remote method is exposed.
|
|
|
|
* @param {String} filterMethod The filter name.
|
|
|
|
* @param {String} paramName The argument name that the remote method accepts.
|
|
|
|
* @param {String} getterName The getter name.
|
|
|
|
* @param {Boolean} hooks Whether to inherit before/after hooks.
|
|
|
|
* @callback {Function} filterCallback The Optional filter function.
|
|
|
|
* @param {Object} SharedMethod object. See [here](https://apidocs.strongloop.com/strong-remoting/#sharedmethod).
|
|
|
|
* @param {Object} RelationDefinition object which includes relation `type`, `ModelConstructor` of `modelFrom`, `modelTo`, `keyFrom`, `keyTo` and more relation definitions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model.nestRemoting = function(relationName, options, filterCallback) {
|
|
|
|
if (typeof options === 'function' && !filterCallback) {
|
|
|
|
filterCallback = options;
|
2015-04-03 07:26:19 +00:00
|
|
|
options = {};
|
2014-11-14 07:33:29 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var regExp = /^__([^_]+)__([^_]+)$/;
|
|
|
|
var relation = this.relations[relationName];
|
|
|
|
if (relation && relation.modelTo && relation.modelTo.sharedClass) {
|
|
|
|
var self = this;
|
|
|
|
var sharedClass = this.sharedClass;
|
|
|
|
var sharedToClass = relation.modelTo.sharedClass;
|
|
|
|
var toModelName = relation.modelTo.modelName;
|
|
|
|
|
|
|
|
var pathName = options.pathName || relation.options.path || relationName;
|
|
|
|
var paramName = options.paramName || 'nk';
|
|
|
|
|
|
|
|
var http = [].concat(sharedToClass.http || [])[0];
|
2016-04-01 09:14:26 +00:00
|
|
|
var httpPath, acceptArgs;
|
2015-04-03 07:26:19 +00:00
|
|
|
|
|
|
|
if (relation.multiple) {
|
|
|
|
httpPath = pathName + '/:' + paramName;
|
|
|
|
acceptArgs = [
|
|
|
|
{
|
|
|
|
arg: paramName, type: 'any', http: { source: 'path' },
|
2016-08-15 09:06:05 +00:00
|
|
|
description: format('Foreign key for %s.', relation.name),
|
2016-04-01 09:14:26 +00:00
|
|
|
required: true,
|
|
|
|
},
|
2015-04-03 07:26:19 +00:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
httpPath = pathName;
|
|
|
|
acceptArgs = [];
|
2014-08-04 17:02:30 +00:00
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
if (httpPath[0] !== '/') {
|
|
|
|
httpPath = '/' + httpPath;
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
// A method should return the method name to use, if it is to be
|
|
|
|
// included as a nested method - a falsy return value will skip.
|
2015-12-01 22:37:25 +00:00
|
|
|
var filter = filterCallback || options.filterMethod || function(method, relation) {
|
2015-04-03 07:26:19 +00:00
|
|
|
var matches = method.name.match(regExp);
|
|
|
|
if (matches) {
|
|
|
|
return '__' + matches[1] + '__' + relation.name + '__' + matches[2];
|
2014-08-04 16:27:50 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
sharedToClass.methods().forEach(function(method) {
|
|
|
|
var methodName;
|
|
|
|
if (!method.isStatic && (methodName = filter(method, relation))) {
|
|
|
|
var prefix = relation.multiple ? '__findById__' : '__get__';
|
|
|
|
var getterName = options.getterName || (prefix + relationName);
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var getterFn = relation.modelFrom.prototype[getterName];
|
|
|
|
if (typeof getterFn !== 'function') {
|
2016-06-07 14:48:28 +00:00
|
|
|
throw new Error(g.f('Invalid remote method: `%s`', getterName));
|
2014-08-04 16:27:50 +00:00
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
var nestedFn = relation.modelTo.prototype[method.name];
|
|
|
|
if (typeof nestedFn !== 'function') {
|
2016-06-07 14:48:28 +00:00
|
|
|
throw new Error(g.f('Invalid remote method: `%s`', method.name));
|
2015-04-03 07:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var opts = {};
|
|
|
|
|
|
|
|
opts.accepts = acceptArgs.concat(method.accepts || []);
|
|
|
|
opts.returns = [].concat(method.returns || []);
|
|
|
|
opts.description = method.description;
|
|
|
|
opts.accessType = method.accessType;
|
|
|
|
opts.rest = extend({}, method.rest || {});
|
|
|
|
opts.rest.delegateTo = method;
|
|
|
|
|
|
|
|
opts.http = [];
|
|
|
|
var routes = [].concat(method.http || []);
|
|
|
|
routes.forEach(function(route) {
|
|
|
|
if (route.path) {
|
|
|
|
var copy = extend({}, route);
|
|
|
|
copy.path = httpPath + route.path;
|
|
|
|
opts.http.push(copy);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (relation.multiple) {
|
|
|
|
sharedClass.defineMethod(methodName, opts, function(fkId) {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
var last = args[args.length - 1];
|
|
|
|
var cb = typeof last === 'function' ? last : null;
|
|
|
|
this[getterName](fkId, function(err, inst) {
|
|
|
|
if (err && cb) return cb(err);
|
|
|
|
if (inst instanceof relation.modelTo) {
|
|
|
|
try {
|
|
|
|
nestedFn.apply(inst, args);
|
|
|
|
} catch (err) {
|
|
|
|
if (cb) return cb(err);
|
|
|
|
}
|
|
|
|
} else if (cb) {
|
|
|
|
cb(err, null);
|
2014-12-10 04:04:56 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
|
|
|
}, method.isStatic);
|
|
|
|
} else {
|
|
|
|
sharedClass.defineMethod(methodName, opts, function() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var last = args[args.length - 1];
|
|
|
|
var cb = typeof last === 'function' ? last : null;
|
|
|
|
this[getterName](function(err, inst) {
|
|
|
|
if (err && cb) return cb(err);
|
|
|
|
if (inst instanceof relation.modelTo) {
|
|
|
|
try {
|
|
|
|
nestedFn.apply(inst, args);
|
|
|
|
} catch (err) {
|
|
|
|
if (cb) return cb(err);
|
|
|
|
}
|
|
|
|
} else if (cb) {
|
|
|
|
cb(err, null);
|
2014-12-10 04:04:56 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
|
|
|
}, method.isStatic);
|
|
|
|
}
|
2014-08-04 16:27:50 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
if (options.hooks === false) return; // don't inherit before/after hooks
|
|
|
|
|
|
|
|
self.once('mounted', function(app, sc, remotes) {
|
|
|
|
var listenerTree = extend({}, remotes.listenerTree || {});
|
|
|
|
listenerTree.before = listenerTree.before || {};
|
|
|
|
listenerTree.after = listenerTree.after || {};
|
|
|
|
|
2016-01-17 14:42:38 +00:00
|
|
|
var beforeListeners = listenerTree.before[toModelName] || {};
|
|
|
|
var afterListeners = listenerTree.after[toModelName] || {};
|
2015-04-03 07:26:19 +00:00
|
|
|
|
|
|
|
sharedClass.methods().forEach(function(method) {
|
|
|
|
var delegateTo = method.rest && method.rest.delegateTo;
|
|
|
|
if (delegateTo && delegateTo.ctor == relation.modelTo) {
|
|
|
|
var before = method.isStatic ? beforeListeners : beforeListeners['prototype'];
|
|
|
|
var after = method.isStatic ? afterListeners : afterListeners['prototype'];
|
|
|
|
var m = method.isStatic ? method.name : 'prototype.' + method.name;
|
|
|
|
if (before && before[delegateTo.name]) {
|
|
|
|
self.beforeRemote(m, function(ctx, result, next) {
|
|
|
|
before[delegateTo.name]._listeners.call(null, ctx, next);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (after && after[delegateTo.name]) {
|
|
|
|
self.afterRemote(m, function(ctx, result, next) {
|
|
|
|
after[delegateTo.name]._listeners.call(null, ctx, next);
|
|
|
|
});
|
|
|
|
}
|
2014-08-05 10:14:39 +00:00
|
|
|
}
|
2015-04-03 07:26:19 +00:00
|
|
|
});
|
2014-08-05 10:14:39 +00:00
|
|
|
});
|
2015-04-03 07:26:19 +00:00
|
|
|
} else {
|
2016-06-07 14:48:28 +00:00
|
|
|
var msg = g.f('Relation `%s` does not exist for model `%s`', relationName, this.modelName);
|
2016-04-01 09:14:26 +00:00
|
|
|
throw new Error(msg);
|
2015-04-03 07:26:19 +00:00
|
|
|
}
|
|
|
|
};
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
Model.ValidationError = require('loopback-datasource-juggler').ValidationError;
|
2014-10-15 14:42:46 +00:00
|
|
|
|
2015-04-03 07:26:19 +00:00
|
|
|
// setup the initial model
|
|
|
|
Model.setup();
|
|
|
|
|
|
|
|
return Model;
|
|
|
|
};
|