2013-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Module exports class Model
|
|
|
|
*/
|
|
|
|
module.exports = DataAccessObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
var util = require('util');
|
2013-05-28 05:20:30 +00:00
|
|
|
var jutil = require('./jutil');
|
2013-05-17 15:49:57 +00:00
|
|
|
var validations = require('./validations.js');
|
|
|
|
var ValidationError = validations.ValidationError;
|
|
|
|
var List = require('./list.js');
|
|
|
|
require('./relations.js');
|
2013-05-28 05:20:30 +00:00
|
|
|
var Inclusion = require('./include.js');
|
2013-06-05 21:33:52 +00:00
|
|
|
var Relation = require('./relations.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;
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DAO class - base class for all persist objects
|
2013-07-23 18:16:43 +00:00
|
|
|
* provides **common API** to access any database connector.
|
2013-07-23 21:40:44 +00:00
|
|
|
* This class describes only abstract behavior layer, refer to `lib/connectors/*.js`
|
2013-07-23 18:16:43 +00:00
|
|
|
* to learn more about specific connector implementations
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
2013-06-05 21:33:52 +00:00
|
|
|
* `DataAccessObject` mixes `Inclusion` classes methods
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} data - initial object data
|
|
|
|
*/
|
|
|
|
function DataAccessObject() {
|
2013-05-28 05:20:30 +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) {
|
2013-10-31 17:51:33 +00:00
|
|
|
return m.getDataSource().idName
|
|
|
|
? m.getDataSource().idName(m.modelName) : 'id';
|
2013-08-15 06:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getIdValue(m, data) {
|
2013-10-31 17:51:33 +00:00
|
|
|
return data && data[m.getDataSource().idName(m.modelName)];
|
2013-08-15 06:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setIdValue(m, data, value) {
|
|
|
|
if(data) {
|
|
|
|
data[idName(m)] = value;
|
|
|
|
}
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
DataAccessObject._forDB = function (data) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if(!(this.getDataSource().isRelational && this.getDataSource().isRelational())) {
|
2013-07-13 02:10:42 +00:00
|
|
|
return data;
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
var res = {};
|
2013-10-07 04:27:02 +00:00
|
|
|
for(var propName in data) {
|
2013-10-02 05:14:21 +00:00
|
|
|
var type = this.getPropertyType(propName);
|
2013-08-26 20:38:24 +00:00
|
|
|
if (type === 'JSON' || type === 'Any' || type === 'Object' || data[propName] instanceof Array) {
|
2013-05-17 15:49:57 +00:00
|
|
|
res[propName] = JSON.stringify(data[propName]);
|
|
|
|
} else {
|
|
|
|
res[propName] = data[propName];
|
|
|
|
}
|
2013-10-07 04:27:02 +00:00
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new instance of Model class, saved in database
|
|
|
|
*
|
|
|
|
* @param data [optional]
|
|
|
|
* @param callback(err, obj)
|
|
|
|
* callback called with arguments:
|
|
|
|
*
|
|
|
|
* - err (null or Error)
|
|
|
|
* - instance (null or Model)
|
|
|
|
*/
|
|
|
|
DataAccessObject.create = function (data, callback) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
var Model = this;
|
|
|
|
var modelName = Model.modelName;
|
|
|
|
|
|
|
|
if (typeof data === 'function') {
|
|
|
|
callback = data;
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
callback = function () {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
2013-07-25 05:51:25 +00:00
|
|
|
if (Array.isArray(data)) {
|
2013-05-17 15:49:57 +00:00
|
|
|
var instances = [];
|
|
|
|
var errors = Array(data.length);
|
|
|
|
var gotError = false;
|
|
|
|
var wait = data.length;
|
|
|
|
if (wait === 0) callback(null, []);
|
|
|
|
|
2013-08-15 06:14:44 +00:00
|
|
|
var instances = [];
|
2013-05-17 15:49:57 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return instances;
|
|
|
|
|
|
|
|
function modelCreated() {
|
|
|
|
if (--wait === 0) {
|
|
|
|
callback(gotError ? errors : null, instances);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var obj;
|
|
|
|
// if we come from save
|
2013-07-25 05:51:25 +00:00
|
|
|
if (data instanceof Model && !getIdValue(this, data)) {
|
2013-05-17 15:49:57 +00:00
|
|
|
obj = data;
|
|
|
|
} else {
|
|
|
|
obj = new Model(data);
|
|
|
|
}
|
|
|
|
data = obj.toObject(true);
|
|
|
|
|
|
|
|
// validation required
|
|
|
|
obj.isValid(function(valid) {
|
|
|
|
if (valid) {
|
|
|
|
create();
|
|
|
|
} else {
|
|
|
|
callback(new ValidationError(obj), obj);
|
|
|
|
}
|
|
|
|
}, data);
|
|
|
|
|
|
|
|
function create() {
|
|
|
|
obj.trigger('create', function(createDone) {
|
|
|
|
obj.trigger('save', function(saveDone) {
|
|
|
|
|
2013-08-15 06:14:44 +00:00
|
|
|
var _idName = idName(Model);
|
2013-05-17 15:49:57 +00:00
|
|
|
this._adapter().create(modelName, this.constructor._forDB(obj.toObject(true)), function (err, id, rev) {
|
|
|
|
if (id) {
|
2013-08-15 06:14:44 +00:00
|
|
|
obj.__data[_idName] = id;
|
|
|
|
obj.__dataWas[_idName] = id;
|
|
|
|
defineReadonlyProp(obj, _idName, id);
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
|
|
|
if (rev) {
|
2013-08-15 06:14:44 +00:00
|
|
|
obj._rev = rev;
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
return callback(err, obj);
|
|
|
|
}
|
|
|
|
saveDone.call(obj, function () {
|
|
|
|
createDone.call(obj, function () {
|
|
|
|
callback(err, obj);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, obj);
|
|
|
|
}, obj);
|
|
|
|
}, obj);
|
|
|
|
}
|
|
|
|
|
2013-05-23 23:38:14 +00:00
|
|
|
// for chaining
|
2013-05-17 15:49:57 +00:00
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2013-08-22 19:24:02 +00:00
|
|
|
/*!
|
|
|
|
* Configure the remoting attributes for a given function
|
|
|
|
* @param {Function} fn The function
|
|
|
|
* @param {Object} options The options
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function setRemoting(fn, options) {
|
|
|
|
options = options || {};
|
|
|
|
for(var opt in options) {
|
|
|
|
if(options.hasOwnProperty(opt)) {
|
|
|
|
fn[opt] = options[opt];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn.shared = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
setRemoting(DataAccessObject.create, {
|
2013-08-22 20:30:13 +00:00
|
|
|
description: 'Create a new instance of the model and persist it into the data source',
|
2013-08-24 00:04:08 +00:00
|
|
|
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'data', type: 'object', root: true},
|
|
|
|
http: {verb: 'post', path: '/'}
|
|
|
|
});
|
2013-06-12 22:45:31 +00:00
|
|
|
|
2013-07-23 18:16:43 +00:00
|
|
|
function stillConnecting(dataSource, obj, args) {
|
2013-10-02 05:14:21 +00:00
|
|
|
return dataSource.ready(obj, args);
|
2013-07-25 05:51:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
2013-08-09 22:16:32 +00:00
|
|
|
* Update or insert a model instance
|
|
|
|
* @param {Object} data The model instance data
|
|
|
|
* @param {Function} [callback] The callback function
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
|
|
|
DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data, callback) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
var Model = this;
|
2013-07-25 05:51:25 +00:00
|
|
|
if (!getIdValue(this, data)) return this.create(data, callback);
|
2013-10-31 17:51:33 +00:00
|
|
|
if (this.getDataSource().connector.updateOrCreate) {
|
2013-05-17 15:49:57 +00:00
|
|
|
var inst = new Model(data);
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) {
|
2013-05-17 15:49:57 +00:00
|
|
|
var obj;
|
|
|
|
if (data) {
|
2013-08-30 23:51:17 +00:00
|
|
|
inst._initProperties(data, false);
|
2013-05-17 15:49:57 +00:00
|
|
|
obj = inst;
|
|
|
|
} else {
|
|
|
|
obj = null;
|
|
|
|
}
|
|
|
|
callback(err, obj);
|
|
|
|
});
|
|
|
|
} else {
|
2013-07-25 05:51:25 +00:00
|
|
|
this.findById(getIdValue(this, data), function (err, inst) {
|
2013-05-17 15:49:57 +00:00
|
|
|
if (err) return callback(err);
|
|
|
|
if (inst) {
|
|
|
|
inst.updateAttributes(data, callback);
|
|
|
|
} else {
|
|
|
|
var obj = new Model(data);
|
|
|
|
obj.save(data, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-16 23:44:31 +00:00
|
|
|
// upsert ~ remoting attributes
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.upsert, {
|
2013-08-22 20:30:13 +00:00
|
|
|
description: 'Update an existing model instance or insert a new one into the data source',
|
2013-08-24 00:04:08 +00:00
|
|
|
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'data', type: 'object', root: true},
|
|
|
|
http: {verb: 'put', path: '/'}
|
|
|
|
});
|
|
|
|
|
2013-08-16 23:44:31 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Find one record, same as `all`, limited by 1 and return object, not collection,
|
|
|
|
* if not found, create using data provided as second argument
|
|
|
|
*
|
|
|
|
* @param {Object} query - search conditions: {where: {test: 'me'}}.
|
|
|
|
* @param {Object} data - object to create.
|
|
|
|
* @param {Function} cb - callback called with (err, instance)
|
|
|
|
*/
|
|
|
|
DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) {
|
2013-10-11 18:50:00 +00:00
|
|
|
if (query === undefined) {
|
2013-05-17 15:49:57 +00:00
|
|
|
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-08-22 19:24:02 +00:00
|
|
|
* Check whether a model instance exists in database
|
2013-05-17 15:49:57 +00:00
|
|
|
*
|
|
|
|
* @param {id} id - identifier of object (primary key value)
|
|
|
|
* @param {Function} cb - callbacl called with (err, exists: Bool)
|
|
|
|
*/
|
|
|
|
DataAccessObject.exists = function exists(id, cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2013-08-28 05:03:59 +00:00
|
|
|
if (id !== undefined && id !== null && id !== '') {
|
2013-07-23 18:16:43 +00:00
|
|
|
this.dataSource.connector.exists(this.modelName, id, cb);
|
2013-05-17 15:49:57 +00:00
|
|
|
} else {
|
2013-08-28 05:03:59 +00:00
|
|
|
cb(new Error('Model::exists requires the id argument'));
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-24 15:02:58 +00:00
|
|
|
// exists ~ remoting attributes
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.exists, {
|
2013-08-22 20:30:13 +00:00
|
|
|
description: 'Check whether a model instance exists in the data source',
|
2013-08-28 05:03:59 +00:00
|
|
|
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'exists', type: 'any'},
|
2013-08-28 05:03:59 +00:00
|
|
|
http: {verb: 'get', path: '/:id/exists'}
|
2013-08-22 19:24:02 +00:00
|
|
|
});
|
2013-08-16 23:44:31 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Find object by id
|
|
|
|
*
|
2013-08-09 22:16:32 +00:00
|
|
|
* @param {*} id - primary key value
|
2013-05-17 15:49:57 +00:00
|
|
|
* @param {Function} cb - callback called with (err, instance)
|
|
|
|
*/
|
2013-06-24 19:42:58 +00:00
|
|
|
DataAccessObject.findById = function find(id, cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.find(this.modelName, id, function (err, data) {
|
2013-05-17 15:49:57 +00:00
|
|
|
var obj = null;
|
|
|
|
if (data) {
|
2013-07-25 05:51:25 +00:00
|
|
|
if (!getIdValue(this, data)) {
|
|
|
|
setIdValue(this, data, id);
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
|
|
|
obj = new this();
|
2013-08-30 23:51:17 +00:00
|
|
|
obj._initProperties(data, false);
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
|
|
|
cb(err, obj);
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2013-05-24 22:10:34 +00:00
|
|
|
// find ~ remoting attributes
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.findById, {
|
2013-08-22 20:30:13 +00:00
|
|
|
description: 'Find a model instance by id from the data source',
|
2013-08-28 05:03:59 +00:00
|
|
|
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'data', type: 'any', root: true},
|
|
|
|
http: {verb: 'get', path: '/:id'}
|
|
|
|
});
|
|
|
|
|
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-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Find all instances of Model, matched by query
|
|
|
|
* make sure you have marked as `index: true` fields for filter or sort
|
|
|
|
*
|
|
|
|
* @param {Object} params (optional)
|
|
|
|
*
|
|
|
|
* - where: Object `{ key: val, key2: {gt: 'val2'}}`
|
|
|
|
* - include: String, Object or Array. See DataAccessObject.include documentation.
|
|
|
|
* - order: String
|
|
|
|
* - limit: Number
|
|
|
|
* - skip: Number
|
|
|
|
*
|
|
|
|
* @param {Function} callback (required) called with arguments:
|
|
|
|
*
|
|
|
|
* - err (null or Error)
|
|
|
|
* - Array of instances
|
|
|
|
*/
|
2013-06-24 19:42:58 +00:00
|
|
|
|
|
|
|
DataAccessObject.find = function find(params, cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
cb = params;
|
|
|
|
params = null;
|
|
|
|
}
|
|
|
|
var constr = this;
|
2013-06-26 03:31:00 +00:00
|
|
|
|
2013-07-17 16:05:37 +00:00
|
|
|
params = params || {};
|
|
|
|
var fields = params.fields;
|
2013-06-26 03:31:00 +00:00
|
|
|
var near = params && geo.nearFilter(params.where);
|
2013-10-31 17:51:33 +00:00
|
|
|
var supportsGeo = !!this.getDataSource().connector.buildNearFilter;
|
2013-06-26 03:31:00 +00:00
|
|
|
|
2013-07-17 00:53:52 +00:00
|
|
|
// normalize fields as array of included property names
|
|
|
|
if(fields) {
|
2013-10-02 22:18:50 +00:00
|
|
|
params.fields = fieldsToArray(fields, Object.keys(this.definition.properties));
|
2013-07-17 00:53:52 +00:00
|
|
|
}
|
2013-10-11 18:50:00 +00:00
|
|
|
|
|
|
|
params = removeUndefined(params);
|
2013-06-26 03:31:00 +00:00
|
|
|
if(near) {
|
|
|
|
if(supportsGeo) {
|
|
|
|
// convert it
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.buildNearFilter(params, near);
|
2013-06-26 03:31:00 +00:00
|
|
|
} else if(params.where) {
|
|
|
|
// do in memory query
|
|
|
|
// using all documents
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.all(this.modelName, {}, function (err, data) {
|
2013-06-26 03:31:00 +00:00
|
|
|
var memory = new Memory();
|
|
|
|
var modelName = constr.modelName;
|
|
|
|
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else if(Array.isArray(data)) {
|
|
|
|
memory.define({
|
2013-07-23 18:16:43 +00:00
|
|
|
properties: constr.dataSource.definitions[constr.modelName].properties,
|
|
|
|
settings: constr.dataSource.definitions[constr.modelName].settings,
|
2013-06-26 03:31:00 +00:00
|
|
|
model: constr
|
|
|
|
});
|
|
|
|
|
|
|
|
data.forEach(function (obj) {
|
|
|
|
memory.create(modelName, obj, function () {
|
|
|
|
// noop
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
memory.all(modelName, params, cb);
|
|
|
|
} else {
|
|
|
|
cb(null, []);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
// already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.all(this.modelName, params, function (err, data) {
|
2013-05-17 15:49:57 +00:00
|
|
|
if (data && data.forEach) {
|
|
|
|
data.forEach(function (d, i) {
|
2013-08-29 04:49:05 +00:00
|
|
|
var obj = new constr();
|
2013-07-17 16:05:37 +00:00
|
|
|
|
2013-08-30 23:51:17 +00:00
|
|
|
obj._initProperties(d, false, params.fields);
|
2013-07-17 16:05:37 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
if (params && params.include && params.collect) {
|
|
|
|
data[i] = obj.__cachedRelations[params.collect];
|
|
|
|
} else {
|
|
|
|
data[i] = obj;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (data && data.countBeforeLimit) {
|
|
|
|
data.countBeforeLimit = data.countBeforeLimit;
|
|
|
|
}
|
2013-06-26 03:31:00 +00:00
|
|
|
if(!supportsGeo && near) {
|
|
|
|
data = geo.filter(data, near);
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
cb(err, data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cb(err, []);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-24 15:02:58 +00:00
|
|
|
// all ~ remoting attributes
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.find, {
|
2013-08-23 21:08:47 +00:00
|
|
|
description: 'Find all instances of the model matched by filter from the data source',
|
2013-08-24 00:04:08 +00:00
|
|
|
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'},
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'data', type: 'array', root: true},
|
|
|
|
http: {verb: 'get', path: '/'}
|
|
|
|
});
|
2013-05-23 23:38:14 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Find one record, same as `all`, limited by 1 and return object, not collection
|
|
|
|
*
|
|
|
|
* @param {Object} params - search conditions: {where: {test: 'me'}}
|
|
|
|
* @param {Function} cb - callback called with (err, instance)
|
|
|
|
*/
|
|
|
|
DataAccessObject.findOne = function findOne(params, cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
if (typeof params === 'function') {
|
|
|
|
cb = params;
|
|
|
|
params = {};
|
|
|
|
}
|
2013-09-06 20:42:12 +00:00
|
|
|
params = params || {};
|
2013-05-17 15:49:57 +00:00
|
|
|
params.limit = 1;
|
2013-06-24 19:42:58 +00:00
|
|
|
this.find(params, function (err, collection) {
|
2013-05-17 15:49:57 +00:00
|
|
|
if (err || !collection || !collection.length > 0) return cb(err, null);
|
|
|
|
cb(err, collection[0]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.findOne, {
|
2013-08-23 21:08:47 +00:00
|
|
|
description: 'Find first instance of the model matched by filter from the data source',
|
2013-08-24 00:04:08 +00:00
|
|
|
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'},
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'data', type: 'object', root: true},
|
|
|
|
http: {verb: 'get', path: '/findOne'}
|
|
|
|
});
|
2013-06-12 22:45:31 +00:00
|
|
|
|
2013-08-16 23:44:31 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
2013-08-18 17:58:53 +00:00
|
|
|
* Destroy all matching records
|
|
|
|
* @param {Object} [where] An object that defines the criteria
|
|
|
|
* @param {Function} [cb] - callback called with (err)
|
2013-05-17 15:49:57 +00:00
|
|
|
*/
|
2013-08-18 17:58:53 +00:00
|
|
|
DataAccessObject.remove =
|
2013-06-26 04:08:34 +00:00
|
|
|
DataAccessObject.deleteAll =
|
2013-08-18 17:58:53 +00:00
|
|
|
DataAccessObject.destroyAll = function destroyAll(where, cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2013-08-18 17:58:53 +00:00
|
|
|
if(!cb && 'function' === typeof where) {
|
|
|
|
cb = where;
|
|
|
|
where = undefined;
|
|
|
|
}
|
|
|
|
if(!where) {
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
|
2013-08-18 17:58:53 +00:00
|
|
|
cb && cb(err, data);
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
// Support an optional where object
|
2013-10-11 18:50:00 +00:00
|
|
|
where = removeUndefined(where);
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) {
|
2013-08-18 17:58:53 +00:00
|
|
|
cb && cb(err, data);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2013-07-22 16:42:09 +00:00
|
|
|
/**
|
2013-08-09 22:16:32 +00:00
|
|
|
* Destroy a record by id
|
|
|
|
* @param {*} id The id value
|
2013-07-22 16:42:09 +00:00
|
|
|
* @param {Function} cb - callback called with (err)
|
|
|
|
*/
|
2013-08-18 17:58:53 +00:00
|
|
|
DataAccessObject.removeById =
|
2013-07-22 16:42:09 +00:00
|
|
|
DataAccessObject.deleteById =
|
|
|
|
DataAccessObject.destroyById = function deleteById(id, cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-07-22 16:42:09 +00:00
|
|
|
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.destroy(this.modelName, id, function (err) {
|
2013-07-22 16:42:09 +00:00
|
|
|
if ('function' === typeof cb) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
// deleteById ~ remoting attributes
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.deleteById, {
|
2013-08-22 20:30:13 +00:00
|
|
|
description: 'Delete a model instance by id from the data source',
|
2013-08-28 05:03:59 +00:00
|
|
|
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
|
2013-08-22 19:24:02 +00:00
|
|
|
http: {verb: 'del', path: '/:id'}
|
|
|
|
});
|
2013-07-22 16:42:09 +00:00
|
|
|
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Return count of matched records
|
|
|
|
*
|
|
|
|
* @param {Object} where - search conditions (optional)
|
|
|
|
* @param {Function} cb - callback, called with (err, count)
|
|
|
|
*/
|
|
|
|
DataAccessObject.count = function (where, cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
if (typeof where === 'function') {
|
|
|
|
cb = where;
|
|
|
|
where = null;
|
|
|
|
}
|
2013-10-11 18:50:00 +00:00
|
|
|
where = removeUndefined(where);
|
2013-10-31 17:51:33 +00:00
|
|
|
this.getDataSource().connector.count(this.modelName, cb, where);
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2013-05-24 15:02:58 +00:00
|
|
|
|
|
|
|
// count ~ remoting attributes
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.count, {
|
2013-08-23 21:08:47 +00:00
|
|
|
description: 'Count instances of the model matched by where from the data source',
|
2013-08-24 00:04:08 +00:00
|
|
|
accepts: {arg: 'where', type: 'object', description: 'Criteria to match model instances'},
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'count', type: 'number'},
|
|
|
|
http: {verb: 'get', path: '/count'}
|
|
|
|
});
|
|
|
|
|
2013-05-24 15:02:58 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Save instance. When instance haven't id, create method called instead.
|
|
|
|
* Triggers: validate, save, update | create
|
|
|
|
* @param options {validate: true, throws: false} [optional]
|
|
|
|
* @param callback(err, obj)
|
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.save = function (options, callback) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var inst = this;
|
|
|
|
var data = inst.toObject(true);
|
|
|
|
var Model = this.constructor;
|
|
|
|
var modelName = Model.modelName;
|
|
|
|
|
2013-08-15 06:14:44 +00:00
|
|
|
if (!getIdValue(Model, this)) {
|
2013-05-17 15:49:57 +00:00
|
|
|
return Model.create(this, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// then save
|
|
|
|
function save() {
|
|
|
|
inst.trigger('save', function (saveDone) {
|
|
|
|
inst.trigger('update', function (updateDone) {
|
|
|
|
inst._adapter().save(modelName, inst.constructor._forDB(data), function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err, inst);
|
|
|
|
}
|
2013-08-30 23:51:17 +00:00
|
|
|
inst._initProperties(data, false);
|
2013-05-17 15:49:57 +00:00
|
|
|
updateDone.call(inst, function () {
|
|
|
|
saveDone.call(inst, function () {
|
|
|
|
callback(err, inst);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, data);
|
|
|
|
}, data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-24 15:02:58 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
DataAccessObject.prototype.isNewRecord = function () {
|
2013-08-15 06:14:44 +00:00
|
|
|
return !getIdValue(this.constructor, this);
|
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 () {
|
2013-10-31 17:51:33 +00:00
|
|
|
return this.getDataSource().connector;
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete object from persistence
|
|
|
|
*
|
|
|
|
* @triggers `destroy` hook (async) before and after destroying object
|
|
|
|
*/
|
2013-08-18 17:58:53 +00:00
|
|
|
DataAccessObject.prototype.remove =
|
2013-06-26 04:08:34 +00:00
|
|
|
DataAccessObject.prototype.delete =
|
2013-05-17 15:49:57 +00:00
|
|
|
DataAccessObject.prototype.destroy = function (cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
this.trigger('destroy', function (destroyed) {
|
2013-08-15 06:14:44 +00:00
|
|
|
this._adapter().destroy(this.constructor.modelName, getIdValue(this.constructor, this), function (err) {
|
2013-05-17 15:49:57 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyed(function () {
|
|
|
|
if(cb) cb();
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-24 15:02:58 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Update single attribute
|
|
|
|
*
|
|
|
|
* equals to `updateAttributes({name: value}, cb)
|
|
|
|
*
|
|
|
|
* @param {String} name - name of property
|
|
|
|
* @param {Mixed} value - value of property
|
|
|
|
* @param {Function} callback - callback called with (err, instance)
|
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.updateAttribute = function updateAttribute(name, value, callback) {
|
|
|
|
var data = {};
|
|
|
|
data[name] = value;
|
|
|
|
this.updateAttributes(data, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update set of attributes
|
|
|
|
*
|
|
|
|
* this method performs validation before updating
|
|
|
|
*
|
|
|
|
* @trigger `validation`, `save` and `update` hooks
|
|
|
|
* @param {Object} data - data to update
|
|
|
|
* @param {Function} callback - callback called with (err, instance)
|
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
var inst = this;
|
|
|
|
var model = this.constructor.modelName;
|
|
|
|
|
|
|
|
if (typeof data === 'function') {
|
|
|
|
cb = data;
|
|
|
|
data = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// update instance's properties
|
2013-10-07 04:27:02 +00:00
|
|
|
for(var key in data) {
|
2013-05-17 15:49:57 +00:00
|
|
|
inst[key] = data[key];
|
2013-10-07 04:27:02 +00:00
|
|
|
}
|
2013-05-17 15:49:57 +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) {
|
|
|
|
|
2013-10-07 04:27:02 +00:00
|
|
|
for(var key in data) {
|
2013-05-17 15:49:57 +00:00
|
|
|
inst[key] = data[key];
|
2013-10-07 04:27:02 +00:00
|
|
|
}
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2013-08-15 06:14:44 +00:00
|
|
|
inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst), inst.constructor._forDB(data), function (err) {
|
2013-05-17 15:49:57 +00:00
|
|
|
if (!err) {
|
2013-10-02 05:14:21 +00:00
|
|
|
// update $was attrs
|
2013-10-07 04:27:02 +00:00
|
|
|
for(var key in data) {
|
2013-05-17 15:49:57 +00:00
|
|
|
inst.__dataWas[key] = inst.__data[key];
|
2013-10-07 04:27:02 +00:00
|
|
|
};
|
2013-05-17 15:49:57 +00:00
|
|
|
}
|
|
|
|
done.call(inst, function () {
|
|
|
|
saveDone.call(inst, function () {
|
|
|
|
cb(err, inst);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, data);
|
|
|
|
}, data);
|
|
|
|
}
|
|
|
|
}, data);
|
|
|
|
};
|
|
|
|
|
2013-05-24 15:02:58 +00:00
|
|
|
// updateAttributes ~ remoting attributes
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.prototype.updateAttributes, {
|
2013-08-22 20:30:13 +00:00
|
|
|
description: 'Update attributes for a model instance and persist it into the data source',
|
2013-08-24 00:04:08 +00:00
|
|
|
accepts: {arg: 'data', type: 'object', http: {source: 'body'}, description: 'An object of model property name/value pairs'},
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'data', type: 'object', root: true},
|
|
|
|
http: {verb: 'put', path: '/'}
|
|
|
|
});
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reload object from persistence
|
|
|
|
*
|
|
|
|
* @requires `id` member of `object` to be able to call `find`
|
|
|
|
* @param {Function} callback - called with (err, instance) arguments
|
|
|
|
*/
|
|
|
|
DataAccessObject.prototype.reload = function reload(callback) {
|
2013-10-31 17:51:33 +00:00
|
|
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2013-08-15 06:14:44 +00:00
|
|
|
this.constructor.findById(getIdValue(this.constructor, this), callback);
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2013-08-26 17:54:15 +00:00
|
|
|
/*
|
2013-08-22 19:24:02 +00:00
|
|
|
setRemoting(DataAccessObject.prototype.reload, {
|
2013-08-22 20:30:13 +00:00
|
|
|
description: 'Reload a model instance from the data source',
|
2013-08-22 19:24:02 +00:00
|
|
|
returns: {arg: 'data', type: 'object', root: true}
|
|
|
|
});
|
2013-08-26 17:54:15 +00:00
|
|
|
*/
|
2013-06-12 22:45:31 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
/**
|
|
|
|
* Define readonly property on object
|
|
|
|
*
|
|
|
|
* @param {Object} obj
|
|
|
|
* @param {String} key
|
|
|
|
* @param {Mixed} value
|
|
|
|
*/
|
|
|
|
function defineReadonlyProp(obj, key, value) {
|
|
|
|
Object.defineProperty(obj, key, {
|
|
|
|
writable: false,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
}
|
2013-05-28 05:20:30 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
var defineScope = require('./scope.js').defineScope;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define scope
|
|
|
|
*/
|
2013-11-12 06:06:43 +00:00
|
|
|
DataAccessObject.scope = function (name, filter, targetClass) {
|
|
|
|
defineScope(this, targetClass || this, name, filter);
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
2013-06-05 21:33:52 +00:00
|
|
|
// jutil.mixin(DataAccessObject, validations.Validatable);
|
2013-05-28 05:20:30 +00:00
|
|
|
jutil.mixin(DataAccessObject, Inclusion);
|
2013-06-05 21:33:52 +00:00
|
|
|
jutil.mixin(DataAccessObject, Relation);
|