loopback-datasource-juggler/lib/dao.js

964 lines
27 KiB
JavaScript
Raw Normal View History

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
*/
2013-05-28 05:20:30 +00:00
var jutil = require('./jutil');
var validations = require('./validations.js');
var ValidationError = 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;
/**
2014-03-12 23:28:46 +00:00
* Base class for all persistent objects.
* Provides a common API to access any database connector.
* This class describes only abstract behavior. Refer to the specific connector (`lib/connectors/*.js`) for details.
*
2014-03-12 23:28:46 +00:00
* `DataAccessObject` mixes `Inclusion` classes methods.
*
2014-03-12 23:28:46 +00:00
* @class DataAccessObject
* @param {Object} data Initial object data
*/
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-01-24 17:09:53 +00:00
return m.getDataSource().idName
2013-10-31 17:51:33 +00:00
? m.getDataSource().idName(m.modelName) : 'id';
}
function getIdValue(m, data) {
2014-01-24 17:09:53 +00:00
return data && data[m.getDataSource().idName(m.modelName)];
}
function setIdValue(m, data, value) {
2014-01-24 17:09:53 +00:00
if (data) {
data[idName(m)] = value;
}
}
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;
};
/**
2014-03-12 23:28:46 +00:00
* Create new instance of Model class, saved in database.
* The callback function is called with arguments:
*
* - err (null or Error)
* - instance (null or Model)
2014-03-12 23:28:46 +00:00
*
* @param data {Object} Optional data object
* @param callback {Function} Callback function
*/
DataAccessObject.create = function (data, callback) {
2014-01-24 17:09:53 +00:00
if (stillConnecting(this.getDataSource(), this, arguments)) return;
2014-01-24 17:09:53 +00:00
var Model = this;
var modelName = Model.modelName;
2014-01-24 17:09:53 +00:00
if (typeof data === 'function') {
callback = data;
data = {};
}
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;
if (wait === 0) callback(null, []);
var instances = [];
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);
}
2014-01-24 17:09:53 +00:00
return instances;
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
}
}
2014-01-24 17:09:53 +00:00
}
var obj;
// if we come from save
if (data instanceof Model && !getIdValue(this, data)) {
obj = data;
} else {
obj = new Model(data);
}
data = obj.toObject(true);
// validation required
obj.isValid(function (valid) {
if (valid) {
create();
} else {
2014-01-24 17:09:53 +00:00
callback(new ValidationError(obj), obj);
}
2014-01-24 17:09:53 +00:00
}, data);
2014-01-24 17:09:53 +00:00
function create() {
obj.trigger('create', function (createDone) {
obj.trigger('save', function (saveDone) {
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;
obj.__dataWas[_idName] = id;
defineReadonlyProp(obj, _idName, id);
}
if (rev) {
obj._rev = rev;
}
if (err) {
return callback(err, obj);
}
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
});
});
}, obj);
2014-01-24 17:09:53 +00:00
}, obj);
}, obj);
}
2014-01-24 17:09:53 +00:00
// for chaining
return obj;
};
/*!
* Configure the remoting attributes for a given function
* @param {Function} fn The function
* @param {Object} options The options
* @private
*/
function setRemoting(fn, options) {
2014-01-24 17:09:53 +00:00
options = options || {};
for (var opt in options) {
if (options.hasOwnProperty(opt)) {
fn[opt] = options[opt];
}
2014-01-24 17:09:53 +00:00
}
fn.shared = true;
}
setRemoting(DataAccessObject.create, {
2014-01-24 17:09:53 +00:00
description: 'Create a new instance of the model and persist it into the data source',
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
returns: {arg: 'data', type: 'object', root: true},
http: {verb: 'post', path: '/'}
});
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
}
/**
2014-03-12 23:28:46 +00:00
* Update or insert a model instance.
* `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).
*/
DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data, callback) {
2014-01-24 17:09:53 +00:00
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
if (!getIdValue(this, data)) return this.create(data, callback);
if (this.getDataSource().connector.updateOrCreate) {
var inst = new Model(data);
this.getDataSource().connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) {
var obj;
if (data) {
inst._initProperties(data);
2014-01-24 17:09:53 +00:00
obj = inst;
} else {
obj = null;
}
callback(err, obj);
});
} else {
this.findById(getIdValue(this, data), function (err, inst) {
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
setRemoting(DataAccessObject.upsert, {
2014-01-24 17:09:53 +00:00
description: 'Update an existing model instance or insert a new one into the data source',
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
returns: {arg: 'data', type: 'object', root: true},
http: {verb: 'put', path: '/'}
});
/**
* Find one record, same as `all`, limited by 1 and return object, not collection,
* if not found, create using data provided as second argument
2014-01-24 17:09:53 +00:00
*
2014-03-12 23:28:46 +00:00
* @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) {
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);
});
};
/**
* 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 {Function} cb Callback function called with (err, exists: Bool)
*/
DataAccessObject.exists = function exists(id, cb) {
2014-01-24 17:09:53 +00:00
if (stillConnecting(this.getDataSource(), this, arguments)) return;
2014-01-24 17:09:53 +00:00
if (id !== undefined && id !== null && id !== '') {
this.dataSource.connector.exists(this.modelName, id, cb);
} else {
cb(new Error('Model::exists requires the id argument'));
}
};
// exists ~ remoting attributes
setRemoting(DataAccessObject.exists, {
2014-01-24 17:09:53 +00:00
description: 'Check whether a model instance exists in the data source',
2014-05-01 21:19:31 +00:00
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true,
http: {source: 'path'}},
2014-01-24 17:09:53 +00:00
returns: {arg: 'exists', type: 'any'},
http: {verb: 'get', path: '/:id/exists'}
});
2013-08-16 23:44:31 +00:00
/**
* Find object by id
*
2014-03-12 23:28:46 +00:00
* @param {*} id Primary key value
* @param {Function} cb Callback called with (err, instance)
*/
DataAccessObject.findById = function find(id, cb) {
2014-01-24 17:09:53 +00:00
if (stillConnecting(this.getDataSource(), this, arguments)) return;
2014-01-24 17:09:53 +00:00
this.getDataSource().connector.find(this.modelName, id, function (err, data) {
var obj = null;
if (data) {
if (!getIdValue(this, data)) {
setIdValue(this, data, id);
}
obj = new this();
obj._initProperties(data);
2014-01-24 17:09:53 +00:00
}
cb(err, obj);
}.bind(this));
};
2013-05-24 22:10:34 +00:00
// find ~ remoting attributes
setRemoting(DataAccessObject.findById, {
2014-01-24 17:09:53 +00:00
description: 'Find a model instance by id from the data source',
2014-05-01 21:19:31 +00:00
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true,
http: {source: 'path'}},
2014-01-24 17:09:53 +00:00
returns: {arg: 'data', type: 'any', root: true},
http: {verb: 'get', path: '/:id'},
rest: {after: convertNullToNotFoundError}
});
function convertNullToNotFoundError(ctx, cb) {
if (ctx.result !== null) return cb();
var modelName = ctx.method.sharedClass.name;
var id = ctx.getArgByName('id');
var msg = 'Unkown "' + 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 () {
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'
};
2013-12-04 05:27:46 +00:00
DataAccessObject._coerce = function (where) {
2014-01-24 17:09:53 +00:00
if (!where) {
return where;
}
var props = this.getDataSource().getModelDefinition(this.modelName).properties;
for (var p in where) {
var DataType = props[p] && props[p].type;
if (!DataType) {
continue;
}
if (Array.isArray(DataType) || DataType === Array) {
DataType = DataType[0];
}
if (DataType === Date) {
var OrigDate = Date;
DataType = function Date(arg) {
return new OrigDate(arg);
};
} else if (DataType === Boolean) {
DataType = function (val) {
if (val === 'true') {
return true;
} else if (val === 'false') {
return false;
} else {
2014-01-24 17:09:53 +00:00
return Boolean(val);
}
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 = function (val) {
var num = Number(val);
return !isNaN(num) ? num : val;
};
}
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;
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++) {
val[i] = DataType(val[i]);
}
} else {
val = DataType(val);
}
// Rebuild {property: {operator: value}}
if (operator) {
var value = {};
value[operator] = val;
val = value;
}
where[p] = val;
}
return where;
};
/**
* Find all instances of Model, matched by query
* make sure you have marked as `index: true` fields for filter or sort
*
* @param {Object} [query] the query object
*
* - where: Object `{ key: val, key2: {gt: 'val2'}}`
2014-03-12 23:28:46 +00:00
* - include: String, Object or Array. See `DataAccessObject.include()`.
* - order: String
* - limit: Number
* - skip: Number
2014-03-12 23:28:46 +00:00
*
* @param {Function} callback (required) called with two arguments: err (null or Error), array of instances
*/
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) {
cb = query;
query = null;
2014-01-24 17:09:53 +00:00
}
var constr = this;
query = query || {};
2014-01-24 17:09:53 +00:00
if (query.where) {
query.where = this._coerce(query.where);
2014-01-24 17:09:53 +00:00
}
var fields = query.fields;
var near = query && geo.nearFilter(query.where);
2014-01-24 17:09:53 +00:00
var supportsGeo = !!this.getDataSource().connector.buildNearFilter;
// normalize fields as array of included property names
if (fields) {
query.fields = fieldsToArray(fields, Object.keys(this.definition.properties));
2014-01-24 17:09:53 +00:00
}
query = removeUndefined(query);
2014-01-24 17:09:53 +00:00
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
this.getDataSource().connector.all(this.modelName, {}, function (err, data) {
var memory = new Memory();
var modelName = constr.modelName;
if (err) {
cb(err);
} else if (Array.isArray(data)) {
memory.define({
properties: constr.dataSource.definitions[constr.modelName].properties,
settings: constr.dataSource.definitions[constr.modelName].settings,
model: constr
});
data.forEach(function (obj) {
memory.create(modelName, obj, function () {
// noop
});
});
memory.all(modelName, query, cb);
2014-01-24 17:09:53 +00:00
} else {
cb(null, []);
}
}.bind(this));
2014-01-24 17:09:53 +00:00
// already handled
return;
}
2014-01-24 17:09:53 +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) {
var obj = new constr();
obj._initProperties(d, {fields: query.fields});
2014-01-24 17:09:53 +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
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
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)) {
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-24 17:09:53 +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, []);
});
};
// all ~ remoting attributes
setRemoting(DataAccessObject.find, {
2014-01-24 17:09:53 +00:00
description: 'Find all instances of the model matched by filter from the data source',
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'},
returns: {arg: 'data', type: 'array', root: true},
http: {verb: 'get', path: '/'}
});
/**
* Find one record, same as `all`, limited by 1 and return object, not collection
2014-01-24 17:09:53 +00:00
*
* @param {Object} query - search conditions: {where: {test: 'me'}}
* @param {Function} cb - callback called with (err, instance)
*/
DataAccessObject.findOne = function findOne(query, cb) {
2014-01-24 17:09:53 +00:00
if (stillConnecting(this.getDataSource(), this, arguments)) return;
if (typeof query === 'function') {
cb = query;
query = {};
2014-01-24 17:09:53 +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]);
});
};
setRemoting(DataAccessObject.findOne, {
2014-01-24 17:09:53 +00:00
description: 'Find first instance of the model matched by filter from the data source',
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'},
returns: {arg: 'data', type: 'object', root: true},
http: {verb: 'get', path: '/findOne'}
});
/**
2013-08-18 17:58:53 +00:00
* Destroy all matching records
* @param {Object} [where] An object that defines the criteria
2014-03-12 23:28:46 +00:00
* @param {Function} [cb] Callback called with (err)
*/
DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyAll = function destroyAll(where, cb) {
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;
2014-01-24 17:09:53 +00:00
if (!cb && 'function' === typeof where) {
2013-08-18 17:58:53 +00:00
cb = where;
where = undefined;
2014-01-24 17:09:53 +00:00
}
if (!where) {
2013-10-31 17:51:33 +00:00
this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
2014-01-24 17:09:53 +00:00
cb && cb(err, data);
2014-01-28 21:45:00 +00:00
if(!err) Model.emit('deletedAll');
2013-08-18 17:58:53 +00:00
}.bind(this));
2014-01-24 17:09:53 +00:00
} else {
2013-08-18 17:58:53 +00:00
// Support an optional where object
where = removeUndefined(where);
2013-12-04 05:27:46 +00:00
where = this._coerce(where);
2013-10-31 17:51:33 +00:00
this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) {
2014-01-24 17:09:53 +00:00
cb && cb(err, data);
2014-01-28 21:45:00 +00:00
if(!err) Model.emit('deletedAll', where);
2013-08-18 17:58:53 +00:00
}.bind(this));
2014-01-24 17:09:53 +00:00
}
};
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
*/
DataAccessObject.removeById = DataAccessObject.deleteById = DataAccessObject.destroyById = function deleteById(id, cb) {
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;
2013-07-22 16:42:09 +00:00
2014-01-24 17:09:53 +00:00
this.getDataSource().connector.destroy(this.modelName, id, function (err) {
if ('function' === typeof cb) {
cb(err);
}
2014-01-28 21:45:00 +00:00
if(!err) Model.emit('deleted', id);
2014-01-24 17:09:53 +00:00
}.bind(this));
2013-07-22 16:42:09 +00:00
};
// deleteById ~ remoting attributes
setRemoting(DataAccessObject.deleteById, {
2014-01-24 17:09:53 +00:00
description: 'Delete a model instance by id from the data source',
2014-05-01 21:19:31 +00:00
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true,
http: {source: 'path'}},
2014-01-24 17:09:53 +00:00
http: {verb: 'del', path: '/:id'}
});
2013-07-22 16:42:09 +00:00
/**
* Return count of matched records
*
2014-03-12 23:28:46 +00:00
* @param {Object} where Search conditions (optional)
* @param {Function} cb Callback, called with (err, count)
*/
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;
}
where = removeUndefined(where);
where = this._coerce(where);
this.getDataSource().connector.count(this.modelName, cb, where);
};
// count ~ remoting attributes
setRemoting(DataAccessObject.count, {
2014-01-24 17:09:53 +00:00
description: 'Count instances of the model matched by where from the data source',
accepts: {arg: 'where', type: 'object', description: 'Criteria to match model instances'},
returns: {arg: 'count', type: 'number'},
http: {verb: 'get', path: '/count'}
});
/**
* 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
*/
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;
}
var inst = this;
var data = inst.toObject(true);
var modelName = Model.modelName;
if (!getIdValue(Model, this)) {
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);
}
2014-01-24 17:09:53 +00:00
});
2014-01-24 17:09:53 +00:00
// 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);
}
inst._initProperties(data);
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
});
});
});
}, data);
}, data);
}
};
DataAccessObject.prototype.isNewRecord = function () {
2014-01-24 17:09:53 +00:00
return !getIdValue(this.constructor, this);
};
/**
* 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
*/
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);
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);
}
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
});
}.bind(this));
2014-01-24 17:09:53 +00:00
});
};
/**
* 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} callback Callback function called with (err, instance)
*/
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);
};
/**
* Update saet 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 {Function} callback Callback function called with (err, instance)
*/
DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb) {
2014-01-24 17:09:53 +00:00
if (stillConnecting(this.getDataSource(), this, arguments)) return;
2014-01-24 17:09:53 +00:00
var inst = this;
var Model = this.constructor;
2014-01-28 21:45:00 +00:00
var model = Model.modelName;
2014-01-24 17:09:53 +00:00
if (typeof data === 'function') {
cb = data;
data = null;
}
2014-01-24 17:09:53 +00:00
if (!data) {
data = {};
}
2014-01-24 17:09:53 +00:00
// update instance's properties
for (var key in data) {
inst[key] = data[key];
}
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) {
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];
2014-01-24 17:09:53 +00:00
}
inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst), inst.constructor._forDB(typedData), function (err) {
2014-01-24 17:09:53 +00:00
if (!err) {
// update $was attrs
for (var key in data) {
inst.__dataWas[key] = inst.__data[key];
}
}
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
});
});
});
}, data);
}, data);
}
}, data);
};
// updateAttributes ~ remoting attributes
setRemoting(DataAccessObject.prototype.updateAttributes, {
2014-01-24 17:09:53 +00:00
description: 'Update attributes for a model instance and persist it into the data source',
accepts: {arg: 'data', type: 'object', http: {source: 'body'}, description: 'An object of model property name/value pairs'},
returns: {arg: 'data', type: 'object', root: true},
http: {verb: 'put', path: '/'}
});
/**
* 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
*/
DataAccessObject.prototype.reload = function reload(callback) {
if (stillConnecting(this.getDataSource(), this, arguments)) {
return;
}
2014-01-24 17:09:53 +00:00
this.constructor.findById(getIdValue(this.constructor, this), callback);
};
/*!
* Define readonly property on object
*
* @param {Object} obj
* @param {String} key
* @param {Mixed} value
*/
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
*/
DataAccessObject.scope = function (name, query, targetClass) {
defineScope(this, targetClass || this, name, query);
};
/*!
* Add 'include'
*/
2013-05-28 05:20:30 +00:00
jutil.mixin(DataAccessObject, Inclusion);
/*!
* Add 'relation'
*/
2013-06-05 21:33:52 +00:00
jutil.mixin(DataAccessObject, Relation);