Implement scope.updateAll
This commit is contained in:
parent
b5188a5af4
commit
7e55ef18f8
21
lib/scope.js
21
lib/scope.js
|
@ -188,6 +188,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
|||
|
||||
f.build = build;
|
||||
f.create = create;
|
||||
f.updateAll = updateAll;
|
||||
f.destroyAll = destroyAll;
|
||||
f.findById = findById;
|
||||
f.findOne = findOne;
|
||||
|
@ -240,6 +241,13 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
|||
|
||||
cls['__delete__' + name] = fn_delete;
|
||||
|
||||
var fn_update = function () {
|
||||
var f = this[name].updateAll;
|
||||
f.apply(this[name], arguments);
|
||||
};
|
||||
|
||||
cls['__update__' + name] = fn_update;
|
||||
|
||||
var fn_findById = function (cb) {
|
||||
var f = this[name].findById;
|
||||
f.apply(this[name], arguments);
|
||||
|
@ -293,6 +301,19 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
|||
targetModel.destroyAll(filter.where, cb);
|
||||
}
|
||||
|
||||
function updateAll(where, data, cb) {
|
||||
if (arguments.length === 2) {
|
||||
// Handle updateAll(data, cb)
|
||||
cb = data;
|
||||
data = where;
|
||||
where = {};
|
||||
}
|
||||
var targetModel = definition.targetModel(this._receiver);
|
||||
var scoped = (this._scope && this._scope.where) || {};
|
||||
var filter = mergeQuery({ where: scoped }, { where: where || {} });
|
||||
targetModel.updateAll(filter.where, data, cb);
|
||||
}
|
||||
|
||||
function findById(id, cb) {
|
||||
var targetModel = definition.targetModel(this._receiver);
|
||||
var idName = targetModel.definition.idName();
|
||||
|
|
|
@ -131,7 +131,7 @@ describe('scope - order', function () {
|
|||
|
||||
});
|
||||
|
||||
describe('scope - filtered count and destroyAll', function () {
|
||||
describe('scope - filtered count, updateAll and destroyAll', function () {
|
||||
|
||||
var stationA;
|
||||
|
||||
|
@ -140,11 +140,13 @@ describe('scope - filtered count and destroyAll', function () {
|
|||
Station = db.define('Station', {
|
||||
name: {type: String, index: true},
|
||||
order: {type: Number, index: true},
|
||||
active: {type: Boolean, index: true, default: true}
|
||||
active: {type: Boolean, index: true, default: true},
|
||||
flagged: {type: Boolean, index: true, default: false}
|
||||
});
|
||||
Station.scope('ordered', {order: 'order'});
|
||||
Station.scope('active', {where: { active: true}});
|
||||
Station.scope('inactive', {where: { active: false}});
|
||||
Station.scope('flagged', {where: { flagged: true}});
|
||||
});
|
||||
|
||||
beforeEach(function (done) {
|
||||
|
@ -245,7 +247,39 @@ describe('scope - filtered count and destroyAll', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should allow updateAll', function(done) {
|
||||
Station.inactive.updateAll({ flagged: true }, function(err, result) {
|
||||
should.not.exist(err);
|
||||
result.count.should.equal(2);
|
||||
verify();
|
||||
});
|
||||
|
||||
var verify = function() {
|
||||
Station.flagged.count(function(err, count) {
|
||||
should.not.exist(err);
|
||||
count.should.equal(2);
|
||||
done();
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
it('should allow filtered updateAll', function(done) {
|
||||
Station.ordered.updateAll({ active: true }, { flagged: true }, function(err, result) {
|
||||
should.not.exist(err);
|
||||
result.count.should.equal(2);
|
||||
verify();
|
||||
});
|
||||
|
||||
var verify = function() {
|
||||
Station.flagged.count(function(err, count) {
|
||||
should.not.exist(err);
|
||||
count.should.equal(2);
|
||||
done();
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
it('should allow filtered destroyAll', function(done) {
|
||||
Station.ordered.destroyAll({ active: false }, function(err) {
|
||||
should.not.exist(err);
|
||||
|
|
Loading…
Reference in New Issue