Add support for updating multiple instances with query
This commit is contained in:
parent
4ff6e0d707
commit
ad3af82923
|
@ -476,6 +476,29 @@ Memory.prototype.count = function count(model, callback, where) {
|
|||
});
|
||||
};
|
||||
|
||||
Memory.prototype.update =
|
||||
Memory.prototype.updateAll = function updateAll(model, where, data, cb) {
|
||||
var self = this;
|
||||
var cache = this.cache[model];
|
||||
var filter = null;
|
||||
where = where || {};
|
||||
filter = applyFilter({where: where});
|
||||
|
||||
var ids = Object.keys(cache);
|
||||
async.each(ids, function (id, done) {
|
||||
var inst = self.fromDb(model, cache[id]);
|
||||
if (!filter || filter(inst)) {
|
||||
self.updateAttributes(model, id, data, done);
|
||||
} else {
|
||||
process.nextTick(done);
|
||||
}
|
||||
}, function (err) {
|
||||
if (!err) {
|
||||
self.saveToFile(null, cb);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Memory.prototype.updateAttributes = function updateAttributes(model, id, data, cb) {
|
||||
if (!id) {
|
||||
var err = new Error('You must provide an id when updating attributes!');
|
||||
|
|
65
lib/dao.js
65
lib/dao.js
|
@ -19,6 +19,7 @@ var utils = require('./utils');
|
|||
var fieldsToArray = utils.fieldsToArray;
|
||||
var removeUndefined = utils.removeUndefined;
|
||||
var util = require('util');
|
||||
var assert = require('assert');
|
||||
|
||||
/**
|
||||
* Base class for all persistent objects.
|
||||
|
@ -961,6 +962,70 @@ DataAccessObject.prototype.save = function (options, callback) {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
// update(data);
|
||||
data = where;
|
||||
where = null;
|
||||
cb = null;
|
||||
} else if (arguments.length === 2) {
|
||||
if (typeof data === 'function') {
|
||||
// update(data, cb);
|
||||
cb = data;
|
||||
data = where;
|
||||
where = null;
|
||||
} else {
|
||||
// update(where, data);
|
||||
cb = null;
|
||||
}
|
||||
}
|
||||
|
||||
assert(typeof where === 'object', 'The where argument should be an object');
|
||||
assert(typeof data === 'object', 'The data argument should be an object');
|
||||
assert(cb === null || typeof cb === 'function', 'The data argument should be an object');
|
||||
|
||||
try {
|
||||
where = removeUndefined(where);
|
||||
where = this._coerce(where);
|
||||
} catch (err) {
|
||||
return process.nextTick(function () {
|
||||
cb && cb(err);
|
||||
});
|
||||
}
|
||||
var connector = this.getDataSource().connector;
|
||||
connector.update(this.modelName, where, data, cb);
|
||||
};
|
||||
|
||||
// updateAll ~ remoting attributes
|
||||
setRemoting(DataAccessObject.updateAll, {
|
||||
description: 'Update instances of the model matched by where from the data source',
|
||||
accepts: [
|
||||
{arg: 'where', type: 'object', http: {source: 'query'},
|
||||
description: 'Criteria to match model instances'},
|
||||
{arg: 'data', type: 'object', http: {source: 'body'},
|
||||
description: 'An object of model property name/value pairs'},
|
||||
],
|
||||
http: {verb: 'post', path: '/update'}
|
||||
});
|
||||
|
||||
DataAccessObject.prototype.isNewRecord = function () {
|
||||
return !getIdValue(this.constructor, this);
|
||||
};
|
||||
|
|
|
@ -541,6 +541,40 @@ describe('basic-querying', function () {
|
|||
|
||||
});
|
||||
|
||||
describe('updateAll ', function () {
|
||||
|
||||
beforeEach(seed);
|
||||
|
||||
it('should only update instances that satisfy the where condition', function (done) {
|
||||
User.update({name: 'John Lennon'}, {name: 'John Smith'}, function () {
|
||||
User.find({where: {name: 'John Lennon'}}, function (err, data) {
|
||||
should.not.exist(err);
|
||||
data.length.should.equal(0);
|
||||
User.find({where: {name: 'John Smith'}}, function (err, data) {
|
||||
should.not.exist(err);
|
||||
data.length.should.equal(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should update all instances without where', function (done) {
|
||||
User.update({name: 'John Smith'}, function () {
|
||||
User.find({where: {name: 'John Lennon'}}, function (err, data) {
|
||||
should.not.exist(err);
|
||||
data.length.should.equal(0);
|
||||
User.find({where: {name: 'John Smith'}}, function (err, data) {
|
||||
should.not.exist(err);
|
||||
data.length.should.equal(6);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
function seed(done) {
|
||||
|
|
Loading…
Reference in New Issue