2014-02-20 01:09:36 +00:00
|
|
|
/*!
|
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
2014-05-03 01:12:24 +00:00
|
|
|
|
2014-02-20 01:09:36 +00:00
|
|
|
var Model = require('./model');
|
2014-10-15 14:42:46 +00:00
|
|
|
var registry = require('./registry');
|
2014-10-09 15:32:03 +00:00
|
|
|
var runtime = require('./runtime');
|
2014-05-06 20:31:23 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
var async = require('async');
|
2015-02-26 15:29:29 +00:00
|
|
|
var deprecated = require('depd')('loopback');
|
2014-02-20 01:09:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extends Model with basic query and CRUD support.
|
|
|
|
*
|
2014-04-14 21:49:29 +00:00
|
|
|
* **Change Event**
|
|
|
|
*
|
|
|
|
* Listen for model changes using the `change` event.
|
|
|
|
*
|
|
|
|
* ```js
|
2014-06-05 07:45:09 +00:00
|
|
|
* MyPersistedModel.on('changed', function(obj) {
|
2014-04-14 21:49:29 +00:00
|
|
|
* console.log(obj) // => the changed model
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
2014-06-05 07:45:09 +00:00
|
|
|
* @class PersistedModel
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
var PersistedModel = module.exports = Model.extend('PersistedModel');
|
2014-02-20 01:09:36 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
/*!
|
2014-06-05 07:45:09 +00:00
|
|
|
* Setup the `PersistedModel` constructor.
|
2014-05-03 01:12:24 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.setup = function setupPersistedModel() {
|
2014-05-03 03:04:06 +00:00
|
|
|
// call Model.setup first
|
|
|
|
Model.setup.call(this);
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
var PersistedModel = this;
|
2014-05-03 01:12:24 +00:00
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
// enable change tracking (usually for replication)
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.settings.trackChanges) {
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel._defineChangeModel();
|
|
|
|
PersistedModel.once('dataSourceAttached', function() {
|
|
|
|
PersistedModel.enableChangeTracking();
|
2014-05-06 20:31:23 +00:00
|
|
|
});
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.setupRemoting();
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-02-21 17:10:48 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Throw an error telling the user that the method is not available and why.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function throwNotAttached(modelName, methodName) {
|
|
|
|
throw new Error(
|
2014-10-30 20:49:47 +00:00
|
|
|
'Cannot call ' + modelName + '.' + methodName + '().' +
|
|
|
|
' The ' + methodName + ' method has not been setup.' +
|
|
|
|
' The PersistedModel has not been correctly attached to a DataSource!'
|
2014-02-21 17:10:48 +00:00
|
|
|
);
|
2014-02-20 01:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Convert null callbacks to 404 error objects.
|
|
|
|
* @param {HttpContext} ctx
|
|
|
|
* @param {Function} cb
|
|
|
|
*/
|
|
|
|
|
|
|
|
function convertNullToNotFoundError(ctx, cb) {
|
|
|
|
if (ctx.result !== null) return cb();
|
|
|
|
|
|
|
|
var modelName = ctx.method.sharedClass.name;
|
|
|
|
var id = ctx.getArgByName('id');
|
2014-05-16 03:32:12 +00:00
|
|
|
var msg = 'Unknown "' + modelName + '" id "' + id + '".';
|
2014-02-20 01:09:36 +00:00
|
|
|
var error = new Error(msg);
|
|
|
|
error.statusCode = error.status = 404;
|
2014-12-18 20:26:27 +00:00
|
|
|
error.code = 'MODEL_NOT_FOUND';
|
2014-02-20 01:09:36 +00:00
|
|
|
cb(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Create new instance of Model, and save to database.
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Object}|[{Object}] data Optional data argument. Can be either a single model instance or an array of instances.
|
2015-02-18 17:13:47 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} callback Callback function called with `cb(err, obj)` signature.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Object} models Model instances or null.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
PersistedModel.create = function(data, callback) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'create');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update or insert a model instance
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Object} data The model instance data to insert.
|
|
|
|
* @callback {Function} callback Callback function called with `cb(err, obj)` signature.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Object} model Updated model instance.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.upsert = PersistedModel.updateOrCreate = function upsert(data, callback) {
|
2014-05-19 22:56:26 +00:00
|
|
|
throwNotAttached(this.modelName, 'upsert');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Find one record matching the optional `where` filter. The same as `find`, but limited to one object.
|
|
|
|
* Returns an object, not collection.
|
2014-10-15 07:07:30 +00:00
|
|
|
* If not found, create the object using data provided as second argument.
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2015-01-23 01:01:31 +00:00
|
|
|
* @param {Object} where Where clause, such as `{where: {test: 'me'}}`
|
|
|
|
* <br/>see [Where filter](http://docs.strongloop.com/display/public/LB/Where+filter).
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Object} data Data to insert if object matching the `where` filter is not found.
|
|
|
|
* @callback {Function} callback Callback function called with `cb(err, instance)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Object} instance Model instance matching the `where` filter, if found.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.findOrCreate = function findOrCreate(query, data, callback) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'findOrCreate');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
2014-07-16 16:09:07 +00:00
|
|
|
PersistedModel.findOrCreate._delegate = true;
|
|
|
|
|
2014-02-20 01:09:36 +00:00
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Check whether a model instance exists in database.
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {id} id Identifier of object (primary key value).
|
|
|
|
*
|
|
|
|
* @callback {Function} callback Callback function called with `(err, exists)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
2015-02-18 17:13:47 +00:00
|
|
|
* @param {Boolean} exists True if the instance with the specified ID exists; false otherwise.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.exists = function exists(id, cb) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'exists');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Find object by ID.
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {*} id Primary key value
|
|
|
|
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
2015-02-18 17:13:47 +00:00
|
|
|
* @param {Object} instance Model instance matching the specified ID or null if no instance matches.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.findById = function find(id, cb) {
|
2014-05-19 22:56:26 +00:00
|
|
|
throwNotAttached(this.modelName, 'findById');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Find all model instances that match `filter` specification.
|
|
|
|
* See [Querying models](http://docs.strongloop.com/display/LB/Querying+models).
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2014-10-15 07:07:30 +00:00
|
|
|
* @options {Object} [filter] Optional Filter JSON object; see below.
|
|
|
|
* @property {String|Object|Array} fields Identify fields to include in return result.
|
|
|
|
* <br/>See [Fields filter](http://docs.strongloop.com/display/LB/Fields+filter).
|
|
|
|
* @property {String|Object|Array} include See PersistedModel.include documentation.
|
|
|
|
* <br/>See [Include filter](http://docs.strongloop.com/display/LB/Include+filter).
|
|
|
|
* @property {Number} limit Maximum number of instances to return.
|
|
|
|
* <br/>See [Limit filter](http://docs.strongloop.com/display/LB/Limit+filter).
|
|
|
|
* @property {String} order Sort order: either "ASC" for ascending or "DESC" for descending.
|
|
|
|
* <br/>See [Order filter](http://docs.strongloop.com/display/LB/Order+filter).
|
|
|
|
* @property {Number} skip Number of results to skip.
|
|
|
|
* <br/>See [Skip filter](http://docs.strongloop.com/display/LB/Skip+filter).
|
|
|
|
* @property {Object} where Where clause, like
|
|
|
|
* ```
|
|
|
|
* { key: val, key2: {gt: 'val2'}, ...}
|
|
|
|
* ```
|
|
|
|
* <br/>See [Where filter](http://docs.strongloop.com/display/LB/Where+filter).
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} callback Callback function called with `(err, returned-instances)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Array} models Model instances matching the filter, or null if none found.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.find = function find(params, cb) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'find');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Find one model instance that matches `filter` specification.
|
|
|
|
* Same as `find`, but limited to one result;
|
|
|
|
* Returns object, not collection.
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2014-10-15 07:07:30 +00:00
|
|
|
* @options {Object} [filter] Optional Filter JSON object; see below.
|
|
|
|
* @property {String|Object|Array} fields Identify fields to include in return result.
|
|
|
|
* <br/>See [Fields filter](http://docs.strongloop.com/display/LB/Fields+filter).
|
|
|
|
* @property {String|Object|Array} include See PersistedModel.include documentation.
|
|
|
|
* <br/>See [Include filter](http://docs.strongloop.com/display/LB/Include+filter).
|
|
|
|
* @property {String} order Sort order: either "ASC" for ascending or "DESC" for descending.
|
|
|
|
* <br/>See [Order filter](http://docs.strongloop.com/display/LB/Order+filter).
|
|
|
|
* @property {Number} skip Number of results to skip.
|
|
|
|
* <br/>See [Skip filter](http://docs.strongloop.com/display/LB/Skip+filter).
|
|
|
|
* @property {Object} where Where clause, like
|
|
|
|
* ```
|
|
|
|
* { key: val, key2: {gt: 'val2'}, ...}
|
|
|
|
* ```
|
|
|
|
* <br/>See [Where filter](http://docs.strongloop.com/display/LB/Where+filter).
|
2015-02-18 17:13:47 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} callback Callback function called with `(err, returned-instance)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Array} model First model instance that matches the filter or null if none found.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.findOne = function findOne(params, cb) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'findOne');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-01-23 01:01:31 +00:00
|
|
|
* Destroy all model instances that match the optional `where` specification.
|
2014-10-15 07:07:30 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Object} [where] Optional where filter, like:
|
2014-10-15 07:07:30 +00:00
|
|
|
* ```
|
|
|
|
* { key: val, key2: {gt: 'val2'}, ...}
|
|
|
|
* ```
|
2015-01-23 01:01:31 +00:00
|
|
|
* <br/>See [Where filter](http://docs.strongloop.com/display/LB/Where+filter).
|
2015-02-18 17:13:47 +00:00
|
|
|
*
|
|
|
|
* @callback {Function} callback Optional callback function called with `(err)` argument.
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.destroyAll = function destroyAll(where, cb) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'destroyAll');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
2014-10-15 07:07:30 +00:00
|
|
|
/**
|
|
|
|
* Alias for `destroyAll`
|
|
|
|
*/
|
|
|
|
PersistedModel.remove = PersistedModel.destroyAll;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for `destroyAll`
|
|
|
|
*/
|
|
|
|
PersistedModel.deleteAll = PersistedModel.destroyAll;
|
|
|
|
|
2014-06-21 06:08:35 +00:00
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Update multiple instances that match the where clause.
|
2014-06-21 06:08:35 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
*```js
|
2014-11-15 04:06:08 +00:00
|
|
|
* Employee.updateAll({managerId: 'x001'}, {managerId: 'x002'}, function(err, count) {
|
2014-06-21 06:08:35 +00:00
|
|
|
* ...
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Object} [where] Optional `where` filter, like
|
2014-10-15 07:07:30 +00:00
|
|
|
* ```
|
|
|
|
* { key: val, key2: {gt: 'val2'}, ...}
|
|
|
|
* ```
|
2015-01-23 01:01:31 +00:00
|
|
|
* <br/>see [Where filter](http://docs.strongloop.com/display/public/LB/Where+filter).
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Object} data Object containing data to replace matching instances, if any.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback Callback function called with `(err, count)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Number} count Number of instances updated.
|
2015-02-18 17:13:47 +00:00
|
|
|
*
|
2014-06-21 06:08:35 +00:00
|
|
|
*/
|
2014-10-15 07:07:30 +00:00
|
|
|
PersistedModel.updateAll = function updateAll(where, data, cb) {
|
|
|
|
throwNotAttached(this.modelName, 'updateAll');
|
|
|
|
};
|
|
|
|
|
2014-10-15 20:40:14 +00:00
|
|
|
/**
|
|
|
|
* Alias for updateAll.
|
|
|
|
*/
|
2014-10-15 07:07:30 +00:00
|
|
|
PersistedModel.update = PersistedModel.updateAll;
|
2014-06-21 06:08:35 +00:00
|
|
|
|
2014-02-20 01:09:36 +00:00
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Destroy model instance with the specified ID.
|
|
|
|
* @param {*} id The ID value of model instance to delete.
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} callback Callback function called with `(err)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.destroyById = function deleteById(id, cb) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'deleteById');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Alias for destroyById.
|
|
|
|
*/
|
2014-10-16 22:54:40 +00:00
|
|
|
PersistedModel.removeById = PersistedModel.destroyById;
|
2014-10-15 07:07:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for destroyById.
|
|
|
|
*/
|
|
|
|
PersistedModel.deleteById = PersistedModel.destroyById;
|
|
|
|
|
|
|
|
/**
|
2015-01-23 01:01:31 +00:00
|
|
|
* Return the number of records that match the optional "where" filter.
|
2015-02-10 23:13:55 +00:00
|
|
|
* @parame {Object} [where] Optional where filter, like
|
2014-10-15 07:07:30 +00:00
|
|
|
* ```
|
|
|
|
* { key: val, key2: {gt: 'val2'}, ...}
|
|
|
|
* ```
|
2015-02-10 23:13:55 +00:00
|
|
|
* <br/>See [Where filter](http://docs.strongloop.com/display/LB/Where+filter).
|
|
|
|
* @callback {Function} callback Callback function called with `(err, count)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Number} count Number of instances updated.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
PersistedModel.count = function(where, cb) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'count');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Save model instance. If the instance doesn't have an ID, then calls [create](#persistedmodelcreatedata-cb) instead.
|
2014-10-15 07:07:30 +00:00
|
|
|
* Triggers: validate, save, update, or create.
|
|
|
|
* @options {Object} [options] See below.
|
2015-01-23 17:54:14 +00:00
|
|
|
* @property {Boolean} validate Perform validation before saving. Default is true.
|
2015-02-10 23:13:55 +00:00
|
|
|
* @property {Boolean} throws If true, throw a validation error; WARNING: This can crash Node.
|
2015-01-23 17:54:14 +00:00
|
|
|
* If false, report the error via callback. Default is false.
|
2015-02-18 17:13:47 +00:00
|
|
|
* @callback {Function} callback Optional callback function called with `(err, obj)` arguments.
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
2015-02-18 17:13:47 +00:00
|
|
|
* @param {Object} instance Model instance saved or created.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
PersistedModel.prototype.save = function(options, callback) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var Model = this.constructor;
|
|
|
|
|
|
|
|
if (typeof options == 'function') {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
callback = callback || function() {
|
2014-05-06 20:31:23 +00:00
|
|
|
};
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (!('validate' in options)) {
|
|
|
|
options.validate = true;
|
|
|
|
}
|
|
|
|
if (!('throws' in options)) {
|
|
|
|
options.throws = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var inst = this;
|
|
|
|
var data = inst.toObject(true);
|
|
|
|
var id = this.getId();
|
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
return Model.create(this, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate first
|
|
|
|
if (!options.validate) {
|
|
|
|
return save();
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
inst.isValid(function(valid) {
|
2014-05-06 20:31:23 +00:00
|
|
|
if (valid) {
|
|
|
|
save();
|
|
|
|
} else {
|
2014-10-15 14:42:46 +00:00
|
|
|
var err = new Model.ValidationError(inst);
|
2014-05-06 20:31:23 +00:00
|
|
|
// throws option is dangerous for async usage
|
|
|
|
if (options.throws) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
callback(err, inst);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// then save
|
|
|
|
function save() {
|
2014-10-30 20:49:47 +00:00
|
|
|
inst.trigger('save', function(saveDone) {
|
|
|
|
inst.trigger('update', function(updateDone) {
|
2014-05-06 20:31:23 +00:00
|
|
|
Model.upsert(inst, function(err) {
|
|
|
|
inst._initProperties(data);
|
2014-10-30 20:49:47 +00:00
|
|
|
updateDone.call(inst, function() {
|
|
|
|
saveDone.call(inst, function() {
|
2014-05-06 20:31:23 +00:00
|
|
|
callback(err, inst);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, data);
|
|
|
|
}, data);
|
|
|
|
}
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the data model is new.
|
2014-10-15 07:07:30 +00:00
|
|
|
* @returns {Boolean} Returns true if the data model is new; false otherwise.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
PersistedModel.prototype.isNewRecord = function() {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.constructor.modelName, 'isNewRecord');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Deletes the model from persistence.
|
|
|
|
* Triggers `destroy` hook (async) before and after destroying object.
|
|
|
|
* @param {Function} callback Callback function.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
PersistedModel.prototype.destroy = function(cb) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.constructor.modelName, 'destroy');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
2014-10-15 07:07:30 +00:00
|
|
|
/**
|
|
|
|
* Alias for destroy.
|
|
|
|
* @header PersistedModel.remove
|
|
|
|
*/
|
|
|
|
PersistedModel.prototype.remove = PersistedModel.prototype.destroy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for destroy.
|
|
|
|
* @header PersistedModel.delete
|
|
|
|
*/
|
|
|
|
PersistedModel.prototype.delete = PersistedModel.prototype.destroy;
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.prototype.destroy._delegate = true;
|
2014-05-03 01:12:24 +00:00
|
|
|
|
2014-02-20 01:09:36 +00:00
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Update a single attribute.
|
|
|
|
* Equivalent to `updateAttributes({name: 'value'}, cb)`
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {String} name Name of property.
|
|
|
|
* @param {Mixed} value Value of property.
|
|
|
|
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Object} instance Updated instance.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.prototype.updateAttribute = function updateAttribute(name, value, callback) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.constructor.modelName, 'updateAttribute');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Update set of attributes. Performs validation before updating.
|
2014-02-20 01:09:36 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* Triggers: `validation`, `save` and `update` hooks
|
2014-10-15 07:07:30 +00:00
|
|
|
* @param {Object} data Dta to update.
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Object} instance Updated instance.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.prototype.updateAttributes = function updateAttributes(data, cb) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.modelName, 'updateAttributes');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Reload object from persistence. Requires `id` member of `object` to be able to call `find`.
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Object} instance Model instance.
|
2014-02-20 01:09:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.prototype.reload = function reload(callback) {
|
2014-02-21 17:10:48 +00:00
|
|
|
throwNotAttached(this.constructor.modelName, 'reload');
|
2014-02-20 01:09:36 +00:00
|
|
|
};
|
2014-04-14 21:17:56 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Set the correct `id` property for the `PersistedModel`. Uses the `setId` method if the model is attached to
|
|
|
|
* connector that defines it. Otherwise, uses the default lookup.
|
2014-10-15 07:07:30 +00:00
|
|
|
* Override this method to handle complex IDs.
|
2014-04-14 21:17:56 +00:00
|
|
|
*
|
2014-10-15 07:07:30 +00:00
|
|
|
* @param {*} val The `id` value. Will be converted to the type that the `id` property specifies.
|
2014-04-14 21:17:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.prototype.setId = function(val) {
|
2014-04-14 21:17:56 +00:00
|
|
|
var ds = this.getDataSource();
|
|
|
|
this[this.getIdName()] = val;
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-04-14 21:17:56 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
/**
|
2014-06-05 07:45:09 +00:00
|
|
|
* Get the `id` value for the `PersistedModel`.
|
2014-05-03 01:12:24 +00:00
|
|
|
*
|
|
|
|
* @returns {*} The `id` value
|
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.prototype.getId = function() {
|
2014-04-14 21:17:56 +00:00
|
|
|
var data = this.toObject();
|
2014-10-30 20:49:47 +00:00
|
|
|
if (!data) return;
|
2014-04-14 21:17:56 +00:00
|
|
|
return data[this.getIdName()];
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-04-14 21:17:56 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Get the `id` property name of the constructor.
|
2014-05-03 01:12:24 +00:00
|
|
|
*
|
|
|
|
* @returns {String} The `id` property name
|
2014-04-14 21:17:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.prototype.getIdName = function() {
|
2014-04-14 21:17:56 +00:00
|
|
|
return this.constructor.getIdName();
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-04-14 21:17:56 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Get the `id` property name of the constructor.
|
2014-05-03 01:12:24 +00:00
|
|
|
*
|
|
|
|
* @returns {String} The `id` property name
|
2014-04-14 21:17:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.getIdName = function() {
|
2014-04-14 21:17:56 +00:00
|
|
|
var Model = this;
|
|
|
|
var ds = Model.getDataSource();
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (ds.idName) {
|
2014-04-14 21:17:56 +00:00
|
|
|
return ds.idName(Model.modelName);
|
|
|
|
} else {
|
|
|
|
return 'id';
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-03 01:12:24 +00:00
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.setupRemoting = function() {
|
|
|
|
var PersistedModel = this;
|
|
|
|
var typeName = PersistedModel.modelName;
|
|
|
|
var options = PersistedModel.settings;
|
2014-05-03 01:12:24 +00:00
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
function setRemoting(scope, name, options) {
|
|
|
|
var fn = scope[name];
|
2014-05-20 20:45:47 +00:00
|
|
|
fn._delegate = true;
|
2014-06-05 07:45:09 +00:00
|
|
|
options.isStatic = scope === PersistedModel;
|
|
|
|
PersistedModel.remoteMethod(name, options);
|
2014-05-16 19:32:54 +00:00
|
|
|
}
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'create', {
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Create a new instance of the model and persist it into the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
|
|
|
|
returns: {arg: 'data', type: typeName, root: true},
|
|
|
|
http: {verb: 'post', path: '/'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'upsert', {
|
2014-09-10 16:35:02 +00:00
|
|
|
aliases: ['updateOrCreate'],
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Update an existing model instance or insert a new one into the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
|
|
|
|
returns: {arg: 'data', type: typeName, root: true},
|
|
|
|
http: {verb: 'put', path: '/'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'exists', {
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Check whether a model instance exists in the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'READ',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
|
|
|
|
returns: {arg: 'exists', type: 'boolean'},
|
2014-07-31 04:57:45 +00:00
|
|
|
http: [
|
|
|
|
{verb: 'get', path: '/:id/exists'},
|
|
|
|
{verb: 'head', path: '/:id'}
|
|
|
|
],
|
|
|
|
rest: {
|
|
|
|
// After hook to map exists to 200/404 for HEAD
|
|
|
|
after: function(ctx, cb) {
|
2014-10-22 21:39:39 +00:00
|
|
|
if (ctx.req.method === 'GET') {
|
|
|
|
// For GET, return {exists: true|false} as is
|
|
|
|
return cb();
|
|
|
|
}
|
2014-10-30 20:49:47 +00:00
|
|
|
if (!ctx.result.exists) {
|
2014-07-31 04:57:45 +00:00
|
|
|
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;
|
2014-12-18 20:26:27 +00:00
|
|
|
error.code = 'MODEL_NOT_FOUND';
|
2014-07-31 04:57:45 +00:00
|
|
|
cb(error);
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-03 01:12:24 +00:00
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'findById', {
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Find a model instance by id from the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'READ',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {
|
|
|
|
arg: 'id', type: 'any', description: 'Model id', required: true,
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
returns: {arg: 'data', type: typeName, root: true},
|
|
|
|
http: {verb: 'get', path: '/:id'},
|
|
|
|
rest: {after: convertNullToNotFoundError}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'find', {
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Find all instances of the model matched by filter from the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'READ',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'},
|
|
|
|
returns: {arg: 'data', type: [typeName], root: true},
|
|
|
|
http: {verb: 'get', path: '/'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'findOne', {
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Find first instance of the model matched by filter from the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'READ',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'},
|
|
|
|
returns: {arg: 'data', type: typeName, root: true},
|
2014-06-21 06:48:46 +00:00
|
|
|
http: {verb: 'get', path: '/findOne'},
|
|
|
|
rest: {after: convertNullToNotFoundError}
|
2014-05-03 01:12:24 +00:00
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'destroyAll', {
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Delete all matching records',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {arg: 'where', type: 'object', description: 'filter.where object'},
|
2014-05-16 00:27:02 +00:00
|
|
|
http: {verb: 'del', path: '/'},
|
|
|
|
shared: false
|
2014-05-03 01:12:24 +00:00
|
|
|
});
|
|
|
|
|
2014-06-21 06:08:35 +00:00
|
|
|
setRemoting(PersistedModel, 'updateAll', {
|
2014-09-10 16:35:02 +00:00
|
|
|
aliases: ['update'],
|
2014-06-21 06:08:35 +00:00
|
|
|
description: 'Update instances of the model matched by where from the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-06-21 06:08:35 +00:00
|
|
|
accepts: [
|
|
|
|
{arg: 'where', type: 'object', http: {source: 'query'},
|
|
|
|
description: 'Criteria to match model instances'},
|
|
|
|
{arg: 'data', type: 'object', http: {source: 'body'},
|
|
|
|
description: 'An object of model property name/value pairs'},
|
|
|
|
],
|
|
|
|
http: {verb: 'post', path: '/update'}
|
|
|
|
});
|
|
|
|
|
2014-07-16 16:09:07 +00:00
|
|
|
setRemoting(PersistedModel, 'deleteById', {
|
2014-09-10 16:35:02 +00:00
|
|
|
aliases: ['destroyById', 'removeById'],
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Delete a model instance by id from the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-03 03:04:06 +00:00
|
|
|
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true,
|
|
|
|
http: {source: 'path'}},
|
2014-05-03 01:12:24 +00:00
|
|
|
http: {verb: 'del', path: '/:id'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'count', {
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Count instances of the model matched by where from the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'READ',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {arg: 'where', type: 'object', description: 'Criteria to match model instances'},
|
|
|
|
returns: {arg: 'count', type: 'number'},
|
|
|
|
http: {verb: 'get', path: '/count'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel.prototype, 'updateAttributes', {
|
2014-05-03 01:12:24 +00:00
|
|
|
description: 'Update attributes for a model instance and persist it into the data source',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-03 01:12:24 +00:00
|
|
|
accepts: {arg: 'data', type: 'object', http: {source: 'body'}, description: 'An object of model property name/value pairs'},
|
|
|
|
returns: {arg: 'data', type: typeName, root: true},
|
|
|
|
http: {verb: 'put', path: '/'}
|
|
|
|
});
|
2014-05-06 20:31:23 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (options.trackChanges) {
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'diff', {
|
2014-05-06 20:31:23 +00:00
|
|
|
description: 'Get a set of deltas and conflicts since the given checkpoint',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'READ',
|
2014-05-06 20:31:23 +00:00
|
|
|
accepts: [
|
|
|
|
{arg: 'since', type: 'number', description: 'Find deltas since this checkpoint'},
|
|
|
|
{arg: 'remoteChanges', type: 'array', description: 'an array of change objects',
|
|
|
|
http: {source: 'body'}}
|
2014-10-16 22:54:40 +00:00
|
|
|
],
|
2014-05-10 00:19:32 +00:00
|
|
|
returns: {arg: 'result', type: 'object', root: true},
|
2014-05-06 20:31:23 +00:00
|
|
|
http: {verb: 'post', path: '/diff'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'changes', {
|
2014-10-30 20:49:47 +00:00
|
|
|
description: 'Get the changes to a model since a given checkpoint.' +
|
|
|
|
'Provide a filter object to reduce the number of results returned.',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'READ',
|
2014-05-06 20:31:23 +00:00
|
|
|
accepts: [
|
|
|
|
{arg: 'since', type: 'number', description: 'Only return changes since this checkpoint'},
|
|
|
|
{arg: 'filter', type: 'object', description: 'Only include changes that match this filter'}
|
|
|
|
],
|
|
|
|
returns: {arg: 'changes', type: 'array', root: true},
|
|
|
|
http: {verb: 'get', path: '/changes'}
|
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'checkpoint', {
|
2014-05-06 20:31:23 +00:00
|
|
|
description: 'Create a checkpoint.',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-06 20:31:23 +00:00
|
|
|
returns: {arg: 'checkpoint', type: 'object', root: true},
|
|
|
|
http: {verb: 'post', path: '/checkpoint'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'currentCheckpoint', {
|
2014-05-06 20:31:23 +00:00
|
|
|
description: 'Get the current checkpoint.',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'READ',
|
2014-05-06 20:31:23 +00:00
|
|
|
returns: {arg: 'checkpoint', type: 'object', root: true},
|
|
|
|
http: {verb: 'get', path: '/checkpoint'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'createUpdates', {
|
2014-05-10 00:19:32 +00:00
|
|
|
description: 'Create an update list from a delta list',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-10 00:19:32 +00:00
|
|
|
accepts: {arg: 'deltas', type: 'array', http: {source: 'body'}},
|
|
|
|
returns: {arg: 'updates', type: 'array', root: true},
|
|
|
|
http: {verb: 'post', path: '/create-updates'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'bulkUpdate', {
|
2014-05-06 20:31:23 +00:00
|
|
|
description: 'Run multiple updates at once. Note: this is not atomic.',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-06 20:31:23 +00:00
|
|
|
accepts: {arg: 'updates', type: 'array'},
|
|
|
|
http: {verb: 'post', path: '/bulk-update'}
|
|
|
|
});
|
2014-05-10 00:19:32 +00:00
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'rectifyAllChanges', {
|
2014-05-10 00:19:32 +00:00
|
|
|
description: 'Rectify all Model changes.',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-10 00:19:32 +00:00
|
|
|
http: {verb: 'post', path: '/rectify-all'}
|
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
setRemoting(PersistedModel, 'rectifyChange', {
|
2014-05-10 00:19:32 +00:00
|
|
|
description: 'Tell loopback that a change to the model with the given id has occurred.',
|
2014-12-12 16:50:17 +00:00
|
|
|
accessType: 'WRITE',
|
2014-05-10 00:19:32 +00:00
|
|
|
accepts: {arg: 'id', type: 'any', http: {source: 'path'}},
|
|
|
|
http: {verb: 'post', path: '/:id/rectify-change'}
|
|
|
|
});
|
2014-05-06 20:31:23 +00:00
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a set of deltas and conflicts since the given checkpoint.
|
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* See [Change.diff()](#change-diff) for details.
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Number} since Find deltas since this checkpoint.
|
|
|
|
* @param {Array} remoteChanges An array of change objects.
|
|
|
|
* @callback {Function} callback Callback function called with `(err, result)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Object} result Object with `deltas` and `conflicts` properties; see [Change.diff()](#change-diff) for details.
|
2014-05-06 20:31:23 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.diff = function(since, remoteChanges, callback) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var Change = this.getChangeModel();
|
|
|
|
Change.diff(this.modelName, since, remoteChanges, callback);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Get the changes to a model since the specified checkpoint. Provide a filter object
|
2014-05-06 20:31:23 +00:00
|
|
|
* to reduce the number of results returned.
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Number} since Return only changes since this checkpoint.
|
|
|
|
* @param {Object} filter Include only changes that match this filter, the same as for [#persistedmodel-find](find()).
|
|
|
|
* @callback {Function} callback Callback function called with `(err, changes)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Array} changes An array of [Change](#change) objects.
|
2014-05-06 20:31:23 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.changes = function(since, filter, callback) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (typeof since === 'function') {
|
2014-05-10 00:19:32 +00:00
|
|
|
filter = {};
|
|
|
|
callback = since;
|
|
|
|
since = -1;
|
|
|
|
}
|
2014-10-30 20:49:47 +00:00
|
|
|
if (typeof filter === 'function') {
|
2014-05-14 16:20:04 +00:00
|
|
|
callback = filter;
|
|
|
|
since = -1;
|
|
|
|
filter = {};
|
|
|
|
}
|
2014-05-10 00:19:32 +00:00
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
var idName = this.dataSource.idName(this.modelName);
|
|
|
|
var Change = this.getChangeModel();
|
|
|
|
var model = this;
|
|
|
|
|
|
|
|
filter = filter || {};
|
|
|
|
filter.fields = {};
|
|
|
|
filter.where = filter.where || {};
|
|
|
|
filter.fields[idName] = true;
|
|
|
|
|
2014-05-10 00:19:32 +00:00
|
|
|
// TODO(ritch) this whole thing could be optimized a bit more
|
2015-02-20 18:28:33 +00:00
|
|
|
Change.find({ where: {
|
2015-03-03 10:23:13 +00:00
|
|
|
checkpoint: { gte: since },
|
2014-05-06 20:31:23 +00:00
|
|
|
modelName: this.modelName
|
2015-02-20 18:28:33 +00:00
|
|
|
}}, function(err, changes) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (err) return callback(err);
|
2015-01-10 18:28:55 +00:00
|
|
|
if (!Array.isArray(changes) || changes.length === 0) return callback(null, []);
|
2014-05-06 20:31:23 +00:00
|
|
|
var ids = changes.map(function(change) {
|
2014-05-10 00:19:32 +00:00
|
|
|
return change.getModelId();
|
2014-05-06 20:31:23 +00:00
|
|
|
});
|
|
|
|
filter.where[idName] = {inq: ids};
|
|
|
|
model.find(filter, function(err, models) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (err) return callback(err);
|
2014-05-06 20:31:23 +00:00
|
|
|
var modelIds = models.map(function(m) {
|
|
|
|
return m[idName].toString();
|
|
|
|
});
|
|
|
|
callback(null, changes.filter(function(ch) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (ch.type() === Change.DELETE) return true;
|
2014-05-06 20:31:23 +00:00
|
|
|
return modelIds.indexOf(ch.modelId) > -1;
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a checkpoint.
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-05-06 20:31:23 +00:00
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.checkpoint = function(cb) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var Checkpoint = this.getChangeModel().getCheckpointModel();
|
|
|
|
this.getSourceId(function(err, sourceId) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (err) return cb(err);
|
2014-05-06 20:31:23 +00:00
|
|
|
Checkpoint.create({
|
|
|
|
sourceId: sourceId
|
|
|
|
}, cb);
|
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Get the current checkpoint ID.
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} callback Callback function called with `(err, currentCheckpointId)` arguments. Required.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Number} currentCheckpointId Current checkpoint ID.
|
2014-05-06 20:31:23 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.currentCheckpoint = function(cb) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var Checkpoint = this.getChangeModel().getCheckpointModel();
|
|
|
|
Checkpoint.current(cb);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replicate changes since the given checkpoint to the given target model.
|
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Number} [since] Since this checkpoint
|
2014-05-06 20:31:23 +00:00
|
|
|
* @param {Model} targetModel Target this model class
|
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {Object} [options.filter] Replicate models that match this filter
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} [callback] Callback function called with `(err, conflicts)` arguments.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {Conflict[]} conflicts A list of changes that could not be replicated due to conflicts.
|
2014-05-06 20:31:23 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.replicate = function(since, targetModel, options, callback) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var lastArg = arguments[arguments.length - 1];
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (typeof lastArg === 'function' && arguments.length > 1) {
|
2014-05-06 20:31:23 +00:00
|
|
|
callback = lastArg;
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (typeof since === 'function' && since.modelName) {
|
2014-05-06 20:31:23 +00:00
|
|
|
targetModel = since;
|
|
|
|
since = -1;
|
|
|
|
}
|
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
if (typeof since !== 'object') {
|
|
|
|
since = { source: since, target: since };
|
|
|
|
}
|
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var sourceModel = this;
|
|
|
|
var diff;
|
|
|
|
var updates;
|
|
|
|
var Change = this.getChangeModel();
|
|
|
|
var TargetChange = targetModel.getChangeModel();
|
|
|
|
var changeTrackingEnabled = Change && TargetChange;
|
|
|
|
|
|
|
|
assert(
|
|
|
|
changeTrackingEnabled,
|
|
|
|
'You must enable change tracking before replicating'
|
|
|
|
);
|
|
|
|
|
|
|
|
callback = callback || function defaultReplicationCallback(err) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (err) throw err;
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
var tasks = [
|
2015-03-03 11:00:47 +00:00
|
|
|
checkpoints,
|
2014-05-14 16:20:04 +00:00
|
|
|
getSourceChanges,
|
2014-05-06 20:31:23 +00:00
|
|
|
getDiffFromTarget,
|
|
|
|
createSourceUpdates,
|
2015-03-03 10:50:06 +00:00
|
|
|
bulkUpdate
|
2014-05-06 20:31:23 +00:00
|
|
|
];
|
|
|
|
|
2014-05-14 16:20:04 +00:00
|
|
|
async.waterfall(tasks, done);
|
2014-05-06 20:31:23 +00:00
|
|
|
|
2014-05-14 16:20:04 +00:00
|
|
|
function getSourceChanges(cb) {
|
2015-03-03 10:23:13 +00:00
|
|
|
sourceModel.changes(since.source, options.filter, cb);
|
2014-05-06 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getDiffFromTarget(sourceChanges, cb) {
|
2015-03-03 10:23:13 +00:00
|
|
|
targetModel.diff(since.target, sourceChanges, cb);
|
2014-05-06 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createSourceUpdates(_diff, cb) {
|
|
|
|
diff = _diff;
|
|
|
|
diff.conflicts = diff.conflicts || [];
|
2014-10-30 20:49:47 +00:00
|
|
|
if (diff && diff.deltas && diff.deltas.length) {
|
2014-05-06 20:31:23 +00:00
|
|
|
sourceModel.createUpdates(diff.deltas, cb);
|
|
|
|
} else {
|
2014-05-10 00:19:32 +00:00
|
|
|
// nothing to replicate
|
2014-05-14 16:20:04 +00:00
|
|
|
done();
|
2014-05-06 20:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bulkUpdate(updates, cb) {
|
|
|
|
targetModel.bulkUpdate(updates, cb);
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:00:47 +00:00
|
|
|
function checkpoints() {
|
2014-05-06 20:31:23 +00:00
|
|
|
var cb = arguments[arguments.length - 1];
|
2015-03-03 11:00:47 +00:00
|
|
|
sourceModel.checkpoint(function(err) {
|
|
|
|
targetModel.checkpoint(function(err) {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
});
|
2014-05-06 20:31:23 +00:00
|
|
|
}
|
2014-05-14 16:20:04 +00:00
|
|
|
|
|
|
|
function done(err) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (err) return callback(err);
|
2014-05-14 16:20:04 +00:00
|
|
|
|
|
|
|
var conflicts = diff.conflicts.map(function(change) {
|
|
|
|
return new Change.Conflict(
|
|
|
|
change.modelId, sourceModel, targetModel
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (conflicts.length) {
|
2014-05-14 16:20:04 +00:00
|
|
|
sourceModel.emit('conflicts', conflicts);
|
|
|
|
}
|
|
|
|
|
2014-11-04 07:13:21 +00:00
|
|
|
if (callback) callback(null, conflicts);
|
2014-05-14 16:20:04 +00:00
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an update list (for `Model.bulkUpdate()`) from a delta list
|
|
|
|
* (result of `Change.diff()`).
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
|
|
|
* @param {Array} deltas
|
2014-05-06 20:31:23 +00:00
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.createUpdates = function(deltas, cb) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var Change = this.getChangeModel();
|
|
|
|
var updates = [];
|
|
|
|
var Model = this;
|
|
|
|
var tasks = [];
|
|
|
|
|
|
|
|
deltas.forEach(function(change) {
|
2014-11-04 07:13:21 +00:00
|
|
|
change = new Change(change);
|
2014-05-06 20:31:23 +00:00
|
|
|
var type = change.type();
|
|
|
|
var update = {type: type, change: change};
|
2014-10-30 20:49:47 +00:00
|
|
|
switch (type) {
|
2014-05-06 20:31:23 +00:00
|
|
|
case Change.CREATE:
|
|
|
|
case Change.UPDATE:
|
|
|
|
tasks.push(function(cb) {
|
|
|
|
Model.findById(change.modelId, function(err, inst) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (err) return cb(err);
|
|
|
|
if (!inst) {
|
2014-05-12 17:36:10 +00:00
|
|
|
console.error('missing data for change:', change);
|
2014-10-30 20:49:47 +00:00
|
|
|
return cb &&
|
|
|
|
cb(new Error('missing data for change: ' + change.modelId));
|
2014-05-12 17:36:10 +00:00
|
|
|
}
|
2014-10-30 20:49:47 +00:00
|
|
|
if (inst.toObject) {
|
2014-05-06 20:31:23 +00:00
|
|
|
update.data = inst.toObject();
|
|
|
|
} else {
|
|
|
|
update.data = inst;
|
|
|
|
}
|
|
|
|
updates.push(update);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
2014-10-30 20:49:47 +00:00
|
|
|
break;
|
2014-05-06 20:31:23 +00:00
|
|
|
case Change.DELETE:
|
|
|
|
updates.push(update);
|
2014-10-30 20:49:47 +00:00
|
|
|
break;
|
2014-05-06 20:31:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async.parallel(tasks, function(err) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (err) return cb(err);
|
2014-05-06 20:31:23 +00:00
|
|
|
cb(null, updates);
|
|
|
|
});
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply an update list.
|
|
|
|
*
|
|
|
|
* **Note: this is not atomic**
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Array} updates An updates list, usually from [createUpdates()](#persistedmodel-createupdates).
|
|
|
|
* @param {Function} callback Callback function.
|
2014-05-06 20:31:23 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.bulkUpdate = function(updates, callback) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var tasks = [];
|
|
|
|
var Model = this;
|
|
|
|
var idName = this.dataSource.idName(this.modelName);
|
|
|
|
var Change = this.getChangeModel();
|
|
|
|
|
|
|
|
updates.forEach(function(update) {
|
2014-10-30 20:49:47 +00:00
|
|
|
switch (update.type) {
|
2014-05-06 20:31:23 +00:00
|
|
|
case Change.UPDATE:
|
|
|
|
case Change.CREATE:
|
|
|
|
// var model = new Model(update.data);
|
|
|
|
// tasks.push(model.save.bind(model));
|
|
|
|
tasks.push(function(cb) {
|
|
|
|
var model = new Model(update.data);
|
|
|
|
model.save(cb);
|
|
|
|
});
|
2014-10-30 20:49:47 +00:00
|
|
|
break;
|
2014-05-06 20:31:23 +00:00
|
|
|
case Change.DELETE:
|
|
|
|
var data = {};
|
|
|
|
data[idName] = update.change.modelId;
|
|
|
|
var model = new Model(data);
|
|
|
|
tasks.push(model.destroy.bind(model));
|
2014-10-30 20:49:47 +00:00
|
|
|
break;
|
2014-05-06 20:31:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async.parallel(tasks, callback);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the `Change` model.
|
2015-02-10 23:13:55 +00:00
|
|
|
* Throws an error if the change model is not correctly setup.
|
2014-05-06 20:31:23 +00:00
|
|
|
* @return {Change}
|
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.getChangeModel = function() {
|
2014-05-06 20:31:23 +00:00
|
|
|
var changeModel = this.Change;
|
|
|
|
var isSetup = changeModel && changeModel.dataSource;
|
|
|
|
|
|
|
|
assert(isSetup, 'Cannot get a setup Change model');
|
|
|
|
|
|
|
|
return changeModel;
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Get the source identifier for this model or dataSource.
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @callback {Function} callback Callback function called with `(err, id)` arguments.
|
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
|
|
|
* @param {String} sourceId Source identifier for the model or dataSource.
|
2014-05-06 20:31:23 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.getSourceId = function(cb) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var dataSource = this.dataSource;
|
2014-10-30 20:49:47 +00:00
|
|
|
if (!dataSource) {
|
2014-05-06 20:31:23 +00:00
|
|
|
this.once('dataSourceAttached', this.getSourceId.bind(this, cb));
|
|
|
|
}
|
|
|
|
assert(
|
2014-10-16 22:54:40 +00:00
|
|
|
dataSource.connector.name,
|
2014-05-06 20:31:23 +00:00
|
|
|
'Model.getSourceId: cannot get id without dataSource.connector.name'
|
|
|
|
);
|
|
|
|
var id = [dataSource.connector.name, this.modelName].join('-');
|
|
|
|
cb(null, id);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable the tracking of changes made to the model. Usually for replication.
|
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.enableChangeTracking = function() {
|
2014-05-06 20:31:23 +00:00
|
|
|
var Model = this;
|
|
|
|
var Change = this.Change || this._defineChangeModel();
|
|
|
|
var cleanupInterval = Model.settings.changeCleanupInterval || 30000;
|
|
|
|
|
2015-02-20 14:31:15 +00:00
|
|
|
assert(this.dataSource, 'Cannot enableChangeTracking(): ' + this.modelName +
|
|
|
|
' is not attached to a dataSource');
|
2014-05-06 20:31:23 +00:00
|
|
|
|
2015-02-26 15:29:29 +00:00
|
|
|
var idName = this.getIdName();
|
|
|
|
var idProp = this.definition.properties[idName];
|
|
|
|
var idType = idProp && idProp.type;
|
|
|
|
var idDefn = idProp && idProp.defaultFn;
|
|
|
|
if (idType !== String || !(idDefn === 'uuid' || idDefn === 'guid')) {
|
|
|
|
deprecated('The model ' + this.modelName + ' is tracking changes, ' +
|
|
|
|
'which requries a string id with GUID/UUID default value.');
|
|
|
|
}
|
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
Change.attachTo(this.dataSource);
|
|
|
|
Change.getCheckpointModel().attachTo(this.dataSource);
|
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
Model.observe('after save', rectifyOnSave);
|
2014-05-06 20:31:23 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
Model.observe('after delete', rectifyOnDelete);
|
2014-05-06 20:31:23 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (runtime.isServer) {
|
2014-05-10 00:19:32 +00:00
|
|
|
// initial cleanup
|
|
|
|
cleanup();
|
2014-05-06 20:31:23 +00:00
|
|
|
|
2014-05-10 00:19:32 +00:00
|
|
|
// cleanup
|
|
|
|
setInterval(cleanup, cleanupInterval);
|
2014-11-04 07:13:21 +00:00
|
|
|
}
|
2014-05-06 20:31:23 +00:00
|
|
|
|
2014-11-04 07:13:21 +00:00
|
|
|
function cleanup() {
|
|
|
|
Model.rectifyAllChanges(function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(Model.modelName + ' Change Cleanup Error:');
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
});
|
2014-05-06 20:31:23 +00:00
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
function rectifyOnSave(ctx, next) {
|
|
|
|
if (ctx.instance) {
|
|
|
|
ctx.Model.rectifyChange(ctx.instance.getId(), reportErrorAndNext);
|
|
|
|
} else {
|
|
|
|
ctx.Model.rectifyAllChanges(reportErrorAndNext);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportErrorAndNext(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(
|
|
|
|
ctx.Model.modelName + '.rectifyChange(s) after save failed:' + err);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function rectifyOnDelete(ctx, next) {
|
|
|
|
var id = getIdFromWhereByModelId(ctx.Model, ctx.where);
|
|
|
|
if (id) {
|
|
|
|
ctx.Model.rectifyChange(id, reportErrorAndNext);
|
|
|
|
} else {
|
|
|
|
ctx.Model.rectifyAllChanges(reportErrorAndNext);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportErrorAndNext(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(
|
|
|
|
ctx.Model.modelName + '.rectifyChange(s) after delete failed:' + err);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIdFromWhereByModelId(Model, where) {
|
|
|
|
var whereKeys = Object.keys(where);
|
|
|
|
if (whereKeys.length != 1) return undefined;
|
|
|
|
|
|
|
|
var idName = Model.getIdName();
|
|
|
|
if (whereKeys[0] !== idName) return undefined;
|
|
|
|
|
|
|
|
var id = where[idName];
|
|
|
|
// TODO(bajtos) support object values that are not LB conditions
|
|
|
|
if (typeof id === 'string' || typeof id === 'number') {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel._defineChangeModel = function() {
|
2014-10-15 14:42:46 +00:00
|
|
|
var BaseChangeModel = registry.getModel('Change');
|
2014-10-13 09:58:14 +00:00
|
|
|
assert(BaseChangeModel,
|
|
|
|
'Change model must be defined before enabling change replication');
|
|
|
|
|
2014-11-04 07:13:21 +00:00
|
|
|
this.Change = BaseChangeModel.extend(this.modelName + '-change',
|
2014-05-10 00:19:32 +00:00
|
|
|
{},
|
|
|
|
{
|
2014-05-14 16:20:04 +00:00
|
|
|
trackModel: this
|
2014-05-10 00:19:32 +00:00
|
|
|
}
|
|
|
|
);
|
2014-11-04 07:13:21 +00:00
|
|
|
|
|
|
|
return this.Change;
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-10 00:19:32 +00:00
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.rectifyAllChanges = function(callback) {
|
2014-05-10 00:19:32 +00:00
|
|
|
this.getChangeModel().rectifyAll(callback);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-10 00:19:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a change error. Override this method in a subclassing model to customize
|
|
|
|
* change error handling.
|
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
|
2014-05-10 00:19:32 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.handleChangeError = function(err) {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (err) {
|
2014-05-10 00:19:32 +00:00
|
|
|
console.error(Model.modelName + ' Change Tracking Error:');
|
|
|
|
console.error(err);
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-10 00:19:32 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-10 23:13:55 +00:00
|
|
|
* Specify that a change to the model with the given ID has occurred.
|
2014-05-10 00:19:32 +00:00
|
|
|
*
|
2015-02-10 23:13:55 +00:00
|
|
|
* @param {*} id The ID of the model that has changed.
|
2014-05-10 00:19:32 +00:00
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.rectifyChange = function(id, callback) {
|
2014-05-10 00:19:32 +00:00
|
|
|
var Change = this.getChangeModel();
|
|
|
|
Change.rectifyModelChanges(this.modelName, [id], callback);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-03 01:12:24 +00:00
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
PersistedModel.setup();
|