loopback-datasource-juggler/lib/dao.js

2179 lines
61 KiB
JavaScript
Raw Normal View History

2014-05-05 14:23:12 +00:00
2014-03-12 23:28:46 +00:00
/*!
* Module exports class Model
*/
module.exports = DataAccessObject;
2014-03-12 23:28:46 +00:00
/*!
* Module dependencies
*/
var async = require('async');
2013-05-28 05:20:30 +00:00
var jutil = require('./jutil');
var ValidationError = require('./validations').ValidationError;
2013-06-05 21:33:52 +00:00
var Relation = require('./relations.js');
2014-02-14 07:42:21 +00:00
var Inclusion = require('./include.js');
var List = require('./list.js');
2013-06-26 03:31:00 +00:00
var geo = require('./geo');
2013-07-23 21:40:44 +00:00
var Memory = require('./connectors/memory').Memory;
var utils = require('./utils');
var fieldsToArray = utils.fieldsToArray;
var removeUndefined = utils.removeUndefined;
var setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
var mergeQuery = utils.mergeQuery;
var util = require('util');
var assert = require('assert');
2015-01-08 14:44:28 +00:00
var BaseModel = require('./model');
var debug = require('debug')('loopback:dao');
/**
2014-03-12 23:28:46 +00:00
* Base class for all persistent objects.
* Provides a common API to access any database connector.
2014-05-22 00:50:44 +00:00
* This class describes only abstract behavior. Refer to the specific connector for additional details.
*
2014-03-12 23:28:46 +00:00
* `DataAccessObject` mixes `Inclusion` classes methods.
* @class DataAccessObject
*/
function DataAccessObject() {
2014-01-24 17:09:53 +00:00
if (DataAccessObject._mixins) {
var self = this;
var args = arguments;
DataAccessObject._mixins.forEach(function (m) {
m.call(self, args);
});
}
}
function idName(m) {
2014-09-04 16:32:38 +00:00
return m.definition.idName() || 'id';
}
function getIdValue(m, data) {
2014-09-04 16:32:38 +00:00
return data && data[idName(m)];
}
function setIdValue(m, data, value) {
2014-01-24 17:09:53 +00:00
if (data) {
data[idName(m)] = value;
}
}
2014-09-06 17:24:30 +00:00
function byIdQuery(m, id) {
var pk = idName(m);
var query = { where: {} };
query.where[pk] = id;
return query;
}
function isWhereByGivenId(Model, where, idValue) {
var keys = Object.keys(where);
if (keys.length != 1) return false;
var pk = idName(Model);
if (keys[0] !== pk) return false;
return where[pk] === idValue;
}
DataAccessObject._forDB = function (data) {
2014-01-24 17:09:53 +00:00
if (!(this.getDataSource().isRelational && this.getDataSource().isRelational())) {
return data;
}
var res = {};
for (var propName in data) {
var type = this.getPropertyType(propName);
if (type === 'JSON' || type === 'Any' || type === 'Object' || data[propName] instanceof Array) {
res[propName] = JSON.stringify(data[propName]);
} else {
res[propName] = data[propName];
}
2014-01-24 17:09:53 +00:00
}
return res;
};
DataAccessObject.defaultScope = function(target, inst) {
2014-09-06 17:44:58 +00:00
var scope = this.definition.settings.scope;
if (typeof scope === 'function') {
scope = this.definition.settings.scope.call(this, target, inst);
}
2014-09-06 17:44:58 +00:00
return scope;
};
DataAccessObject.applyScope = function(query, inst) {
var scope = this.defaultScope(query, inst) || {};
if (typeof scope === 'object') {
2014-12-03 23:07:35 +00:00
mergeQuery(query, scope || {}, this.definition.settings.scope);
}
};
DataAccessObject.applyProperties = function(data, inst) {
var properties = this.definition.settings.properties;
properties = properties || this.definition.settings.attributes;
if (typeof properties === 'object') {
util._extend(data, properties);
} else if (typeof properties === 'function') {
util._extend(data, properties.call(this, data, inst) || {});
} else if (properties !== false) {
var scope = this.defaultScope(data, inst) || {};
if (typeof scope.where === 'object') {
setScopeValuesFromWhere(data, scope.where, this);
}
2014-09-06 17:44:58 +00:00
}
};
DataAccessObject.lookupModel = function(data) {
return this;
};
// Empty callback function
function noCallback(err, result) {
// NOOP
debug('callback is ignored: err=%j, result=%j', err, result);
}
/**
2014-05-22 00:50:44 +00:00
* Create an instance of Model with given data and save to the attached data source. Callback is optional.
* Example:
*```js
* User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
* console.log(user instanceof User); // true
* });
* ```
* Note: You must include a callback and use the created model provided in the callback if your code depends on your model being
* saved or having an ID.
*
* @param {Object} [data] Optional data object
* @param {Object} [options] Options for create
* @param {Function} [cb] Callback function called with these arguments:
* - err (null or Error)
* - instance (null or Model)
*/
DataAccessObject.create = function (data, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
2014-01-24 17:09:53 +00:00
var Model = this;
var self = this;
if (options === undefined && cb === undefined) {
if (typeof data === 'function') {
// create(cb)
cb = data;
data = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
// create(data, cb);
cb = options;
options = {};
}
2014-01-24 17:09:53 +00:00
}
data = data || {};
options = options || {};
cb = cb || (Array.isArray(data) ? noCallback : utils.createPromiseCallback());
2014-01-24 17:09:53 +00:00
assert(typeof data === 'object', 'The data argument must be an object or array');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
2014-01-24 17:09:53 +00:00
var hookState = {};
2014-01-24 17:09:53 +00:00
if (Array.isArray(data)) {
// Undefined item will be skipped by async.map() which internally uses
// Array.prototype.map(). The following loop makes sure all items are
// iterated
for (var i = 0, n = data.length; i < n; i++) {
if (data[i] === undefined) {
data[i] = {};
}
}
async.map(data, function(item, done) {
self.create(item, options, function(err, result) {
// Collect all errors and results
done(null, {err: err, result: result || item});
});
}, function(err, results) {
if (err) {
return cb(err, results);
}
// Convert the results into two arrays
var errors = null;
var data = [];
for (var i = 0, n = results.length; i < n; i++) {
if (results[i].err) {
if (!errors) {
errors = [];
2014-01-24 17:09:53 +00:00
}
errors[i] = results[i].err;
}
data[i] = results[i].result;
2014-01-24 17:09:53 +00:00
}
cb(errors, data);
});
return data;
2014-01-24 17:09:53 +00:00
}
var enforced = {};
2014-01-24 17:09:53 +00:00
var obj;
var idValue = getIdValue(this, data);
2014-01-24 17:09:53 +00:00
// if we come from save
if (data instanceof Model && !idValue) {
2014-01-24 17:09:53 +00:00
obj = data;
} else {
obj = new Model(data);
}
this.applyProperties(enforced, obj);
obj.setAttributes(enforced);
Model = this.lookupModel(data); // data-specific
if (Model !== obj.constructor) obj = new Model(data);
var context = {
Model: Model,
instance: obj,
isNewInstance: true,
hookState: hookState,
options: options
};
Model.notifyObserversOf('before save', context, function(err) {
if (err) return cb(err);
data = obj.toObject(true);
// validation required
obj.isValid(function (valid) {
if (valid) {
create();
} else {
cb(new ValidationError(obj), obj);
}
}, data);
});
2014-01-24 17:09:53 +00:00
function create() {
obj.trigger('create', function (createDone) {
obj.trigger('save', function (saveDone) {
var _idName = idName(Model);
var modelName = Model.modelName;
var val = removeUndefined(obj.toObject(true));
this._adapter().create(modelName, this.constructor._forDB(val), function (err, id, rev) {
2014-01-24 17:09:53 +00:00
if (id) {
obj.__data[_idName] = id;
defineReadonlyProp(obj, _idName, id);
}
if (rev) {
obj._rev = rev;
}
if (err) {
return cb(err, obj);
2014-01-24 17:09:53 +00:00
}
2014-09-05 15:09:23 +00:00
obj.__persisted = true;
2014-01-24 17:09:53 +00:00
saveDone.call(obj, function () {
createDone.call(obj, function () {
if (err) {
return cb(err, obj);
}
var context = {
Model: Model,
instance: obj,
isNewInstance: true,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if(!err) Model.emit('changed', obj);
});
2014-01-24 17:09:53 +00:00
});
});
}, obj);
}, obj, cb);
}, obj, cb);
2014-01-24 17:09:53 +00:00
}
// Does this make any sense? How would chaining be used here? -partap
2014-01-24 17:09:53 +00:00
// for chaining
return cb.promise || obj;
};
function stillConnecting(dataSource, obj, args) {
if (typeof args[args.length-1] === 'function') {
return dataSource.ready(obj, args);
}
// promise variant
var promiseArgs = Array.prototype.slice.call(args);
promiseArgs.callee = args.callee
var cb = utils.createPromiseCallback();
promiseArgs.push(cb);
if (dataSource.ready(obj, promiseArgs)) {
return cb.promise;
} else {
return false;
}
2013-07-25 05:51:25 +00:00
}
/**
2014-05-22 00:50:44 +00:00
* Update or insert a model instance: update exiting record if one is found, such that parameter `data.id` matches `id` of model instance;
* otherwise, insert a new record.
*
2014-05-22 00:50:44 +00:00
* NOTE: No setters, validations, or hooks are applied when using upsert.
* `updateOrCreate` is an alias
2013-08-09 22:16:32 +00:00
* @param {Object} data The model instance data
* @param {Object} [options] Options for upsert
* @param {Function} cb The callback function (optional).
*/
// [FIXME] rfeng: This is a hack to set up 'upsert' first so that
// 'upsert' will be used as the name for strong-remoting to keep it backward
// compatible for angular SDK
DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
if (options === undefined && cb === undefined) {
if (typeof data === 'function') {
// upsert(cb)
cb = data;
data = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
// upsert(data, cb)
cb = options;
options = {};
}
}
cb = cb || utils.createPromiseCallback();
data = data || {};
options = options || {};
assert(typeof data === 'object', 'The data argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
var hookState = {};
var self = this;
2014-01-24 17:09:53 +00:00
var Model = this;
var id = getIdValue(this, data);
if (id === undefined || id === null) {
return this.create(data, options, cb);
}
var context = {
Model: Model,
query: byIdQuery(Model, id),
hookState: hookState,
options: options
};
Model.notifyObserversOf('access', context, doUpdateOrCreate);
function doUpdateOrCreate(err, ctx) {
if (err) return cb(err);
var isOriginalQuery = isWhereByGivenId(Model, ctx.query.where, id)
if (Model.getDataSource().connector.updateOrCreate && isOriginalQuery) {
2015-03-11 08:39:16 +00:00
var context = {
Model: Model,
where: ctx.query.where,
data: data,
hookState: hookState,
options: options
2015-03-05 10:55:04 +00:00
};
Model.notifyObserversOf('before save', context, function(err, ctx) {
if (err) return cb(err);
data = ctx.data;
var update = data;
var inst = data;
if(!(data instanceof Model)) {
inst = new Model(data);
}
update = inst.toObject(false);
Model.applyProperties(update, inst);
Model = Model.lookupModel(update);
// FIXME(bajtos) validate the model!
// https://github.com/strongloop/loopback-datasource-juggler/issues/262
update = removeUndefined(update);
self.getDataSource().connector
.updateOrCreate(Model.modelName, update, done);
function done(err, data, result) {
var obj;
if (data && !(data instanceof Model)) {
inst._initProperties(data, { persisted: true });
obj = inst;
} else {
obj = data;
}
if (err) {
cb(err, obj);
if(!err) {
Model.emit('changed', inst);
}
} else {
var context = {
Model: Model,
instance: obj,
isNewInstance: result ? result.isNewInstance : undefined,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if(!err) {
Model.emit('changed', inst);
}
});
}
}
});
} else {
Model.findOne({ where: ctx.query.where }, { notify: false }, function (err, inst) {
if (err) {
return cb(err);
}
if (!isOriginalQuery) {
// The custom query returned from a hook may hide the fact that
// there is already a model with `id` value `data[idName(Model)]`
delete data[idName(Model)];
}
if (inst) {
inst.updateAttributes(data, options, cb);
} else {
Model = self.lookupModel(data);
var obj = new Model(data);
obj.save(options, cb);
}
});
}
2014-01-24 17:09:53 +00:00
}
return cb.promise;
};
/**
2014-05-22 00:50:44 +00:00
* Find one record that matches specified query criteria. Same as `find`, but limited to one record, and this function returns an
* object, not a collection.
* If the specified instance is not found, then create it using data provided as second argument.
2014-01-24 17:09:53 +00:00
*
2014-05-22 00:50:44 +00:00
* @param {Object} query Search conditions. See [find](#dataaccessobjectfindquery-callback) for query format.
* For example: `{where: {test: 'me'}}`.
2014-03-12 23:28:46 +00:00
* @param {Object} data Object to create.
* @param {Object} [options] Option for findOrCreate
* @param {Function} cb Callback called with (err, instance, created)
*/
DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
assert(arguments.length >= 1, 'At least one argument is required');
if (data === undefined && options === undefined && cb === undefined) {
assert(typeof query === 'object', 'Single argument must be data object');
// findOrCreate(data);
// query will be built from data, and method will return Promise
data = query;
query = {where: data};
} else if (options === undefined && cb === undefined) {
if (typeof data === 'function') {
// findOrCreate(data, cb);
// query will be built from data
cb = data;
data = query;
query = {where: data};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
// findOrCreate(query, data, cb)
cb = options;
options = {};
}
2014-01-24 17:09:53 +00:00
}
cb = cb || utils.createPromiseCallback();
query = query || {where: {}};
data = data || {};
options = options || {};
assert(typeof query === 'object', 'The query argument must be an object');
assert(typeof data === 'object', 'The data argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
var hookState = {};
var Model = this;
var self = this;
function _findOrCreate(query, data) {
var modelName = self.modelName;
data = removeUndefined(data);
self.getDataSource().connector.findOrCreate(modelName, query,
self._forDB(data),
function(err, data, created) {
var obj, Model = self.lookupModel(data);
if (data) {
obj = new Model(data, {fields: query.fields, applySetters: false,
persisted: true});
}
if (created) {
var context = {
Model: Model,
instance: obj,
isNewInstance: true,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if (cb.promise) {
cb(err, [obj, created]);
} else {
cb(err, obj, created);
}
if (!err) Model.emit('changed', obj);
});
} else {
if (cb.promise) {
cb(err, [obj, created]);
} else {
cb(err, obj, created);
}
}
});
}
if (this.getDataSource().connector.findOrCreate) {
query.limit = 1;
try {
this._normalize(query);
} catch (err) {
process.nextTick(function () {
cb(err);
});
return cb.promise;
}
this.applyScope(query);
var context = {
Model: Model,
query: query,
hookState: hookState,
options: options
};
Model.notifyObserversOf('access', context, function (err, ctx) {
if (err) return cb(err);
var query = ctx.query;
var enforced = {};
var Model = self.lookupModel(data);
var obj = data instanceof Model ? data : new Model(data);
Model.applyProperties(enforced, obj);
obj.setAttributes(enforced);
var context = {
Model: Model,
instance: obj,
isNewInstance: true,
hookState: hookState,
options: options
};
Model.notifyObserversOf('before save', context, function(err, ctx) {
if (err) return cb(err);
var obj = ctx.instance;
var data = obj.toObject(true);
// validation required
obj.isValid(function (valid) {
if (valid) {
_findOrCreate(query, data);
} else {
cb(new ValidationError(obj), obj);
}
}, data);
});
});
} else {
Model.findOne(query, options, function (err, record) {
if (err) return cb(err);
if (record) {
if (cb.promise) {
return cb(null, [record, false]);
} else {
return cb(null, record, false);
}
}
Model.create(data, options, function (err, record) {
if (cb.promise) {
cb(err, [record, record != null]);
} else {
cb(err, record, record != null);
}
});
});
}
return cb.promise;
};
/**
* Check whether a model instance exists in database
*
2014-03-12 23:28:46 +00:00
* @param {id} id Identifier of object (primary key value)
* @param {Object} [options] Options
2014-03-12 23:28:46 +00:00
* @param {Function} cb Callback function called with (err, exists: Bool)
*/
DataAccessObject.exists = function exists(id, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
assert(arguments.length >= 1, 'The id argument is required');
if (cb === undefined) {
if (typeof options === 'function') {
// exists(id, cb)
cb = options;
options = {};
}
}
cb = cb || utils.createPromiseCallback();
options = options || {};
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
2014-01-24 17:09:53 +00:00
if (id !== undefined && id !== null && id !== '') {
this.count(byIdQuery(this, id).where, options, function(err, count) {
2014-09-06 17:24:30 +00:00
cb(err, err ? false : count === 1);
});
2014-01-24 17:09:53 +00:00
} else {
process.nextTick(function() {
cb(new Error('Model::exists requires the id argument'));
});
2014-01-24 17:09:53 +00:00
}
return cb.promise;
};
/**
2014-05-22 00:50:44 +00:00
* Find model instance by ID.
*
2014-05-22 00:50:44 +00:00
* Example:
* ```js
* User.findById(23, function(err, user) {
* console.info(user.id); // 23
* });
* ```
*
2014-03-12 23:28:46 +00:00
* @param {*} id Primary key value
* @param {Object} [options] Options
2014-03-12 23:28:46 +00:00
* @param {Function} cb Callback called with (err, instance)
*/
DataAccessObject.findById = function find(id, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
assert(arguments.length >= 1, 'The id argument is required');
if (cb === undefined) {
if (typeof options === 'function') {
// findById(id, cb)
cb = options;
options = {};
}
}
cb = cb || utils.createPromiseCallback();
options = options || {};
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
if (id == null || id === '') {
process.nextTick(function() {
cb(new Error('Model::findById requires the id argument'));
});
} else {
this.findOne(byIdQuery(this, id), options, cb);
}
return cb.promise;
};
/**
* Find model instances by ids
* @param {Array} ids An array of ids
* @param {Object} query Query filter
* @param {Object} [options] Options
* @param {Function} cb Callback called with (err, instance)
*/
DataAccessObject.findByIds = function(ids, query, options, cb) {
if (options === undefined && cb === undefined) {
if (typeof query === 'function') {
// findByIds(ids, cb)
cb = query;
query = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
// findByIds(ids, query, cb)
cb = options;
options = {};
}
2014-07-29 13:01:47 +00:00
}
cb = cb || utils.createPromiseCallback();
options = options || {};
query = query || {};
assert(Array.isArray(ids), 'The ids argument must be an array');
assert(typeof query === 'object', 'The query argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
2014-09-04 16:32:38 +00:00
var pk = idName(this);
2014-07-29 13:01:47 +00:00
if (ids.length === 0) {
process.nextTick(function() { cb(null, []); });
return cb.promise;;
2014-07-29 13:01:47 +00:00
}
2014-07-29 13:01:47 +00:00
var filter = { where: {} };
filter.where[pk] = { inq: [].concat(ids) };
mergeQuery(filter, query || {});
this.find(filter, options, function(err, results) {
cb(err, err ? results : utils.sortObjectsByIds(pk, ids, results));
});
return cb.promise;
2014-07-29 13:01:47 +00:00
};
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:26:17 +00:00
var msg = 'Unknown "' + modelName + '" id "' + id + '".';
var error = new Error(msg);
error.statusCode = error.status = 404;
cb(error);
}
2013-05-24 22:10:34 +00:00
2013-07-17 16:05:37 +00:00
// alias function for backwards compat.
DataAccessObject.all = function () {
return DataAccessObject.find.apply(this, arguments);
};
2013-07-17 16:05:37 +00:00
var operators = {
2014-01-24 17:09:53 +00:00
gt: '>',
gte: '>=',
lt: '<',
lte: '<=',
between: 'BETWEEN',
inq: 'IN',
nin: 'NOT IN',
neq: '!=',
like: 'LIKE',
nlike: 'NOT LIKE'
};
2014-06-04 21:02:55 +00:00
/*
* Normalize the filter object and throw errors if invalid values are detected
* @param {Object} filter The query filter object
* @returns {Object} The normalized filter object
* @private
*/
DataAccessObject._normalize = function (filter) {
if (!filter) {
return undefined;
}
var err = null;
if ((typeof filter !== 'object') || Array.isArray(filter)) {
err = new Error(util.format('The query filter %j is not an object', filter));
err.statusCode = 400;
throw err;
}
if (filter.limit || filter.skip || filter.offset) {
var limit = Number(filter.limit || 100);
var offset = Number(filter.skip || filter.offset || 0);
if (isNaN(limit) || limit <= 0 || Math.ceil(limit) !== limit) {
err = new Error(util.format('The limit parameter %j is not valid',
filter.limit));
err.statusCode = 400;
throw err;
}
if (isNaN(offset) || offset < 0 || Math.ceil(offset) !== offset) {
err = new Error(util.format('The offset/skip parameter %j is not valid',
filter.skip || filter.offset));
err.statusCode = 400;
throw err;
}
filter.limit = limit;
filter.offset = offset;
filter.skip = offset;
}
if (filter.order) {
var order = filter.order;
if (!Array.isArray(order)) {
order = [order];
}
var fields = [];
for (var i = 0, m = order.length; i < m; i++) {
if (typeof order[i] === 'string') {
// Normalize 'f1 ASC, f2 DESC, f3' to ['f1 ASC', 'f2 DESC', 'f3']
var tokens = order[i].split(/(?:\s*,\s*)+/);
for (var t = 0, n = tokens.length; t < n; t++) {
var token = tokens[t];
if (token.length === 0) {
// Skip empty token
continue;
}
var parts = token.split(/\s+/);
if (parts.length >= 2) {
var dir = parts[1].toUpperCase();
if (dir === 'ASC' || dir === 'DESC') {
token = parts[0] + ' ' + dir;
} else {
err = new Error(util.format('The order %j has invalid direction', token));
err.statusCode = 400;
throw err;
}
}
fields.push(token);
}
} else {
err = new Error(util.format('The order %j is not valid', order[i]));
err.statusCode = 400;
throw err;
}
}
if (fields.length === 1 && typeof filter.order === 'string') {
filter.order = fields[0];
} else {
filter.order = fields;
}
}
// normalize fields as array of included property names
if (filter.fields) {
filter.fields = fieldsToArray(filter.fields,
Object.keys(this.definition.properties));
}
filter = removeUndefined(filter);
this._coerce(filter.where);
return filter;
};
function DateType(arg) {
var d = new Date(arg);
if (isNaN(d.getTime())) {
throw new Error('Invalid date: ' + arg);
}
return d;
}
function BooleanType(arg) {
if (typeof arg === 'string') {
switch (arg) {
case 'true':
case '1':
return true;
case 'false':
case '0':
return false;
}
}
if (arg == null) {
return null;
}
return Boolean(arg);
}
function NumberType(val) {
var num = Number(val);
return !isNaN(num) ? num : val;
}
2014-06-04 21:02:55 +00:00
/*
* Coerce values based the property types
* @param {Object} where The where clause
* @returns {Object} The coerced where clause
* @private
*/
2013-12-04 05:27:46 +00:00
DataAccessObject._coerce = function (where) {
var self = this;
2014-01-24 17:09:53 +00:00
if (!where) {
return where;
}
2014-06-04 21:23:53 +00:00
var err;
if (typeof where !== 'object' || Array.isArray(where)) {
2014-06-04 21:23:53 +00:00
err = new Error(util.format('The where clause %j is not an object', where));
err.statusCode = 400;
throw err;
}
var props = self.definition.properties;
2014-01-24 17:09:53 +00:00
for (var p in where) {
// Handle logical operators
if (p === 'and' || p === 'or' || p === 'nor') {
var clauses = where[p];
if (Array.isArray(clauses)) {
for (var k = 0; k < clauses.length; k++) {
self._coerce(clauses[k]);
}
2014-06-04 21:23:53 +00:00
} else {
err = new Error(util.format('The %s operator has invalid clauses %j', p, clauses));
2014-06-04 21:23:53 +00:00
err.statusCode = 400;
throw err;
}
return where;
}
2014-01-24 17:09:53 +00:00
var DataType = props[p] && props[p].type;
if (!DataType) {
continue;
}
if (Array.isArray(DataType) || DataType === Array) {
DataType = DataType[0];
}
if (DataType === Date) {
DataType = DateType;
2014-01-24 17:09:53 +00:00
} else if (DataType === Boolean) {
DataType = BooleanType;
2014-01-24 17:09:53 +00:00
} else if (DataType === Number) {
// This fixes a regression in mongodb connector
// For numbers, only convert it produces a valid number
// LoopBack by default injects a number id. We should fix it based
// on the connector's input, for example, MongoDB should use string
// while RDBs typically use number
DataType = NumberType;
2014-01-24 17:09:53 +00:00
}
if (!DataType) {
continue;
}
2015-01-08 14:44:28 +00:00
if (DataType.prototype instanceof BaseModel) {
continue;
}
2014-01-24 17:09:53 +00:00
if (DataType === geo.GeoPoint) {
// Skip the GeoPoint as the near operator breaks the assumption that
// an operation has only one property
// We should probably fix it based on
// http://docs.mongodb.org/manual/reference/operator/query/near/
// The other option is to make operators start with $
continue;
}
var val = where[p];
if (val === null || val === undefined) {
continue;
}
// Check there is an operator
var operator = null;
if ('object' === typeof val) {
if (Object.keys(val).length !== 1) {
// Skip if there are not only one properties
// as the assumption for operators is not true here
continue;
}
for (var op in operators) {
if (op in val) {
val = val[op];
operator = op;
switch(operator) {
case 'inq':
case 'nin':
if (!Array.isArray(val)) {
err = new Error(util.format('The %s property has invalid clause %j', p, where[p]));
err.statusCode = 400;
throw err;
}
break;
case 'between':
if (!Array.isArray(val) || val.length !== 2) {
err = new Error(util.format('The %s property has invalid clause %j', p, where[p]));
err.statusCode = 400;
throw err;
}
break;
case 'like':
case 'nlike':
if (!(typeof val === 'string' || val instanceof RegExp)) {
err = new Error(util.format('The %s property has invalid clause %j', p, where[p]));
err.statusCode = 400;
throw err;
}
break;
}
2014-01-24 17:09:53 +00:00
break;
}
2014-01-24 17:09:53 +00:00
}
}
2014-01-24 17:09:53 +00:00
// Coerce the array items
if (Array.isArray(val)) {
for (var i = 0; i < val.length; i++) {
if (val[i] !== null && val[i] !== undefined) {
val[i] = DataType(val[i]);
}
2014-01-24 17:09:53 +00:00
}
} else {
if (val !== null && val !== undefined) {
val = DataType(val);
}
2014-01-24 17:09:53 +00:00
}
// Rebuild {property: {operator: value}}
if (operator) {
var value = {};
value[operator] = val;
val = value;
}
where[p] = val;
}
return where;
};
/**
2014-05-22 00:50:44 +00:00
* Find all instances of Model that match the specified query.
* Fields used for filter and sort should be declared with `{index: true}` in model definition.
* See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information.
*
2014-05-22 00:50:44 +00:00
* For example, find the second page of ten users over age 21 in descending order exluding the password property.
*
2014-05-22 00:50:44 +00:00
* ```js
* User.find({
* where: {
* age: {gt: 21}},
* order: 'age DESC',
* limit: 10,
* skip: 10,
* fields: {password: false}
* },
* console.log
* );
* ```
*
2014-05-22 00:50:44 +00:00
* @options {Object} [query] Optional JSON object that specifies query criteria and parameters.
* @property {Object} where Search criteria in JSON format `{ key: val, key2: {gt: 'val2'}}`.
2014-05-22 00:50:44 +00:00
* Operations:
* - gt: >
* - gte: >=
* - lt: <
* - lte: <=
* - between
* - inq: IN
* - nin: NOT IN
* - neq: !=
* - like: LIKE
* - nlike: NOT LIKE
*
2014-05-22 00:50:44 +00:00
* You can also use `and` and `or` operations. See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information.
* @property {String|Object|Array} include Allows you to load relations of several objects and optimize numbers of requests.
* Format examples;
* - `'posts'`: Load posts
* - `['posts', 'passports']`: Load posts and passports
* - `{'owner': 'posts'}`: Load owner and owner's posts
* - `{'owner': ['posts', 'passports']}`: Load owner, owner's posts, and owner's passports
* - `{'owner': [{posts: 'images'}, 'passports']}`: Load owner, owner's posts, owner's posts' images, and owner's passports
* See `DataAccessObject.include()`.
* @property {String} order Sort order. Format: `'key1 ASC, key2 DESC'`
* @property {Number} limit Maximum number of instances to return.
* @property {Number} skip Number of instances to skip.
* @property {Number} offset Alias for `skip`.
* @property {Object|Array|String} fields Included/excluded fields.
* - `['foo']` or `'foo'` - include only the foo property
* - `['foo', 'bar']` - include the foo and bar properties. Format:
* - `{foo: true}` - include only foo
* - `{bat: false}` - include all properties, exclude bat
*
* @param {Function} cb Required callback function. Call this function with two arguments: `err` (null or Error) and an array of instances.
*/
DataAccessObject.find = function find(query, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
2014-01-24 17:09:53 +00:00
if (options === undefined && cb === undefined) {
if (typeof query === 'function') {
// find(cb);
cb = query;
query = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
// find(query, cb);
cb = options;
options = {};
}
2014-01-24 17:09:53 +00:00
}
cb = cb || utils.createPromiseCallback();
query = query || {};
options = options || {};
assert(typeof query === 'object', 'The query argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
2015-03-05 10:55:04 +00:00
var hookState = {};
var self = this;
2014-01-24 17:09:53 +00:00
try {
this._normalize(query);
} catch (err) {
process.nextTick(function () {
cb(err);
});
return cb.promise;
2014-01-24 17:09:53 +00:00
}
this.applyScope(query);
var near = query && geo.nearFilter(query.where);
2014-01-24 17:09:53 +00:00
var supportsGeo = !!this.getDataSource().connector.buildNearFilter;
if (near) {
if (supportsGeo) {
// convert it
this.getDataSource().connector.buildNearFilter(query, near);
} else if (query.where) {
2014-01-24 17:09:53 +00:00
// do in memory query
// using all documents
2014-09-06 17:24:30 +00:00
// TODO [fabien] use default scope here?
2014-01-24 17:09:53 +00:00
var context = {
Model: self,
query: query,
hookState: hookState,
options: options
};
self.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
self.getDataSource().connector.all(self.modelName, {}, function (err, data) {
var memory = new Memory();
var modelName = self.modelName;
2014-01-24 17:09:53 +00:00
if (err) {
cb(err);
} else if (Array.isArray(data)) {
memory.define({
properties: self.dataSource.definitions[self.modelName].properties,
settings: self.dataSource.definitions[self.modelName].settings,
model: self
2014-01-24 17:09:53 +00:00
});
data.forEach(function (obj) {
memory.create(modelName, obj, function () {
// noop
});
});
// FIXME: apply "includes" and other transforms - see allCb below
memory.all(modelName, ctx.query, cb);
} else {
cb(null, []);
}
});
});
2014-01-24 17:09:53 +00:00
// already handled
return cb.promise;
}
2014-01-24 17:09:53 +00:00
}
var allCb = function(err, data) {
var results = [];
if (Array.isArray(data)) {
for (var i = 0, n = data.length; i < n; i++) {
var d = data[i];
var Model = self.lookupModel(d);
var obj = new Model(d, {fields: query.fields, applySetters: false, persisted: true});
if (query && query.include) {
if (query.collect) {
2014-01-29 07:01:11 +00:00
// The collect property indicates that the query is to return the
// standalone items for a related model, not as child of the parent object
2014-01-29 07:01:11 +00:00
// For example, article.tags
obj = obj.__cachedRelations[query.collect];
if (obj === null) {
obj = undefined;
}
2014-01-29 01:59:59 +00:00
} else {
2014-01-29 07:01:11 +00:00
// This handles the case to return parent items including the related
// models. For example, Article.find({include: 'tags'}, ...);
2014-01-29 01:59:59 +00:00
// Try to normalize the include
var includes = Inclusion.normalizeInclude(query.include || []);
includes.forEach(function(inc) {
var relationName = inc;
if (utils.isPlainObject(inc)) {
relationName = Object.keys(inc)[0];
}
2014-01-29 01:59:59 +00:00
// Promote the included model as a direct property
var included = obj.__cachedRelations[relationName];
if (Array.isArray(included)) {
included = new List(included, null, obj);
2014-02-14 07:42:21 +00:00
}
if (included) obj.__data[relationName] = included;
2014-01-29 01:59:59 +00:00
});
delete obj.__data.__cachedRelations;
}
2014-01-24 17:09:53 +00:00
}
if (obj !== undefined) {
results.push(obj);
}
}
2014-01-29 01:59:59 +00:00
2014-01-24 17:09:53 +00:00
if (data && data.countBeforeLimit) {
results.countBeforeLimit = data.countBeforeLimit;
2013-06-26 03:31:00 +00:00
}
2014-01-24 17:09:53 +00:00
if (!supportsGeo && near) {
results = geo.filter(results, near);
2014-01-24 17:09:53 +00:00
}
cb(err, results);
2013-06-26 03:31:00 +00:00
}
2014-01-24 17:09:53 +00:00
else
cb(err, []);
};
if (options.notify === false) {
self.getDataSource().connector.all(self.modelName, query, allCb);
} else {
var context = {
Model: this,
query: query,
hookState: hookState,
options: options
};
this.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
var query = ctx.query;
self.getDataSource().connector.all(self.modelName, query, allCb);
});
}
return cb.promise;
};
/**
2014-05-22 00:50:44 +00:00
* Find one record, same as `find`, but limited to one result. This function returns an object, not a collection.
2014-01-24 17:09:53 +00:00
*
* @param {Object} query Search conditions. See [find](#dataaccessobjectfindquery-callback) for query format.
2014-05-22 00:50:44 +00:00
* For example: `{where: {test: 'me'}}`.
* @param {Object} [options] Options
2014-05-22 00:50:44 +00:00
* @param {Function} cb Callback function called with (err, instance)
*/
DataAccessObject.findOne = function findOne(query, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
2014-01-24 17:09:53 +00:00
if (options === undefined && cb === undefined) {
if (typeof query === 'function') {
cb = query;
query = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
cb = options;
options = {};
}
}
cb = cb || utils.createPromiseCallback();
query = query || {};
options = options || {};
assert(typeof query === 'object', 'The query argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
query.limit = 1;
this.find(query, options, function (err, collection) {
2014-01-24 17:09:53 +00:00
if (err || !collection || !collection.length > 0) return cb(err, null);
cb(err, collection[0]);
});
return cb.promise;
};
/**
2014-05-22 00:50:44 +00:00
* Destroy all matching records.
* Delete all model instances from data source. Note: destroyAll method does not destroy hooks.
* Example:
*````js
* Product.destroyAll({price: {gt: 99}}, function(err) {
// removed matching products
* });
* ````
*
2014-05-22 00:50:44 +00:00
* @param {Object} [where] Optional object that defines the criteria. This is a "where" object. Do NOT pass a filter object.
* @param {Object) [options] Options
* @param {Function} [cb] Callback called with (err, info)
*/
DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyAll = function destroyAll(where, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
2014-09-06 17:24:30 +00:00
var Model = this;
if (options === undefined && cb === undefined) {
if (typeof where === 'function') {
cb = where;
where = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
cb = options;
options = {};
}
2014-09-06 17:24:30 +00:00
}
cb = cb || utils.createPromiseCallback();
where = where || {};
options = options || {};
assert(typeof where === 'object', 'The where argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
var hookState = {};
2014-09-06 17:24:30 +00:00
var query = { where: where };
this.applyScope(query);
where = query.where;
2015-03-05 10:55:04 +00:00
var context = {
Model: Model,
where: whereIsEmpty(where) ? {} : where,
hookState: hookState,
options: options
2015-03-05 10:55:04 +00:00
};
if (options.notify === false) {
doDelete(where);
2014-09-06 17:24:30 +00:00
} else {
query = { where: whereIsEmpty(where) ? {} : where };
var context = {
Model: Model,
query: query,
hookState: hookState,
options: options
};
Model.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
var context = {
Model: Model,
where: ctx.query.where,
hookState: hookState,
options: options
};
Model.notifyObserversOf('before delete', context, function(err, ctx) {
if (err) return cb(err);
doDelete(ctx.where);
});
});
}
function doDelete(where) {
if (whereIsEmpty(where)) {
Model.getDataSource().connector.destroyAll(Model.modelName, done);
} else {
try {
// Support an optional where object
where = removeUndefined(where);
where = Model._coerce(where);
} catch (err) {
return process.nextTick(function() {
cb(err);
});
}
Model.getDataSource().connector.destroyAll(Model.modelName, where, done);
}
function done(err, info) {
2015-02-04 16:30:13 +00:00
if (err) return cb(err);
if (options.notify === false) {
return cb(err, info);
}
var context = {
Model: Model,
where: where,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after delete', context, function(err) {
cb(err, info);
if (!err)
Model.emit('deletedAll', whereIsEmpty(where) ? undefined : where);
2014-09-06 17:24:30 +00:00
});
}
}
return cb.promise;
2014-09-06 17:24:30 +00:00
};
function whereIsEmpty(where) {
return !where ||
(typeof where === 'object' && Object.keys(where).length === 0)
}
2013-07-22 16:42:09 +00:00
/**
* Delete the record with the specified ID.
* Aliases are `destroyById` and `deleteById`.
2013-08-09 22:16:32 +00:00
* @param {*} id The id value
2014-03-12 23:28:46 +00:00
* @param {Function} cb Callback called with (err)
2013-07-22 16:42:09 +00:00
*/
// [FIXME] rfeng: This is a hack to set up 'deleteById' first so that
// 'deleteById' will be used as the name for strong-remoting to keep it backward
// compatible for angular SDK
DataAccessObject.removeById = DataAccessObject.destroyById = DataAccessObject.deleteById = function deleteById(id, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
assert(arguments.length >= 1, 'The id argument is required');
if (cb === undefined) {
if (typeof options === 'function') {
// destroyById(id, cb)
cb = options;
options = {};
}
}
options = options || {};
cb = cb || utils.createPromiseCallback();
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
if (id == null || id === '') {
process.nextTick(function() {
cb(new Error('Model::deleteById requires the id argument'));
});
return cb.promise;
}
2014-09-06 17:24:30 +00:00
var Model = this;
this.remove(byIdQuery(this, id).where, options, function(err) {
2014-09-06 17:24:30 +00:00
if ('function' === typeof cb) {
cb(err);
}
if(!err) Model.emit('deleted', id);
});
return cb.promise;
2014-09-06 17:24:30 +00:00
};
2013-07-22 16:42:09 +00:00
/**
2014-05-22 00:50:44 +00:00
* Return count of matched records. Optional query parameter allows you to count filtered set of model instances.
* Example:
*
2014-05-22 00:50:44 +00:00
*```js
* User.count({approved: true}, function(err, count) {
* console.log(count); // 2081
* });
* ```
*
2014-05-22 00:50:44 +00:00
* @param {Object} [where] Search conditions (optional)
* @param {Object} [options] Options
2014-03-12 23:28:46 +00:00
* @param {Function} cb Callback, called with (err, count)
*/
DataAccessObject.count = function (where, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
2014-01-24 17:09:53 +00:00
if (options === undefined && cb === undefined) {
if (typeof where === 'function') {
// count(cb)
cb = where;
where = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
// count(where, cb)
cb = options;
options = {};
}
2014-01-24 17:09:53 +00:00
}
cb = cb || utils.createPromiseCallback();
where = where || {};
options = options || {};
assert(typeof where === 'object', 'The where argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
var hookState = {};
2014-09-06 17:24:30 +00:00
var query = { where: where };
this.applyScope(query);
where = query.where;
try {
where = removeUndefined(where);
where = this._coerce(where);
} catch (err) {
process.nextTick(function () {
cb(err);
});
return cb.promise;
}
var Model = this;
2015-03-05 10:55:04 +00:00
var context = {
Model: Model,
query: { where: where },
hookState: hookState,
options: options
};
this.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
where = ctx.query.where;
Model.getDataSource().connector.count(Model.modelName, cb, where);
});
return cb.promise;
};
/**
* Save instance. If the instance does not have an ID, call `create` instead.
* Triggers: validate, save, update or create.
* @options {Object} options Optional options to use.
* @property {Boolean} validate Default is true.
* @property {Boolean} throws Default is false.
* @param {Function} cb Callback function with err and object arguments
*/
DataAccessObject.prototype.save = function (options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
2014-01-28 21:45:00 +00:00
var Model = this.constructor;
2014-01-24 17:09:53 +00:00
if (typeof options === 'function') {
cb = options;
2014-01-24 17:09:53 +00:00
options = {};
}
cb = cb || utils.createPromiseCallback();
2014-01-24 17:09:53 +00:00
options = options || {};
assert(typeof options === 'object', 'The options argument should be an object');
assert(typeof cb === 'function', 'The cb argument should be a function');
var hookState = {};
if (options.validate === undefined) {
2014-01-24 17:09:53 +00:00
options.validate = true;
}
if (options.throws === undefined) {
2014-01-24 17:09:53 +00:00
options.throws = false;
}
2014-09-05 15:22:14 +00:00
if (this.isNewRecord()) {
return Model.create(this, cb);
2014-01-24 17:09:53 +00:00
}
var inst = this;
var modelName = Model.modelName;
2015-03-11 08:39:16 +00:00
var context = {
Model: Model,
instance: inst,
hookState: hookState,
options: options
};
Model.notifyObserversOf('before save', context, function(err) {
if (err) return cb(err);
var data = inst.toObject(true);
Model.applyProperties(data, inst);
inst.setAttributes(data);
2014-01-24 17:09:53 +00:00
// validate first
if (!options.validate) {
return save();
}
inst.isValid(function (valid) {
if (valid) {
save();
} else {
var err = new ValidationError(inst);
// throws option is dangerous for async usage
if (options.throws) {
throw err;
}
cb(err, inst);
}
});
// then save
function save() {
inst.trigger('save', function (saveDone) {
inst.trigger('update', function (updateDone) {
data = removeUndefined(data);
inst._adapter().save(modelName, inst.constructor._forDB(data), function (err, unusedData, result) {
if (err) {
return cb(err, inst);
}
inst._initProperties(data, { persisted: true });
var context = {
Model: Model,
instance: inst,
isNewInstance: result && result.isNewInstance,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if (err) return cb(err, inst);
updateDone.call(inst, function () {
saveDone.call(inst, function () {
cb(err, inst);
if(!err) {
Model.emit('changed', inst);
}
});
});
2014-01-24 17:09:53 +00:00
});
});
}, data, cb);
}, data, cb);
}
});
return cb.promise;
};
/**
* Update multiple instances that match the where clause
*
* Example:
*
*```js
* Employee.update({managerId: 'x001'}, {managerId: 'x002'}, function(err) {
* ...
* });
* ```
*
* @param {Object} [where] Search conditions (optional)
* @param {Object} data Changes to be made
* @param {Object} [options] Options for update
* @param {Function} cb Callback, called with (err, info)
*/
DataAccessObject.update =
DataAccessObject.updateAll = function (where, data, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
assert(arguments.length >= 1, 'At least one argument is required');
if (data === undefined && options === undefined && cb === undefined && arguments.length === 1) {
data = where;
where = {};
} else if (options === undefined && cb === undefined) {
// One of:
// updateAll(data, cb)
// updateAll(where, data) -> Promise
if (typeof data === 'function') {
cb = data;
data = where;
where = {};
}
} else if (cb === undefined) {
// One of:
// updateAll(where, data, options) -> Promise
// updateAll(where, data, cb)
if (typeof options === 'function') {
cb = options;
options = {};
}
}
data = data || {};
options = options || {};
cb = cb || utils.createPromiseCallback();
assert(typeof where === 'object', 'The where argument must be an object');
assert(typeof data === 'object', 'The data argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
var hookState = {};
2014-09-06 17:24:30 +00:00
var query = { where: where };
this.applyScope(query);
this.applyProperties(data);
2014-09-06 17:24:30 +00:00
where = query.where;
var Model = this;
var context = {
Model: Model,
query: { where: where },
hookState: hookState,
options: options
};
Model.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
var context = {
Model: Model,
where: ctx.query.where,
data: data,
hookState: hookState,
options: options
};
Model.notifyObserversOf('before save', context,
function(err, ctx) {
if (err) return cb(err);
doUpdate(ctx.where, ctx.data);
});
});
function doUpdate(where, data) {
try {
where = removeUndefined(where);
where = Model._coerce(where);
data = removeUndefined(data);
data = Model._coerce(data);
} catch (err) {
return process.nextTick(function () {
cb(err);
});
}
var connector = Model.getDataSource().connector;
connector.update(Model.modelName, where, data, function(err, info) {
if (err) return cb (err);
var context = {
Model: Model,
where: where,
data: data,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err, ctx) {
return cb(err, info);
});
});
}
return cb.promise;
};
DataAccessObject.prototype.isNewRecord = function () {
return !this.__persisted;
};
/**
* Return connector of current record
* @private
*/
DataAccessObject.prototype._adapter = function () {
2014-01-24 17:09:53 +00:00
return this.getDataSource().connector;
};
/**
* Delete object from persistence
*
2014-03-12 23:28:46 +00:00
* Triggers `destroy` hook (async) before and after destroying object
*
* @param {Object} [options] Options for delete
* @param {Function} cb Callback
*/
2013-08-18 17:58:53 +00:00
DataAccessObject.prototype.remove =
2014-01-24 17:09:53 +00:00
DataAccessObject.prototype.delete =
DataAccessObject.prototype.destroy = function (options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
if (cb === undefined && typeof options === 'function') {
cb = options;
options = {};
}
cb = cb || utils.createPromiseCallback();
options = options || {};
assert(typeof options === 'object', 'The options argument should be an object');
assert(typeof cb === 'function', 'The cb argument should be a function');
var hookState = {};
var inst = this;
2014-01-29 19:03:04 +00:00
var Model = this.constructor;
var id = getIdValue(this.constructor, this);
2015-03-11 08:39:16 +00:00
var context = {
Model: Model,
query: byIdQuery(Model, id),
hookState: hookState,
options: options
};
Model.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
var context = {
Model: Model,
where: ctx.query.where,
instance: inst,
hookState: hookState,
options: options
};
Model.notifyObserversOf('before delete', context, function(err, ctx) {
if (err) return cb(err);
doDeleteInstance(ctx.where);
});
});
function doDeleteInstance(where) {
if (!isWhereByGivenId(Model, where, id)) {
// A hook modified the query, it is no longer
// a simple 'delete model with the given id'.
// We must switch to full query-based delete.
Model.deleteAll(where, { notify: false }, function(err) {
if (err) return cb(err);
var context = {
Model: Model,
where: where,
instance: inst,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after delete', context, function(err) {
cb(err);
if (!err) Model.emit('deleted', id);
});
2014-01-24 17:09:53 +00:00
});
return;
}
inst.trigger('destroy', function (destroyed) {
inst._adapter().destroy(inst.constructor.modelName, id, function (err) {
if (err) {
return cb(err);
}
destroyed(function () {
var context = {
Model: Model,
where: where,
instance: inst,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after delete', context, function(err) {
cb(err);
if (!err) Model.emit('deleted', id);
});
});
});
}, null, cb);
}
return cb.promise;
2014-01-24 17:09:53 +00:00
};
/**
* Set a single attribute.
* Equivalent to `setAttributes({name: value})`
*
* @param {String} name Name of property
* @param {Mixed} value Value of property
*/
DataAccessObject.prototype.setAttribute = function setAttribute(name, value) {
this[name] = value; // TODO [fabien] - currently not protected by applyProperties
};
/**
* Update a single attribute.
* Equivalent to `updateAttributes({name: value}, cb)`
*
2014-03-12 23:28:46 +00:00
* @param {String} name Name of property
* @param {Mixed} value Value of property
* @param {Function} cb Callback function called with (err, instance)
*/
DataAccessObject.prototype.updateAttribute = function updateAttribute(name, value, cb) {
2014-01-24 17:09:53 +00:00
var data = {};
data[name] = value;
return this.updateAttributes(data, cb);
};
/**
* Update set of attributes.
*
* @trigger `change` hook
* @param {Object} data Data to update
*/
DataAccessObject.prototype.setAttributes = function setAttributes(data) {
if (typeof data !== 'object') return;
this.constructor.applyProperties(data, this);
var Model = this.constructor;
var inst = this;
// update instance's properties
for (var key in data) {
inst.setAttribute(key, data[key]);
}
Model.emit('set', inst);
};
2014-08-19 20:05:27 +00:00
DataAccessObject.prototype.unsetAttribute = function unsetAttribute(name, nullify) {
if (nullify || this.constructor.definition.settings.persistUndefinedAsNull) {
2014-08-19 20:05:27 +00:00
this[name] = this.__data[name] = null;
} else {
delete this[name];
delete this.__data[name];
}
};
// Compare two id values to decide if updateAttributes is trying to change
// the id value for a given instance
function idEquals(id1, id2) {
if (id1 === id2) {
return true;
}
// Allows number/string conversions
if ((typeof id1 === 'number' && typeof id2 === 'string') ||
(typeof id1 === 'string' && typeof id2 === 'number')) {
return id1 == id2;
}
// For complex id types such as MongoDB ObjectID
id1 = JSON.stringify(id1);
id2 = JSON.stringify(id2);
if (id1 === id2) {
return true;
}
return false;
}
/**
* Update set of attributes.
* Performs validation before updating.
*
* @trigger `validation`, `save` and `update` hooks
2014-03-12 23:28:46 +00:00
* @param {Object} data Data to update
* @param {Object} [options] Options for updateAttributes
* @param {Function} cb Callback function called with (err, instance)
*/
DataAccessObject.prototype.updateAttributes = function updateAttributes(data, options, cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
if (options === undefined && cb === undefined) {
if (typeof data === 'function') {
// updateAttributes(cb)
cb = data;
data = undefined;
}
} else if (cb === undefined) {
if (typeof options === 'function') {
// updateAttributes(data, cb)
cb = options;
options = {};
}
2014-01-24 17:09:53 +00:00
}
cb = cb || utils.createPromiseCallback();
options = options || {};
assert((typeof data === 'object') && (data !== null),
'The data argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
var hookState = {};
var inst = this;
var Model = this.constructor;
var model = Model.modelName;
2015-03-11 08:39:16 +00:00
2015-01-30 07:26:11 +00:00
// Convert the data to be plain object so that update won't be confused
if (data instanceof Model) {
data = data.toObject(false);
}
data = removeUndefined(data);
// Make sure id(s) cannot be changed
var idNames = Model.definition.idNames();
for (var i = 0, n = idNames.length; i < n; i++) {
var idName = idNames[i];
if (data[idName] !== undefined && !idEquals(data[idName], inst[idName])) {
var err = new Error('id property (' + idName + ') ' +
'cannot be updated from ' + inst[idName] + ' to ' + data[idName]);
err.statusCode = 400;
return process.nextTick(function() {
cb(err);
});
}
}
var context = {
Model: Model,
where: byIdQuery(Model, getIdValue(Model, inst)).where,
2015-03-05 10:55:04 +00:00
data: data,
currentInstance: inst,
hookState: hookState,
options: options
};
Model.notifyObserversOf('before save', context, function(err, ctx) {
if (err) return cb(err);
data = ctx.data;
// update instance's properties
inst.setAttributes(data);
inst.isValid(function (valid) {
if (!valid) {
2014-01-24 17:09:53 +00:00
cb(new ValidationError(inst), inst);
return;
2014-01-24 17:09:53 +00:00
}
2014-01-24 17:09:53 +00:00
inst.trigger('save', function (saveDone) {
inst.trigger('update', function (done) {
var typedData = {};
2014-01-24 17:09:53 +00:00
for (var key in data) {
// Convert the properties by type
2014-01-24 17:09:53 +00:00
inst[key] = data[key];
typedData[key] = inst[key];
if (typeof typedData[key] === 'object'
&& typedData[key] !== null
&& typeof typedData[key].toObject === 'function') {
typedData[key] = typedData[key].toObject();
}
2014-01-24 17:09:53 +00:00
}
inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst),
inst.constructor._forDB(typedData), function (err) {
if (!err) inst.__persisted = true;
2014-01-24 17:09:53 +00:00
done.call(inst, function () {
saveDone.call(inst, function () {
if (err) return cb(err, inst);
var context = {
Model: Model,
instance: inst,
isNewInstance: false,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if(!err) Model.emit('changed', inst);
cb(err, inst);
});
2014-01-24 17:09:53 +00:00
});
});
});
}, data, cb);
}, data, cb);
}, data);
});
return cb.promise;
};
/**
* Reload object from persistence
2014-03-12 23:28:46 +00:00
* Requires `id` member of `object` to be able to call `find`
* @param {Function} cb Called with (err, instance) arguments
2014-06-04 21:02:55 +00:00
* @private
*/
DataAccessObject.prototype.reload = function reload(cb) {
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
}
return this.constructor.findById(getIdValue(this.constructor, this), cb);
};
2014-06-04 21:02:55 +00:00
/*
* Define readonly property on object
*
* @param {Object} obj
* @param {String} key
* @param {Mixed} value
2014-06-04 21:02:55 +00:00
* @private
*/
function defineReadonlyProp(obj, key, value) {
2014-01-24 17:09:53 +00:00
Object.defineProperty(obj, key, {
writable: false,
enumerable: true,
configurable: true,
value: value
});
}
2013-05-28 05:20:30 +00:00
var defineScope = require('./scope.js').defineScope;
/**
* Define a scope for the model class. Scopes enable you to specify commonly-used
* queries that you can reference as method calls on a model.
*
* @param {String} name The scope name
* @param {Object} query The query object for DataAccessObject.find()
* @param {ModelClass} [targetClass] The model class for the query, default to
* the declaring model
*/
2014-08-08 22:52:30 +00:00
DataAccessObject.scope = function (name, query, targetClass, methods, options) {
var cls = this;
if (options && options.isStatic === false) {
cls = cls.prototype;
}
2015-03-26 14:39:26 +00:00
return defineScope(cls, targetClass || cls, name, query, methods, options);
};
2014-06-04 21:02:55 +00:00
/*
* Add 'include'
*/
2013-05-28 05:20:30 +00:00
jutil.mixin(DataAccessObject, Inclusion);
2014-06-04 21:02:55 +00:00
/*
* Add 'relation'
*/
2013-06-05 21:33:52 +00:00
jutil.mixin(DataAccessObject, Relation);