2014-05-05 14:23:12 +00:00
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
/*!
|
2013-05-17 15:49:57 +00:00
|
|
|
* Module exports class Model
|
|
|
|
*/
|
|
|
|
module.exports = DataAccessObject;
|
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
/*!
|
2013-05-17 15:49:57 +00:00
|
|
|
* Module dependencies
|
|
|
|
*/
|
2013-05-28 05:20:30 +00:00
|
|
|
var jutil = require('./jutil');
|
2014-08-26 15:51:01 +00:00
|
|
|
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;
|
2013-10-11 18:50:00 +00:00
|
|
|
var utils = require('./utils');
|
|
|
|
var fieldsToArray = utils.fieldsToArray;
|
|
|
|
var removeUndefined = utils.removeUndefined;
|
2014-09-06 09:13:47 +00:00
|
|
|
var setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
|
|
|
|
var mergeQuery = utils.mergeQuery;
|
2014-06-02 06:31:51 +00:00
|
|
|
var util = require('util');
|
2014-06-17 23:30:02 +00:00
|
|
|
var assert = require('assert');
|
2013-10-11 18:50:00 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
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.
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* `DataAccessObject` mixes `Inclusion` classes methods.
|
|
|
|
* @class DataAccessObject
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 06:14:44 +00:00
|
|
|
function idName(m) {
|
2014-09-04 16:32:38 +00:00
|
|
|
return m.definition.idName() || 'id';
|
2013-08-15 06:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getIdValue(m, data) {
|
2014-09-04 16:32:38 +00:00
|
|
|
return data && data[idName(m)];
|
2013-08-15 06:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setIdValue(m, data, value) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (data) {
|
|
|
|
data[idName(m)] = value;
|
|
|
|
}
|
2013-08-15 06:14:44 +00:00
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-09-06 17:24:30 +00:00
|
|
|
function byIdQuery(m, id) {
|
|
|
|
var pk = idName(m);
|
|
|
|
var query = { where: {} };
|
|
|
|
query.where[pk] = id;
|
|
|
|
m.applyScope(query);
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
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];
|
2013-10-07 04:27:02 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
return res;
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2014-09-06 17:44:58 +00:00
|
|
|
DataAccessObject.defaultScope = function(target, isData) {
|
|
|
|
var scope = this.definition.settings.scope;
|
|
|
|
if (typeof scope === 'function') {
|
|
|
|
scope = this.definition.settings.scope.call(this, target, isData);
|
2014-09-06 09:13:47 +00:00
|
|
|
}
|
2014-09-06 17:44:58 +00:00
|
|
|
return scope;
|
2014-09-06 09:13:47 +00:00
|
|
|
};
|
|
|
|
|
2014-09-06 17:24:30 +00:00
|
|
|
DataAccessObject.applyScope = function(query) {
|
2014-09-06 17:44:58 +00:00
|
|
|
var scope = this.defaultScope(query, false) || {};
|
2014-09-06 12:38:57 +00:00
|
|
|
if (typeof scope === 'object') {
|
2014-09-06 17:24:30 +00:00
|
|
|
mergeQuery(query, scope || {}, this.definition.settings.scoping);
|
2014-09-06 12:38:57 +00:00
|
|
|
}
|
2014-09-06 09:13:47 +00:00
|
|
|
};
|
|
|
|
|
2014-09-06 17:44:58 +00:00
|
|
|
DataAccessObject.applyProperties = function(data) {
|
2014-09-07 12:12:14 +00:00
|
|
|
var properties = this.definition.settings.properties;
|
|
|
|
if (typeof properties === 'object') {
|
|
|
|
util._extend(data, properties);
|
|
|
|
} else if (properties !== false) {
|
|
|
|
var scope = this.defaultScope(data, true) || {};
|
|
|
|
if (typeof scope.where === 'object') {
|
|
|
|
setScopeValuesFromWhere(data, scope.where, this);
|
|
|
|
}
|
2014-09-06 17:44:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
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
|
2014-06-11 19:59:21 +00:00
|
|
|
* saved or having an ID.
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
2014-05-22 00:50:44 +00:00
|
|
|
* @param {Object} data Optional data object
|
|
|
|
* @param {Function} callback Callback function called with these arguments:
|
2013-05-17 15:49:57 +00:00
|
|
|
* - err (null or Error)
|
|
|
|
* - instance (null or Model)
|
|
|
|
*/
|
|
|
|
DataAccessObject.create = function (data, callback) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var Model = this;
|
|
|
|
var modelName = Model.modelName;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (typeof data === 'function') {
|
|
|
|
callback = data;
|
|
|
|
data = {};
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
callback = function () {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
var instances = [];
|
|
|
|
var errors = Array(data.length);
|
|
|
|
var gotError = false;
|
|
|
|
var wait = data.length;
|
2014-05-09 22:27:45 +00:00
|
|
|
if (wait === 0) {
|
|
|
|
callback(null, []);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < data.length; i += 1) {
|
|
|
|
(function (d, i) {
|
|
|
|
instances.push(Model.create(d, function (err, inst) {
|
|
|
|
if (err) {
|
|
|
|
errors[i] = err;
|
|
|
|
gotError = true;
|
|
|
|
}
|
|
|
|
modelCreated();
|
|
|
|
}));
|
|
|
|
})(data[i], i);
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
return instances;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
function modelCreated() {
|
|
|
|
if (--wait === 0) {
|
|
|
|
callback(gotError ? errors : null, instances);
|
2014-01-28 21:45:00 +00:00
|
|
|
if(!gotError) instances.forEach(Model.emit.bind('changed'));
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
var enforced = {};
|
2014-01-24 17:09:53 +00:00
|
|
|
var obj;
|
2014-09-05 14:35:01 +00:00
|
|
|
var idValue = getIdValue(this, data);
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// if we come from save
|
2014-09-05 14:35:01 +00:00
|
|
|
if (data instanceof Model && !idValue) {
|
2014-01-24 17:09:53 +00:00
|
|
|
obj = data;
|
|
|
|
} else {
|
|
|
|
obj = new Model(data);
|
|
|
|
}
|
2014-09-06 09:13:47 +00:00
|
|
|
|
|
|
|
this.applyProperties(enforced);
|
|
|
|
obj.setAttributes(enforced);
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
data = obj.toObject(true);
|
2014-09-05 14:35:01 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// validation required
|
|
|
|
obj.isValid(function (valid) {
|
|
|
|
if (valid) {
|
|
|
|
create();
|
2013-05-17 15:49:57 +00:00
|
|
|
} else {
|
2014-01-24 17:09:53 +00:00
|
|
|
callback(new ValidationError(obj), obj);
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}, data);
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
function create() {
|
|
|
|
obj.trigger('create', function (createDone) {
|
|
|
|
obj.trigger('save', function (saveDone) {
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var _idName = idName(Model);
|
|
|
|
this._adapter().create(modelName, this.constructor._forDB(obj.toObject(true)), function (err, id, rev) {
|
|
|
|
if (id) {
|
|
|
|
obj.__data[_idName] = id;
|
|
|
|
defineReadonlyProp(obj, _idName, id);
|
|
|
|
}
|
|
|
|
if (rev) {
|
|
|
|
obj._rev = rev;
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
return callback(err, obj);
|
|
|
|
}
|
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 () {
|
|
|
|
callback(err, obj);
|
2014-01-28 21:45:00 +00:00
|
|
|
if(!err) Model.emit('changed', obj);
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-05-17 15:49:57 +00:00
|
|
|
}, obj);
|
2014-07-08 17:54:13 +00:00
|
|
|
}, obj, callback);
|
|
|
|
}, obj, callback);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// for chaining
|
|
|
|
return obj;
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 18:16:43 +00:00
|
|
|
function stillConnecting(dataSource, obj, args) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return dataSource.ready(obj, args);
|
2013-07-25 05:51:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:49:57 +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-06-11 19:59:21 +00:00
|
|
|
*
|
2014-05-22 00:50:44 +00:00
|
|
|
* NOTE: No setters, validations, or hooks are applied when using upsert.
|
2014-05-07 18:24:49 +00:00
|
|
|
* `updateOrCreate` is an alias
|
2013-08-09 22:16:32 +00:00
|
|
|
* @param {Object} data The model instance data
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Function} callback The callback function (optional).
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
2014-07-02 19:20:56 +00:00
|
|
|
// [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, callback) {
|
2014-05-08 22:46:39 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
var Model = this;
|
2014-05-08 22:46:39 +00:00
|
|
|
if (!getIdValue(this, data)) {
|
|
|
|
return this.create(data, callback);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
if (this.getDataSource().connector.updateOrCreate) {
|
2014-05-09 22:27:45 +00:00
|
|
|
var update = data;
|
|
|
|
var inst = data;
|
|
|
|
if(!(data instanceof Model)) {
|
|
|
|
inst = new Model(data);
|
|
|
|
}
|
|
|
|
update = inst.toObject(false);
|
2014-09-06 09:13:47 +00:00
|
|
|
this.applyProperties(update);
|
2014-05-08 22:46:39 +00:00
|
|
|
update = removeUndefined(update);
|
|
|
|
this.getDataSource().connector.updateOrCreate(Model.modelName, update, function (err, data) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var obj;
|
2014-05-08 22:46:39 +00:00
|
|
|
if (data && !(data instanceof Model)) {
|
2014-02-11 06:38:59 +00:00
|
|
|
inst._initProperties(data);
|
2014-01-24 17:09:53 +00:00
|
|
|
obj = inst;
|
|
|
|
} else {
|
2014-05-08 22:46:39 +00:00
|
|
|
obj = data;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
callback(err, obj);
|
2014-05-10 00:18:28 +00:00
|
|
|
if(!err) {
|
|
|
|
Model.emit('changed', inst);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.findById(getIdValue(this, data), function (err, inst) {
|
2014-05-08 22:46:39 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
if (inst) {
|
|
|
|
inst.updateAttributes(data, callback);
|
|
|
|
} else {
|
|
|
|
var obj = new Model(data);
|
|
|
|
obj.save(data, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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 {Function} cb Callback called with (err, instance)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (query === undefined) {
|
|
|
|
query = {where: {}};
|
|
|
|
}
|
|
|
|
if (typeof data === 'function' || typeof data === 'undefined') {
|
|
|
|
callback = data;
|
|
|
|
data = query && query.where;
|
|
|
|
}
|
|
|
|
if (typeof callback === 'undefined') {
|
|
|
|
callback = function () {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var t = this;
|
|
|
|
this.findOne(query, function (err, record) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
if (record) return callback(null, record);
|
|
|
|
t.create(data, callback);
|
|
|
|
});
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-08-22 19:24:02 +00:00
|
|
|
* Check whether a model instance exists in database
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {id} id Identifier of object (primary key value)
|
|
|
|
* @param {Function} cb Callback function called with (err, exists: Bool)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
DataAccessObject.exists = function exists(id, cb) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (id !== undefined && id !== null && id !== '') {
|
2014-09-06 17:24:30 +00:00
|
|
|
this.count(byIdQuery(this, id).where, function(err, count) {
|
|
|
|
cb(err, err ? false : count === 1);
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
|
|
|
cb(new Error('Model::exists requires the id argument'));
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-05-22 00:50:44 +00:00
|
|
|
* Find model instance by ID.
|
2014-06-11 19:59:21 +00:00
|
|
|
*
|
2014-05-22 00:50:44 +00:00
|
|
|
* Example:
|
|
|
|
* ```js
|
|
|
|
* User.findById(23, function(err, user) {
|
|
|
|
* console.info(user.id); // 23
|
|
|
|
* });
|
|
|
|
* ```
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {*} id Primary key value
|
|
|
|
* @param {Function} cb Callback called with (err, instance)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
2013-06-24 19:42:58 +00:00
|
|
|
DataAccessObject.findById = function find(id, cb) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2014-09-06 12:38:57 +00:00
|
|
|
this.findOne(byIdQuery(this, id), cb);
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2014-07-29 13:01:47 +00:00
|
|
|
DataAccessObject.findByIds = function(ids, cond, cb) {
|
|
|
|
if (typeof cond === 'function') {
|
|
|
|
cb = cond;
|
|
|
|
cond = {};
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filter = { where: {} };
|
2014-08-20 12:03:38 +00:00
|
|
|
filter.where[pk] = { inq: [].concat(ids) };
|
2014-07-29 13:01:47 +00:00
|
|
|
mergeQuery(filter, cond || {});
|
|
|
|
this.find(filter, function(err, results) {
|
2014-08-15 17:39:18 +00:00
|
|
|
cb(err, err ? results : utils.sortObjectsByIds(pk, ids, results));
|
2014-07-29 13:01:47 +00:00
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2013-11-21 15:19:34 +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 + '".';
|
2013-11-21 15:19:34 +00:00
|
|
|
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 () {
|
|
|
|
DataAccessObject.find.apply(this, arguments);
|
2013-08-22 19:24:02 +00:00
|
|
|
};
|
2013-07-17 16:05:37 +00:00
|
|
|
|
2013-11-29 22:45:50 +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'
|
2013-11-29 22:45:50 +00:00
|
|
|
};
|
|
|
|
|
2014-06-04 21:02:55 +00:00
|
|
|
/*
|
2014-06-02 06:31:51 +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;
|
2014-06-17 16:07:55 +00:00
|
|
|
filter.skip = offset;
|
2014-06-02 06:31:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 06:40:20 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 06:31:51 +00:00
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
2014-06-27 06:40:20 +00:00
|
|
|
function DateType(arg) {
|
|
|
|
return new Date(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function BooleanType(val) {
|
|
|
|
if (val === 'true') {
|
|
|
|
return true;
|
|
|
|
} else if (val === 'false') {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return Boolean(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function NumberType(val) {
|
|
|
|
var num = Number(val);
|
|
|
|
return !isNaN(num) ? num : val;
|
|
|
|
}
|
|
|
|
|
2014-06-04 21:02:55 +00:00
|
|
|
/*
|
2014-06-02 06:31:51 +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) {
|
2014-06-02 06:31:51 +00:00
|
|
|
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;
|
2014-06-02 06:31:51 +00:00
|
|
|
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));
|
2014-06-02 06:31:51 +00:00
|
|
|
err.statusCode = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var props = self.definition.properties;
|
2014-01-24 17:09:53 +00:00
|
|
|
for (var p in where) {
|
2014-06-02 06:31:51 +00:00
|
|
|
// Handle logical operators
|
|
|
|
if (p === 'and' || p === 'or' || p === 'nor') {
|
|
|
|
var clauses = where[p];
|
|
|
|
if (Array.isArray(clauses)) {
|
2014-06-27 06:40:20 +00:00
|
|
|
for (var k = 0; k < clauses.length; k++) {
|
|
|
|
self._coerce(clauses[k]);
|
2014-06-02 06:31:51 +00:00
|
|
|
}
|
2014-06-04 21:23:53 +00:00
|
|
|
} else {
|
2014-06-27 06:40:20 +00:00
|
|
|
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;
|
2014-06-02 06:31:51 +00:00
|
|
|
}
|
|
|
|
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) {
|
2014-06-27 06:40:20 +00:00
|
|
|
DataType = DateType;
|
2014-01-24 17:09:53 +00:00
|
|
|
} else if (DataType === Boolean) {
|
2014-06-27 06:40:20 +00:00
|
|
|
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
|
2014-06-27 06:40:20 +00:00
|
|
|
DataType = NumberType;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!DataType) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-06-27 06:40:20 +00:00
|
|
|
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;
|
2013-11-29 22:45:50 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-11-29 22:45:50 +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++) {
|
2014-06-10 23:11:50 +00:00
|
|
|
if (val[i] !== null && val[i] !== undefined) {
|
|
|
|
val[i] = DataType(val[i]);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-10 23:11:50 +00:00
|
|
|
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;
|
2013-11-29 22:45:50 +00:00
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
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-06-11 19:59:21 +00:00
|
|
|
*
|
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-03-11 22:38:07 +00:00
|
|
|
*
|
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-03-11 22:38:07 +00:00
|
|
|
*
|
2014-05-22 00:50:44 +00:00
|
|
|
* @options {Object} [query] Optional JSON object that specifies query criteria and parameters.
|
2014-06-11 19:59:21 +00:00
|
|
|
* @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-06-11 19:59:21 +00:00
|
|
|
*
|
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
|
2014-06-11 19:59:21 +00:00
|
|
|
*
|
2014-05-22 00:50:44 +00:00
|
|
|
* @param {Function} callback Required callback function. Call this function with two arguments: `err` (null or Error) and an array of instances.
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
2013-06-24 19:42:58 +00:00
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
DataAccessObject.find = function find(query, cb) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
|
|
|
|
|
|
|
if (arguments.length === 1) {
|
2014-03-11 22:38:07 +00:00
|
|
|
cb = query;
|
|
|
|
query = null;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-06-02 06:31:51 +00:00
|
|
|
var self = this;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
query = query || {};
|
2014-09-06 12:38:57 +00:00
|
|
|
|
2014-06-02 06:31:51 +00:00
|
|
|
try {
|
|
|
|
this._normalize(query);
|
|
|
|
} catch (err) {
|
|
|
|
return process.nextTick(function () {
|
|
|
|
cb && cb(err);
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-06 12:38:57 +00:00
|
|
|
this.applyScope(query);
|
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
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
|
2014-03-11 22:38:07 +00:00
|
|
|
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
|
|
|
this.getDataSource().connector.all(this.modelName, {}, function (err, data) {
|
|
|
|
var memory = new Memory();
|
2014-06-02 06:31:51 +00:00
|
|
|
var modelName = self.modelName;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
} else if (Array.isArray(data)) {
|
|
|
|
memory.define({
|
2014-06-02 06:31:51 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
memory.all(modelName, query, cb);
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
|
|
|
cb(null, []);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-12-14 17:49:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// already handled
|
|
|
|
return;
|
2013-12-14 17:49:11 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-12-14 17:49:11 +00:00
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
this.getDataSource().connector.all(this.modelName, query, function (err, data) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (data && data.forEach) {
|
|
|
|
data.forEach(function (d, i) {
|
2014-09-05 14:35:01 +00:00
|
|
|
var obj = new self(d, {fields: query.fields, applySetters: false, persisted: true});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
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
|
|
|
|
// standlone items for a related model, not as child of the parent object
|
|
|
|
// For example, article.tags
|
2014-03-11 22:38:07 +00:00
|
|
|
obj = obj.__cachedRelations[query.collect];
|
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
|
2014-03-11 22:38:07 +00:00
|
|
|
var includes = query.include || [];
|
2014-01-29 01:59:59 +00:00
|
|
|
if (typeof includes === 'string') {
|
|
|
|
includes = [includes];
|
2014-02-14 01:43:28 +00:00
|
|
|
} else if (!Array.isArray(includes) && typeof includes === 'object') {
|
2014-01-29 01:59:59 +00:00
|
|
|
includes = Object.keys(includes);
|
|
|
|
}
|
|
|
|
includes.forEach(function (inc) {
|
|
|
|
// Promote the included model as a direct property
|
2014-02-14 07:42:21 +00:00
|
|
|
var data = obj.__cachedRelations[inc];
|
|
|
|
if(Array.isArray(data)) {
|
2014-02-14 19:21:30 +00:00
|
|
|
data = new List(data, null, obj);
|
2014-02-14 07:42:21 +00:00
|
|
|
}
|
|
|
|
obj.__data[inc] = data;
|
2014-01-29 01:59:59 +00:00
|
|
|
});
|
|
|
|
delete obj.__data.__cachedRelations;
|
2014-01-27 23:56:04 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-01-27 23:56:04 +00:00
|
|
|
data[i] = obj;
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-01-29 01:59:59 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (data && data.countBeforeLimit) {
|
|
|
|
data.countBeforeLimit = data.countBeforeLimit;
|
2013-06-26 03:31:00 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!supportsGeo && near) {
|
|
|
|
data = geo.filter(data, near);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(err, data);
|
2013-06-26 03:31:00 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
else
|
|
|
|
cb(err, []);
|
|
|
|
});
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2014-05-22 00:50:44 +00:00
|
|
|
* @param {Object} query Sarch conditions. See [find](#dataaccessobjectfindquery-callback) for query format.
|
|
|
|
* For example: `{where: {test: 'me'}}`.
|
|
|
|
* @param {Function} cb Callback function called with (err, instance)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
2014-03-11 22:38:07 +00:00
|
|
|
DataAccessObject.findOne = function findOne(query, cb) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
if (typeof query === 'function') {
|
|
|
|
cb = query;
|
|
|
|
query = {};
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-03-11 22:38:07 +00:00
|
|
|
query = query || {};
|
|
|
|
query.limit = 1;
|
|
|
|
this.find(query, 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]);
|
|
|
|
});
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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-06-11 19:59:21 +00:00
|
|
|
*
|
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.
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Function} [cb] Callback called with (err)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
2014-05-07 18:24:49 +00:00
|
|
|
DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyAll = function destroyAll(where, cb) {
|
2014-09-06 17:24:30 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
|
|
|
var Model = this;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-09-06 17:24:30 +00:00
|
|
|
if (!cb && 'function' === typeof where) {
|
|
|
|
cb = where;
|
|
|
|
where = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = { where: where };
|
|
|
|
this.applyScope(query);
|
|
|
|
where = query.where;
|
|
|
|
|
|
|
|
if (!where || (typeof where === 'object' && Object.keys(where).length === 0)) {
|
|
|
|
this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
|
|
|
|
cb && cb(err, data);
|
|
|
|
if(!err) Model.emit('deletedAll');
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
// Support an optional where object
|
|
|
|
where = removeUndefined(where);
|
|
|
|
where = this._coerce(where);
|
|
|
|
} catch (err) {
|
|
|
|
return process.nextTick(function() {
|
|
|
|
cb && cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) {
|
|
|
|
cb && cb(err, data);
|
|
|
|
if(!err) Model.emit('deletedAll', where);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
};
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2013-07-22 16:42:09 +00:00
|
|
|
/**
|
2014-06-11 19:59:21 +00:00
|
|
|
* Delete the record with the specified ID.
|
2014-05-07 18:24:49 +00:00
|
|
|
* 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
|
|
|
*/
|
2014-05-07 18:24:49 +00:00
|
|
|
|
2014-07-03 06:17:01 +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, cb) {
|
2014-09-06 17:24:30 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
|
|
|
var Model = this;
|
|
|
|
|
|
|
|
this.remove(byIdQuery(this, id).where, function(err) {
|
|
|
|
if ('function' === typeof cb) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
if(!err) Model.emit('deleted', id);
|
|
|
|
});
|
|
|
|
};
|
2013-07-22 16:42:09 +00:00
|
|
|
|
2013-05-17 15:49:57 +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-06-11 19:59:21 +00:00
|
|
|
*
|
2014-05-22 00:50:44 +00:00
|
|
|
*```js
|
|
|
|
* User.count({approved: true}, function(err, count) {
|
|
|
|
* console.log(count); // 2081
|
|
|
|
* });
|
|
|
|
* ```
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
2014-05-22 00:50:44 +00:00
|
|
|
* @param {Object} [where] Search conditions (optional)
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Function} cb Callback, called with (err, count)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
DataAccessObject.count = function (where, cb) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
|
|
|
|
|
|
|
if (typeof where === 'function') {
|
|
|
|
cb = where;
|
|
|
|
where = null;
|
|
|
|
}
|
2014-09-06 17:24:30 +00:00
|
|
|
|
|
|
|
var query = { where: where };
|
|
|
|
this.applyScope(query);
|
|
|
|
where = query.where;
|
|
|
|
|
2014-06-02 06:31:51 +00:00
|
|
|
try {
|
|
|
|
where = removeUndefined(where);
|
|
|
|
where = this._coerce(where);
|
|
|
|
} catch (err) {
|
|
|
|
return process.nextTick(function () {
|
|
|
|
cb && cb(err);
|
|
|
|
});
|
|
|
|
}
|
2014-09-06 17:24:30 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
this.getDataSource().connector.count(this.modelName, cb, where);
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-05-07 18:24:49 +00:00
|
|
|
* 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} callback Callback function with err and object arguments
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.save = function (options, callback) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2014-01-28 21:45:00 +00:00
|
|
|
var Model = this.constructor;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (typeof options == 'function') {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
callback = callback || function () {
|
|
|
|
};
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (!('validate' in options)) {
|
|
|
|
options.validate = true;
|
|
|
|
}
|
|
|
|
if (!('throws' in options)) {
|
|
|
|
options.throws = false;
|
|
|
|
}
|
2014-09-06 09:13:47 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var inst = this;
|
|
|
|
var data = inst.toObject(true);
|
|
|
|
var modelName = Model.modelName;
|
2014-09-06 09:13:47 +00:00
|
|
|
|
|
|
|
Model.applyProperties(data, this);
|
|
|
|
|
2014-09-05 15:22:14 +00:00
|
|
|
if (this.isNewRecord()) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return Model.create(this, callback);
|
2014-09-06 09:13:47 +00:00
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
callback(err, inst);
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// then save
|
|
|
|
function save() {
|
|
|
|
inst.trigger('save', function (saveDone) {
|
|
|
|
inst.trigger('update', function (updateDone) {
|
2014-05-09 16:59:34 +00:00
|
|
|
data = removeUndefined(data);
|
2014-01-24 17:09:53 +00:00
|
|
|
inst._adapter().save(modelName, inst.constructor._forDB(data), function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err, inst);
|
|
|
|
}
|
2014-09-05 15:09:23 +00:00
|
|
|
inst._initProperties(data, { persisted: true });
|
2014-01-24 17:09:53 +00:00
|
|
|
updateDone.call(inst, function () {
|
|
|
|
saveDone.call(inst, function () {
|
|
|
|
callback(err, inst);
|
2014-01-28 21:45:00 +00:00
|
|
|
if(!err) {
|
|
|
|
Model.emit('changed', inst);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-08 17:54:13 +00:00
|
|
|
}, data, callback);
|
|
|
|
}, data, callback);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2014-06-17 23:30:02 +00:00
|
|
|
/**
|
|
|
|
* 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 {Function} cb Callback, called with (err, count)
|
|
|
|
*/
|
|
|
|
DataAccessObject.update =
|
|
|
|
DataAccessObject.updateAll = function (where, data, cb) {
|
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
|
|
|
|
|
|
|
if (arguments.length === 1) {
|
2014-06-20 19:05:32 +00:00
|
|
|
// update(data) is being called
|
2014-06-17 23:30:02 +00:00
|
|
|
data = where;
|
|
|
|
where = null;
|
|
|
|
cb = null;
|
|
|
|
} else if (arguments.length === 2) {
|
|
|
|
if (typeof data === 'function') {
|
2014-06-20 19:05:32 +00:00
|
|
|
// update(data, cb) is being called
|
2014-06-17 23:30:02 +00:00
|
|
|
cb = data;
|
|
|
|
data = where;
|
|
|
|
where = null;
|
|
|
|
} else {
|
2014-06-20 19:05:32 +00:00
|
|
|
// update(where, data) is being called
|
2014-06-17 23:30:02 +00:00
|
|
|
cb = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(typeof where === 'object', 'The where argument should be an object');
|
|
|
|
assert(typeof data === 'object', 'The data argument should be an object');
|
2014-06-20 19:05:32 +00:00
|
|
|
assert(cb === null || typeof cb === 'function', 'The cb argument should be a function');
|
2014-09-06 17:24:30 +00:00
|
|
|
|
|
|
|
var query = { where: where };
|
|
|
|
this.applyScope(query);
|
|
|
|
this.applyProperties(data);
|
|
|
|
|
|
|
|
where = query.where;
|
|
|
|
|
2014-06-17 23:30:02 +00:00
|
|
|
try {
|
|
|
|
where = removeUndefined(where);
|
|
|
|
where = this._coerce(where);
|
|
|
|
} catch (err) {
|
|
|
|
return process.nextTick(function () {
|
|
|
|
cb && cb(err);
|
|
|
|
});
|
|
|
|
}
|
2014-09-06 09:13:47 +00:00
|
|
|
|
2014-06-17 23:30:02 +00:00
|
|
|
var connector = this.getDataSource().connector;
|
|
|
|
connector.update(this.modelName, where, data, cb);
|
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
DataAccessObject.prototype.isNewRecord = function () {
|
2014-09-05 14:35:01 +00:00
|
|
|
return !this.__persisted;
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-07-23 18:16:43 +00:00
|
|
|
* Return connector of current record
|
2013-05-17 15:49:57 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
DataAccessObject.prototype._adapter = function () {
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.getDataSource().connector;
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete object from persistence
|
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* Triggers `destroy` hook (async) before and after destroying object
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
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 (cb) {
|
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2014-01-29 19:03:04 +00:00
|
|
|
var Model = this.constructor;
|
|
|
|
var id = getIdValue(this.constructor, this);
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
this.trigger('destroy', function (destroyed) {
|
2014-01-29 19:03:04 +00:00
|
|
|
this._adapter().destroy(this.constructor.modelName, id, function (err) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
destroyed(function () {
|
|
|
|
if (cb) cb();
|
2014-01-29 19:03:04 +00:00
|
|
|
Model.emit('deleted', id);
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-05-17 15:49:57 +00:00
|
|
|
}.bind(this));
|
2014-07-08 17:54:13 +00:00
|
|
|
}, null, cb);
|
2014-01-24 17:09:53 +00:00
|
|
|
};
|
2014-07-29 08:54:28 +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) {
|
2014-09-06 09:13:47 +00:00
|
|
|
this[name] = value; // TODO [fabien] - currently not protected by applyProperties
|
2014-07-29 08:54:28 +00:00
|
|
|
};
|
2013-05-24 15:02:58 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
2014-05-07 18:24:49 +00:00
|
|
|
* Update a single attribute.
|
|
|
|
* Equivalent to `updateAttributes({name: value}, cb)`
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {String} name Name of property
|
|
|
|
* @param {Mixed} value Value of property
|
|
|
|
* @param {Function} callback Callback function called with (err, instance)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.updateAttribute = function updateAttribute(name, value, callback) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var data = {};
|
|
|
|
data[name] = value;
|
|
|
|
this.updateAttributes(data, callback);
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-07-29 08:54:28 +00:00
|
|
|
* Update set of attributes.
|
|
|
|
*
|
|
|
|
* @trigger `change` hook
|
|
|
|
* @param {Object} data Data to update
|
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.setAttributes = function setAttributes(data) {
|
|
|
|
if (typeof data !== 'object') return;
|
|
|
|
|
2014-09-06 09:13:47 +00:00
|
|
|
this.constructor.applyProperties(data);
|
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
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[name] = this.__data[name] = null;
|
|
|
|
} else {
|
|
|
|
delete this[name];
|
|
|
|
delete this.__data[name];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
/**
|
|
|
|
* Update set of attributes.
|
2014-05-07 18:24:49 +00:00
|
|
|
* Performs validation before updating.
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
|
|
|
* @trigger `validation`, `save` and `update` hooks
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Object} data Data to update
|
|
|
|
* @param {Function} callback Callback function called with (err, instance)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var inst = this;
|
2014-03-11 22:38:07 +00:00
|
|
|
var Model = this.constructor;
|
2014-01-28 21:45:00 +00:00
|
|
|
var model = Model.modelName;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (typeof data === 'function') {
|
|
|
|
cb = data;
|
|
|
|
data = null;
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!data) {
|
|
|
|
data = {};
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// update instance's properties
|
2014-07-29 08:54:28 +00:00
|
|
|
inst.setAttributes(data);
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
inst.isValid(function (valid) {
|
|
|
|
if (!valid) {
|
|
|
|
if (cb) {
|
|
|
|
cb(new ValidationError(inst), inst);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inst.trigger('save', function (saveDone) {
|
|
|
|
inst.trigger('update', function (done) {
|
2014-02-06 04:21:27 +00:00
|
|
|
var typedData = {};
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
for (var key in data) {
|
2014-02-25 02:38:45 +00:00
|
|
|
// Convert the properties by type
|
2014-01-24 17:09:53 +00:00
|
|
|
inst[key] = data[key];
|
2014-02-25 02:38:45 +00:00
|
|
|
typedData[key] = inst[key];
|
2014-08-15 16:01:40 +00:00
|
|
|
if (typeof typedData[key] === 'object'
|
2014-08-16 06:30:23 +00:00
|
|
|
&& typedData[key] !== null
|
2014-08-15 16:01:40 +00:00
|
|
|
&& typeof typedData[key].toObject === 'function') {
|
|
|
|
typedData[key] = typedData[key].toObject();
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 17:54:13 +00:00
|
|
|
inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst),
|
|
|
|
inst.constructor._forDB(typedData), function (err) {
|
2014-09-05 14:35:01 +00:00
|
|
|
if (!err) inst.__persisted = true;
|
2014-01-24 17:09:53 +00:00
|
|
|
done.call(inst, function () {
|
|
|
|
saveDone.call(inst, function () {
|
2014-01-29 19:03:04 +00:00
|
|
|
if(cb) cb(err, inst);
|
2014-01-28 21:45:00 +00:00
|
|
|
if(!err) Model.emit('changed', inst);
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-08 17:54:13 +00:00
|
|
|
}, data, cb);
|
|
|
|
}, data, cb);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
}, data);
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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} callback Called with (err, instance) arguments
|
2014-06-04 21:02:55 +00:00
|
|
|
* @private
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.reload = function reload(callback) {
|
2014-03-11 22:38:07 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
this.constructor.findById(getIdValue(this.constructor, this), callback);
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2013-06-12 22:45:31 +00:00
|
|
|
|
2014-06-04 21:02:55 +00:00
|
|
|
/*
|
2013-05-17 15:49:57 +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
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
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-17 15:49:57 +00:00
|
|
|
}
|
2013-05-28 05:20:30 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
var defineScope = require('./scope.js').defineScope;
|
|
|
|
|
2014-03-11 22:38:07 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2013-10-02 05:14:21 +00:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
defineScope(cls, targetClass || cls, name, query, methods, options);
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
2014-06-04 21:02:55 +00:00
|
|
|
/*
|
2014-03-11 22:38:07 +00:00
|
|
|
* Add 'include'
|
|
|
|
*/
|
2013-05-28 05:20:30 +00:00
|
|
|
jutil.mixin(DataAccessObject, Inclusion);
|
2014-03-11 22:38:07 +00:00
|
|
|
|
2014-06-04 21:02:55 +00:00
|
|
|
/*
|
2014-03-11 22:38:07 +00:00
|
|
|
* Add 'relation'
|
|
|
|
*/
|
2013-06-05 21:33:52 +00:00
|
|
|
jutil.mixin(DataAccessObject, Relation);
|