2013-12-20 01:49:47 +00:00
|
|
|
/*!
|
2013-07-15 18:32:00 +00:00
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
2014-10-09 15:32:03 +00:00
|
|
|
var registry = require('./registry');
|
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;
|
|
|
|
var stringUtils = require('underscore.string');
|
2013-07-15 18:32:00 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-16 22:54:40 +00:00
|
|
|
* The base class for **all models**.
|
2013-12-20 01:49:47 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* **Inheriting from `Model`**
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* var properties = {...};
|
|
|
|
* var options = {...};
|
|
|
|
* var MyModel = loopback.Model.extend('MyModel', properties, options);
|
|
|
|
* ```
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* **Options**
|
|
|
|
*
|
|
|
|
* - `trackChanges` - If true, changes to the model will be tracked. **Required
|
|
|
|
* for replication.**
|
|
|
|
*
|
|
|
|
* **Events**
|
|
|
|
*
|
|
|
|
* #### Event: `changed`
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* Emitted after a model has been successfully created, saved, or updated.
|
2014-09-02 18:11:36 +00:00
|
|
|
* Argument: `inst`, model instance, object
|
2014-01-28 20:54:41 +00:00
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* MyModel.on('changed', function(inst) {
|
|
|
|
* console.log('model with id %s has been changed', inst.id);
|
|
|
|
* // => model with id 1 has been changed
|
|
|
|
* });
|
|
|
|
* ```
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* #### Event: `deleted`
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
|
|
|
* Emitted after an individual model has been deleted.
|
2014-09-02 18:11:36 +00:00
|
|
|
* Argument: `id`, model ID (number).
|
2014-01-28 20:54:41 +00:00
|
|
|
*
|
|
|
|
* ```js
|
2014-09-02 18:11:36 +00:00
|
|
|
* MyModel.on('deleted', function(id) {
|
|
|
|
* console.log('model with id %s has been deleted', id);
|
2014-01-28 20:54:41 +00:00
|
|
|
* // => model with id 1 has been deleted
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* #### Event: `deletedAll`
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* Emitted after an individual model has been deleted.
|
2014-09-02 18:11:36 +00:00
|
|
|
* Argument: `where` (optional), where filter, JSON object.
|
2014-01-28 20:54:41 +00:00
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* MyModel.on('deletedAll', function(where) {
|
|
|
|
* if(where) {
|
2014-09-02 18:11:36 +00:00
|
|
|
* console.log('all models where ', where, ' have been deleted');
|
2014-01-28 20:54:41 +00:00
|
|
|
* // => all models where
|
|
|
|
* // => {price: {gt: 100}}
|
|
|
|
* // => have been deleted
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* ```
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* #### Event: `attached`
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* Emitted after a `Model` has been attached to an `app`.
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* #### Event: `dataSourceAttached`
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-01-28 20:54:41 +00:00
|
|
|
* Emitted after a `Model` has been attached to a `DataSource`.
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-09-02 18:11:36 +00:00
|
|
|
* #### Event: set
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-09-02 18:11:36 +00:00
|
|
|
* Emitted when model property is set.
|
|
|
|
* Argument: `inst`, model instance, object
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-09-02 18:11:36 +00:00
|
|
|
* ```js
|
|
|
|
* MyModel.on('set', function(inst) {
|
|
|
|
* console.log('model with id %s has been changed', inst.id);
|
|
|
|
* // => model with id 1 has been changed
|
|
|
|
* });
|
|
|
|
* ```
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2013-12-20 01:49:47 +00:00
|
|
|
* @param {Object} data
|
2014-10-15 07:07:30 +00:00
|
|
|
* @property {String} modelName The name of the model. Static property.
|
|
|
|
* @property {DataSource} dataSource Data source to which the model is connected, if any. Static property.
|
|
|
|
* @class
|
2013-07-15 18:32:00 +00:00
|
|
|
*/
|
|
|
|
|
2014-07-10 20:46:06 +00:00
|
|
|
var Model = module.exports = registry.modelBuilder.define('Model');
|
2013-07-15 18:32:00 +00:00
|
|
|
|
|
|
|
/*!
|
2013-07-15 21:07:17 +00:00
|
|
|
* Called when a model is extended.
|
2013-07-15 18:32:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
Model.setup = function () {
|
|
|
|
var ModelCtor = this;
|
2014-01-28 20:54:41 +00:00
|
|
|
var options = this.settings;
|
2014-08-28 18:56:43 +00:00
|
|
|
var typeName = this.modelName;
|
2014-05-16 19:32:54 +00:00
|
|
|
|
2014-10-11 14:36:29 +00:00
|
|
|
var remotingOptions = {};
|
|
|
|
extend(remotingOptions, options.remoting || {});
|
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
// create a sharedClass
|
|
|
|
var sharedClass = ModelCtor.sharedClass = new SharedClass(
|
2014-07-21 14:56:46 +00:00
|
|
|
ModelCtor.modelName,
|
2014-05-19 22:56:26 +00:00
|
|
|
ModelCtor,
|
2014-10-11 14:36:29 +00:00
|
|
|
remotingOptions
|
2014-05-19 22:56:26 +00:00
|
|
|
);
|
|
|
|
|
2014-08-28 18:56:43 +00:00
|
|
|
// setup a remoting type converter for this model
|
|
|
|
RemoteObjects.convert(typeName, function(val) {
|
|
|
|
return val ? new ModelCtor(val) : val;
|
|
|
|
});
|
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
// support remoting prototype methods
|
2013-07-16 03:14:04 +00:00
|
|
|
ModelCtor.sharedCtor = function (data, id, fn) {
|
2014-05-19 22:56:26 +00:00
|
|
|
var ModelCtor = this;
|
|
|
|
|
2013-07-16 02:45:27 +00:00
|
|
|
if(typeof data === 'function') {
|
|
|
|
fn = data;
|
|
|
|
data = null;
|
|
|
|
id = null;
|
|
|
|
} else if (typeof id === 'function') {
|
|
|
|
fn = id;
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2013-07-16 02:45:27 +00:00
|
|
|
if(typeof data !== 'object') {
|
|
|
|
id = data;
|
|
|
|
data = null;
|
|
|
|
} else {
|
|
|
|
id = null;
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2013-07-16 02:45:27 +00:00
|
|
|
if(id && data) {
|
2013-07-16 19:51:59 +00:00
|
|
|
var model = new ModelCtor(data);
|
2013-07-16 02:45:27 +00:00
|
|
|
model.id = id;
|
|
|
|
fn(null, model);
|
|
|
|
} else if(data) {
|
2013-07-16 19:51:59 +00:00
|
|
|
fn(null, new ModelCtor(data));
|
2013-07-16 02:45:27 +00:00
|
|
|
} else if(id) {
|
2013-08-17 05:11:58 +00:00
|
|
|
ModelCtor.findById(id, function (err, model) {
|
2013-07-16 02:45:27 +00:00
|
|
|
if(err) {
|
|
|
|
fn(err);
|
|
|
|
} else if(model) {
|
|
|
|
fn(null, model);
|
|
|
|
} else {
|
|
|
|
err = new Error('could not find a model with id ' + id);
|
|
|
|
err.statusCode = 404;
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2013-07-16 02:45:27 +00:00
|
|
|
fn(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fn(new Error('must specify an id or data'));
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-07-16 02:45:27 +00:00
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
var idDesc = ModelCtor.modelName + ' id';
|
|
|
|
ModelCtor.sharedCtor.accepts = [
|
2014-07-24 17:26:49 +00:00
|
|
|
{arg: 'id', type: 'any', required: true, http: {source: 'path'},
|
|
|
|
description: idDesc}
|
2014-05-19 22:56:26 +00:00
|
|
|
// {arg: 'instance', type: 'object', http: {source: 'body'}}
|
|
|
|
];
|
|
|
|
|
|
|
|
ModelCtor.sharedCtor.http = [
|
|
|
|
{path: '/:id'}
|
|
|
|
];
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
ModelCtor.sharedCtor.returns = {root: true};
|
|
|
|
|
2013-07-16 02:45:27 +00:00
|
|
|
// before remote hook
|
|
|
|
ModelCtor.beforeRemote = function (name, fn) {
|
|
|
|
var self = this;
|
|
|
|
if(this.app) {
|
|
|
|
var remotes = this.app.remotes();
|
2014-07-21 14:56:46 +00:00
|
|
|
var className = self.modelName;
|
2014-01-22 10:22:23 +00:00
|
|
|
remotes.before(className + '.' + name, function (ctx, next) {
|
2013-07-16 02:45:27 +00:00
|
|
|
fn(ctx, ctx.result, next);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var args = arguments;
|
|
|
|
this.once('attached', function () {
|
|
|
|
self.beforeRemote.apply(self, args);
|
|
|
|
});
|
|
|
|
}
|
2013-11-20 21:31:30 +00:00
|
|
|
};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2013-07-16 17:25:02 +00:00
|
|
|
// after remote hook
|
|
|
|
ModelCtor.afterRemote = function (name, fn) {
|
|
|
|
var self = this;
|
|
|
|
if(this.app) {
|
|
|
|
var remotes = this.app.remotes();
|
2014-07-21 14:56:46 +00:00
|
|
|
var className = self.modelName;
|
2014-01-22 10:22:23 +00:00
|
|
|
remotes.after(className + '.' + name, function (ctx, next) {
|
2013-07-16 17:25:02 +00:00
|
|
|
fn(ctx, ctx.result, next);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var args = arguments;
|
|
|
|
this.once('attached', function () {
|
|
|
|
self.afterRemote.apply(self, args);
|
|
|
|
});
|
|
|
|
}
|
2013-11-20 21:31:30 +00:00
|
|
|
};
|
2013-08-16 23:39:13 +00:00
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
// resolve relation functions
|
|
|
|
sharedClass.resolve(function resolver(define) {
|
2014-08-08 22:55:47 +00:00
|
|
|
|
|
|
|
var relations = ModelCtor.relations || {};
|
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
// get the relations
|
2014-07-16 23:35:43 +00:00
|
|
|
for (var relationName in relations) {
|
2014-05-19 22:56:26 +00:00
|
|
|
var relation = relations[relationName];
|
2014-07-16 23:35:43 +00:00
|
|
|
if (relation.type === 'belongsTo') {
|
2014-08-22 13:39:53 +00:00
|
|
|
ModelCtor.belongsToRemoting(relationName, relation, define);
|
|
|
|
} else if (
|
|
|
|
relation.type === 'hasOne' ||
|
|
|
|
relation.type === 'embedsOne'
|
|
|
|
) {
|
|
|
|
ModelCtor.hasOneRemoting(relationName, relation, define);
|
2014-07-30 11:24:40 +00:00
|
|
|
} else if (
|
|
|
|
relation.type === 'hasMany' ||
|
|
|
|
relation.type === 'embedsMany' ||
|
|
|
|
relation.type === 'referencesMany') {
|
2014-07-16 23:35:43 +00:00
|
|
|
ModelCtor.hasManyRemoting(relationName, relation, define);
|
2014-05-19 22:56:26 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-08 22:55:47 +00:00
|
|
|
|
|
|
|
// handle scopes
|
|
|
|
var scopes = ModelCtor.scopes || {};
|
|
|
|
for (var scopeName in scopes) {
|
|
|
|
ModelCtor.scopeRemoting(scopeName, scopes[scopeName], define);
|
|
|
|
}
|
2014-05-19 22:56:26 +00:00
|
|
|
});
|
2014-08-08 22:55:47 +00:00
|
|
|
|
2013-07-15 18:32:00 +00:00
|
|
|
return ModelCtor;
|
2013-11-20 21:31:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Get the reference to ACL in a lazy fashion to avoid race condition in require
|
|
|
|
*/
|
2014-02-04 00:00:01 +00:00
|
|
|
var _aclModel = null;
|
|
|
|
Model._ACL = function getACL(ACL) {
|
|
|
|
if(ACL !== undefined) {
|
|
|
|
// The function is used as a setter
|
|
|
|
_aclModel = ACL;
|
|
|
|
}
|
|
|
|
if(_aclModel) {
|
|
|
|
return _aclModel;
|
|
|
|
}
|
2014-10-15 14:42:46 +00:00
|
|
|
var aclModel = registry.getModel('ACL');
|
2014-06-06 09:47:25 +00:00
|
|
|
_aclModel = registry.getModelByType(aclModel);
|
2014-02-04 00:00:01 +00:00
|
|
|
return _aclModel;
|
|
|
|
};
|
|
|
|
|
2013-11-20 21:31:30 +00:00
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Check if the given access token can invoke the specified method.
|
2013-11-20 21:31:30 +00:00
|
|
|
*
|
2014-10-15 07:07:30 +00:00
|
|
|
* @param {AccessToken} token The access token.
|
2014-06-11 21:55:47 +00:00
|
|
|
* @param {*} modelId The model ID.
|
2014-10-15 07:07:30 +00:00
|
|
|
* @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.
|
2014-06-11 21:55:47 +00:00
|
|
|
* @param {Boolean} allowed True if the request is allowed; false otherwise.
|
2013-11-20 21:31:30 +00:00
|
|
|
*/
|
2014-01-26 22:02:56 +00:00
|
|
|
|
2014-08-08 05:19:27 +00:00
|
|
|
Model.checkAccess = function(token, modelId, sharedMethod, ctx, callback) {
|
2014-10-15 14:42:46 +00:00
|
|
|
var ANONYMOUS = registry.getModel('AccessToken').ANONYMOUS;
|
2013-12-10 23:57:55 +00:00
|
|
|
token = token || ANONYMOUS;
|
2014-02-04 00:00:01 +00:00
|
|
|
var aclModel = Model._ACL();
|
2014-08-08 05:19:27 +00:00
|
|
|
|
|
|
|
ctx = ctx || {};
|
|
|
|
if(typeof ctx === 'function' && callback === undefined) {
|
|
|
|
callback = ctx;
|
|
|
|
ctx = {};
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-05-31 02:29:30 +00:00
|
|
|
aclModel.checkAccessForContext({
|
|
|
|
accessToken: token,
|
|
|
|
model: this,
|
|
|
|
property: sharedMethod.name,
|
|
|
|
method: sharedMethod.name,
|
|
|
|
sharedMethod: sharedMethod,
|
2014-06-02 20:41:14 +00:00
|
|
|
modelId: modelId,
|
2014-08-08 05:19:27 +00:00
|
|
|
accessType: this._getAccessTypeForMethod(sharedMethod),
|
|
|
|
remotingContext: ctx
|
2014-05-31 02:29:30 +00:00
|
|
|
}, function(err, accessRequest) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
callback(null, accessRequest.isAllowed());
|
|
|
|
});
|
2013-11-20 21:31:30 +00:00
|
|
|
};
|
|
|
|
|
2013-12-20 01:49:47 +00:00
|
|
|
/*!
|
2013-12-07 01:04:47 +00:00
|
|
|
* Determine the access type for the given `RemoteMethod`.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
* @param {RemoteMethod} method
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model._getAccessTypeForMethod = function(method) {
|
|
|
|
if(typeof method === 'string') {
|
|
|
|
method = {name: method};
|
|
|
|
}
|
|
|
|
assert(
|
2014-10-16 22:54:40 +00:00
|
|
|
typeof method === 'object',
|
2013-12-07 01:04:47 +00:00
|
|
|
'method is a required argument and must be a RemoteMethod object'
|
|
|
|
);
|
|
|
|
|
2014-02-04 00:00:01 +00:00
|
|
|
var ACL = Model._ACL();
|
2013-12-07 01:04:47 +00:00
|
|
|
|
|
|
|
switch(method.name) {
|
|
|
|
case'create':
|
|
|
|
return ACL.WRITE;
|
|
|
|
case 'updateOrCreate':
|
|
|
|
return ACL.WRITE;
|
|
|
|
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;
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-12-07 01:04:47 +00:00
|
|
|
|
2014-02-21 03:43:50 +00:00
|
|
|
/**
|
|
|
|
* Get the `Application` the Model is attached to.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Application} app
|
|
|
|
* @end
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model.getApp = function(callback) {
|
|
|
|
var Model = this;
|
|
|
|
if(this.app) {
|
|
|
|
callback(null, this.app);
|
|
|
|
} else {
|
|
|
|
Model.once('attached', function() {
|
|
|
|
assert(Model.app);
|
|
|
|
callback(null, Model.app);
|
|
|
|
});
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-02-21 03:43:50 +00:00
|
|
|
|
2014-05-16 19:32:54 +00:00
|
|
|
/**
|
|
|
|
* Enable remote invocation for the method with the given name.
|
2014-10-15 07:07:30 +00:00
|
|
|
* See [Defining remote methods](http://docs.strongloop.com/display/LB/Defining+remote+methods) for more information.
|
|
|
|
*
|
2014-07-29 18:54:26 +00:00
|
|
|
* Static method example:
|
2014-05-16 19:32:54 +00:00
|
|
|
* ```js
|
2014-07-29 18:54:26 +00:00
|
|
|
* Model.myMethod();
|
2014-05-16 19:32:54 +00:00
|
|
|
* Model.remoteMethod('myMethod');
|
2014-07-29 18:54:26 +00:00
|
|
|
* ```
|
2014-10-15 07:07:30 +00:00
|
|
|
*
|
2014-07-29 18:54:26 +00:00
|
|
|
* @param {String} name The name of the method.
|
2014-05-16 19:32:54 +00:00
|
|
|
* @param {Object} options The remoting options.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model.remoteMethod = function(name, options) {
|
2014-05-19 22:56:26 +00:00
|
|
|
if(options.isStatic === undefined) {
|
|
|
|
options.isStatic = true;
|
|
|
|
}
|
2014-05-16 19:32:54 +00:00
|
|
|
this.sharedClass.defineMethod(name, options);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-16 19:32:54 +00:00
|
|
|
|
2014-09-29 20:05:31 +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) {
|
|
|
|
this.sharedClass.disableMethod(name, isStatic || false);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-09-29 20:05:31 +00:00
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
Model.belongsToRemoting = function(relationName, relation, define) {
|
2014-08-07 13:39:07 +00:00
|
|
|
var modelName = relation.modelTo && relation.modelTo.modelName;
|
|
|
|
modelName = modelName || 'PersistedModel';
|
2014-05-19 22:56:26 +00:00
|
|
|
var fn = this.prototype[relationName];
|
2014-08-05 07:10:43 +00:00
|
|
|
var pathName = (relation.options.http && relation.options.http.path) || relationName;
|
2014-06-21 06:48:46 +00:00
|
|
|
define('__get__' + relationName, {
|
2014-05-19 22:56:26 +00:00
|
|
|
isStatic: false,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'get', path: '/' + pathName},
|
2014-05-19 22:56:26 +00:00
|
|
|
accepts: {arg: 'refresh', type: 'boolean', http: {source: 'query'}},
|
|
|
|
description: 'Fetches belongsTo relation ' + relationName,
|
2014-08-07 13:39:07 +00:00
|
|
|
returns: {arg: relationName, type: modelName, root: true}
|
2014-05-19 22:56:26 +00:00
|
|
|
}, fn);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2014-08-01 09:25:28 +00:00
|
|
|
Model.hasOneRemoting = function(relationName, relation, define) {
|
|
|
|
var fn = this.prototype[relationName];
|
2014-08-05 07:10:43 +00:00
|
|
|
var pathName = (relation.options.http && relation.options.http.path) || relationName;
|
2014-08-01 09:25:28 +00:00
|
|
|
define('__get__' + relationName, {
|
|
|
|
isStatic: false,
|
|
|
|
http: {verb: 'get', path: '/' + pathName},
|
|
|
|
accepts: {arg: 'refresh', type: 'boolean', http: {source: 'query'}},
|
|
|
|
description: 'Fetches hasOne relation ' + relationName,
|
|
|
|
returns: {arg: relationName, type: relation.modelTo.modelName, root: true}
|
|
|
|
}, fn);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-08-01 09:25:28 +00:00
|
|
|
|
2014-07-16 23:35:43 +00:00
|
|
|
Model.hasManyRemoting = function (relationName, relation, define) {
|
2014-08-05 07:10:43 +00:00
|
|
|
var pathName = (relation.options.http && relation.options.http.path) || relationName;
|
2014-07-24 17:26:49 +00:00
|
|
|
var toModelName = relation.modelTo.modelName;
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
function convertNullToNotFoundError(ctx, cb) {
|
|
|
|
if (ctx.result !== null) return cb();
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
var fk = ctx.getArgByName('fk');
|
|
|
|
var msg = 'Unknown "' + toModelName + '" id "' + fk + '".';
|
|
|
|
var error = new Error(msg);
|
|
|
|
error.statusCode = error.status = 404;
|
|
|
|
cb(error);
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-07-16 23:35:43 +00:00
|
|
|
var findByIdFunc = this.prototype['__findById__' + relationName];
|
|
|
|
define('__findById__' + relationName, {
|
|
|
|
isStatic: false,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'get', path: '/' + pathName + '/:fk'},
|
2014-07-16 23:35:43 +00:00
|
|
|
accepts: {arg: 'fk', type: 'any',
|
|
|
|
description: 'Foreign key for ' + relationName, required: true,
|
|
|
|
http: {source: 'path'}},
|
|
|
|
description: 'Find a related item by id for ' + relationName,
|
2014-08-30 14:54:14 +00:00
|
|
|
returns: {arg: 'result', type: toModelName, root: true},
|
|
|
|
rest: {after: convertNullToNotFoundError}
|
2014-07-16 23:35:43 +00:00
|
|
|
}, findByIdFunc);
|
|
|
|
|
|
|
|
var destroyByIdFunc = this.prototype['__destroyById__' + relationName];
|
|
|
|
define('__destroyById__' + relationName, {
|
|
|
|
isStatic: false,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'delete', path: '/' + pathName + '/:fk'},
|
2014-07-16 23:35:43 +00:00
|
|
|
accepts: {arg: 'fk', type: 'any',
|
|
|
|
description: 'Foreign key for ' + relationName, required: true,
|
|
|
|
http: {source: 'path'}},
|
|
|
|
description: 'Delete a related item by id for ' + relationName,
|
|
|
|
returns: {}
|
|
|
|
}, destroyByIdFunc);
|
|
|
|
|
|
|
|
var updateByIdFunc = this.prototype['__updateById__' + relationName];
|
|
|
|
define('__updateById__' + relationName, {
|
|
|
|
isStatic: false,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'put', path: '/' + pathName + '/:fk'},
|
2014-07-24 17:26:49 +00:00
|
|
|
accepts: [
|
|
|
|
{arg: 'fk', type: 'any',
|
|
|
|
description: 'Foreign key for ' + relationName, required: true,
|
|
|
|
http: {source: 'path'}},
|
|
|
|
{arg: 'data', type: toModelName, http: {source: 'body'}}
|
|
|
|
],
|
2014-07-16 23:35:43 +00:00
|
|
|
description: 'Update a related item by id for ' + relationName,
|
2014-07-24 17:26:49 +00:00
|
|
|
returns: {arg: 'result', type: toModelName, root: true}
|
2014-07-16 23:35:43 +00:00
|
|
|
}, updateByIdFunc);
|
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
if (relation.modelThrough || relation.type === 'referencesMany') {
|
|
|
|
var modelThrough = relation.modelThrough || relation.modelTo;
|
2014-09-01 05:52:14 +00:00
|
|
|
|
|
|
|
var accepts = [];
|
|
|
|
if (relation.type === 'hasMany' && relation.modelThrough) {
|
2014-09-01 05:55:53 +00:00
|
|
|
// Restrict: only hasManyThrough relation can have additional properties
|
2014-09-01 05:52:14 +00:00
|
|
|
accepts.push({arg: 'data', type: modelThrough.modelName, http: {source: 'body'}});
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:35:43 +00:00
|
|
|
var addFunc = this.prototype['__link__' + relationName];
|
|
|
|
define('__link__' + relationName, {
|
|
|
|
isStatic: false,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'put', path: '/' + pathName + '/rel/:fk'},
|
2014-09-01 05:52:14 +00:00
|
|
|
accepts: [{arg: 'fk', type: 'any',
|
2014-07-16 23:35:43 +00:00
|
|
|
description: 'Foreign key for ' + relationName, required: true,
|
2014-09-01 05:52:14 +00:00
|
|
|
http: {source: 'path'}}].concat(accepts),
|
2014-07-16 23:35:43 +00:00
|
|
|
description: 'Add a related item by id for ' + relationName,
|
2014-07-30 13:01:22 +00:00
|
|
|
returns: {arg: relationName, type: modelThrough.modelName, root: true}
|
2014-07-16 23:35:43 +00:00
|
|
|
}, addFunc);
|
|
|
|
|
|
|
|
var removeFunc = this.prototype['__unlink__' + relationName];
|
|
|
|
define('__unlink__' + relationName, {
|
|
|
|
isStatic: false,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'delete', path: '/' + pathName + '/rel/:fk'},
|
2014-07-16 23:35:43 +00:00
|
|
|
accepts: {arg: 'fk', type: 'any',
|
|
|
|
description: 'Foreign key for ' + relationName, required: true,
|
|
|
|
http: {source: 'path'}},
|
|
|
|
description: 'Remove the ' + relationName + ' relation to an item by id',
|
|
|
|
returns: {}
|
|
|
|
}, 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,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'head', path: '/' + pathName + '/rel/:fk'},
|
2014-07-16 23:35:43 +00:00
|
|
|
accepts: {arg: 'fk', type: 'any',
|
|
|
|
description: 'Foreign key for ' + relationName, required: true,
|
|
|
|
http: {source: 'path'}},
|
|
|
|
description: 'Check the existence of ' + relationName + ' relation to an item by id',
|
2014-07-28 16:37:50 +00:00
|
|
|
returns: {arg: 'exists', type: 'boolean', root: true},
|
|
|
|
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');
|
|
|
|
var msg = 'Unknown "' + modelName + '" id "' + id + '".';
|
|
|
|
var error = new Error(msg);
|
|
|
|
error.statusCode = error.status = 404;
|
|
|
|
cb(error);
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 23:35:43 +00:00
|
|
|
}, existsFunc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-08 22:55:47 +00:00
|
|
|
Model.scopeRemoting = function(scopeName, scope, define) {
|
|
|
|
var pathName = (scope.options && scope.options.http && scope.options.http.path)
|
|
|
|
|| scopeName;
|
|
|
|
var isStatic = scope.isStatic;
|
|
|
|
var toModelName = scope.modelTo.modelName;
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2014-08-08 22:55:47 +00:00
|
|
|
define('__get__' + scopeName, {
|
|
|
|
isStatic: isStatic,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'get', path: '/' + pathName},
|
2014-05-19 22:56:26 +00:00
|
|
|
accepts: {arg: 'filter', type: 'object'},
|
2014-08-08 22:55:47 +00:00
|
|
|
description: 'Queries ' + scopeName + ' of ' + this.modelName + '.',
|
|
|
|
returns: {arg: scopeName, type: [toModelName], root: true}
|
2014-05-19 22:56:26 +00:00
|
|
|
});
|
|
|
|
|
2014-08-08 22:55:47 +00:00
|
|
|
define('__create__' + scopeName, {
|
|
|
|
isStatic: isStatic,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'post', path: '/' + pathName},
|
2014-07-24 17:26:49 +00:00
|
|
|
accepts: {arg: 'data', type: toModelName, http: {source: 'body'}},
|
2014-08-08 22:55:47 +00:00
|
|
|
description: 'Creates a new instance in ' + scopeName + ' of this model.',
|
2014-05-19 22:56:26 +00:00
|
|
|
returns: {arg: 'data', type: toModelName, root: true}
|
|
|
|
});
|
|
|
|
|
2014-08-08 22:55:47 +00:00
|
|
|
define('__delete__' + scopeName, {
|
|
|
|
isStatic: isStatic,
|
2014-08-01 09:25:28 +00:00
|
|
|
http: {verb: 'delete', path: '/' + pathName},
|
2014-08-08 22:55:47 +00:00
|
|
|
description: 'Deletes all ' + scopeName + ' of this model.'
|
2014-05-19 22:56:26 +00:00
|
|
|
});
|
2014-08-12 13:03:15 +00:00
|
|
|
|
|
|
|
define('__count__' + scopeName, {
|
|
|
|
isStatic: isStatic,
|
|
|
|
http: {verb: 'get', path: '/' + pathName + '/count'},
|
2014-08-26 12:41:36 +00:00
|
|
|
accepts: {arg: 'where', type: 'object', description: 'Criteria to match model instances'},
|
2014-08-12 13:03:15 +00:00
|
|
|
description: 'Counts ' + scopeName + ' of ' + this.modelName + '.',
|
2014-10-16 22:54:40 +00:00
|
|
|
returns: {arg: 'count', type: 'number'}
|
2014-08-12 13:03:15 +00:00
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
};
|
|
|
|
|
2014-08-04 17:08:43 +00:00
|
|
|
Model.nestRemoting = function(relationName, options, cb) {
|
|
|
|
if (typeof options === 'function' && !cb) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2014-08-04 16:27:50 +00:00
|
|
|
options = options || {};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 17:08:43 +00:00
|
|
|
var regExp = /^__([^_]+)__([^_]+)$/;
|
2014-08-04 16:27:50 +00:00
|
|
|
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;
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
var pathName = options.pathName || relation.options.path || relationName;
|
|
|
|
var paramName = options.paramName || 'nk';
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
var http = [].concat(sharedToClass.http || [])[0];
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
if (relation.multiple) {
|
|
|
|
var httpPath = pathName + '/:' + paramName;
|
|
|
|
var acceptArgs = [
|
2014-10-16 22:54:40 +00:00
|
|
|
{
|
|
|
|
arg: paramName, type: 'any', http: { source: 'path' },
|
2014-08-04 16:27:50 +00:00
|
|
|
description: 'Foreign key for ' + relation.name,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
var httpPath = pathName;
|
|
|
|
var acceptArgs = [];
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 17:02:30 +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.
|
2014-08-04 17:08:43 +00:00
|
|
|
var filter = cb || options.filterMethod || function(method, relation) {
|
2014-08-04 17:02:30 +00:00
|
|
|
var matches = method.name.match(regExp);
|
|
|
|
if (matches) {
|
|
|
|
return '__' + matches[1] + '__' + relation.name + '__' + matches[2];
|
|
|
|
}
|
|
|
|
};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
sharedToClass.methods().forEach(function(method) {
|
2014-08-04 17:02:30 +00:00
|
|
|
var methodName;
|
|
|
|
if (!method.isStatic && (methodName = filter(method, relation))) {
|
2014-08-04 16:27:50 +00:00
|
|
|
var prefix = relation.multiple ? '__findById__' : '__get__';
|
|
|
|
var getterName = options.getterName || (prefix + relationName);
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
var getterFn = relation.modelFrom.prototype[getterName];
|
|
|
|
if (typeof getterFn !== 'function') {
|
|
|
|
throw new Error('Invalid remote method: `' + getterName + '`');
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
var nestedFn = relation.modelTo.prototype[method.name];
|
|
|
|
if (typeof nestedFn !== 'function') {
|
|
|
|
throw new Error('Invalid remote method: `' + method.name + '`');
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
var opts = {};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
opts.accepts = acceptArgs.concat(method.accepts || []);
|
|
|
|
opts.returns = [].concat(method.returns || []);
|
|
|
|
opts.description = method.description;
|
|
|
|
opts.rest = extend({}, method.rest || {});
|
2014-08-05 10:14:39 +00:00
|
|
|
opts.rest.delegateTo = method.name;
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
if (relation.multiple) {
|
|
|
|
sharedClass.defineMethod(methodName, opts, function(fkId) {
|
2014-08-05 10:14:39 +00:00
|
|
|
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) {
|
|
|
|
nestedFn.apply(inst, args);
|
|
|
|
} else if (cb) {
|
|
|
|
cb(err, null);
|
|
|
|
}
|
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
}, method.isStatic);
|
|
|
|
} else {
|
|
|
|
sharedClass.defineMethod(methodName, opts, function() {
|
2014-08-05 10:14:39 +00:00
|
|
|
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) {
|
|
|
|
nestedFn.apply(inst, args);
|
|
|
|
} else if (cb) {
|
|
|
|
cb(err, null);
|
|
|
|
}
|
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
}, method.isStatic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
if (options.hooks === false) return; // don't inherit before/after hooks
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
self.once('mounted', function(app, sc, remotes) {
|
|
|
|
var listenerTree = extend({}, remotes.listenerTree || {});
|
|
|
|
listenerTree.before = listenerTree.before || {};
|
|
|
|
listenerTree.after = listenerTree.after || {};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
var beforeListeners = remotes.listenerTree.before[toModelName] || {};
|
|
|
|
var afterListeners = remotes.listenerTree.after[toModelName] || {};
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
sharedClass.methods().forEach(function(method) {
|
|
|
|
var delegateTo = method.rest && method.rest.delegateTo;
|
|
|
|
if (delegateTo) {
|
|
|
|
var before = method.isStatic ? beforeListeners : beforeListeners['prototype'];
|
|
|
|
var after = method.isStatic ? afterListeners : afterListeners['prototype'];
|
|
|
|
var m = method.isStatic ? method.name : 'prototype.' + method.name;
|
2014-08-22 20:18:24 +00:00
|
|
|
if (before && before[delegateTo]) {
|
2014-08-05 10:14:39 +00:00
|
|
|
self.beforeRemote(m, function(ctx, result, next) {
|
|
|
|
before[delegateTo]._listeners.call(null, ctx, next);
|
|
|
|
});
|
|
|
|
}
|
2014-08-22 20:18:24 +00:00
|
|
|
if (after && after[delegateTo]) {
|
2014-08-05 10:14:39 +00:00
|
|
|
self.afterRemote(m, function(ctx, result, next) {
|
|
|
|
after[delegateTo]._listeners.call(null, ctx, next);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Relation `' + relationName + '` does not exist for model `' + this.modelName + '`');
|
|
|
|
}
|
|
|
|
};
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2014-10-15 14:42:46 +00:00
|
|
|
Model.ValidationError = require('loopback-datasource-juggler').ValidationError;
|
|
|
|
|
2013-07-15 18:32:00 +00:00
|
|
|
// setup the initial model
|
2013-11-11 21:35:54 +00:00
|
|
|
Model.setup();
|
2013-11-20 21:31:30 +00:00
|
|
|
|