Fix the where option for delete

This commit is contained in:
Raymond Feng 2013-08-18 10:58:53 -07:00
parent 71f9a542fd
commit 9482d3c7de
3 changed files with 59 additions and 12 deletions

View File

@ -225,11 +225,24 @@ function applyFilter(filter) {
}
}
Memory.prototype.destroyAll = function destroyAll(model, callback) {
Object.keys(this.cache[model]).forEach(function (id) {
delete this.cache[model][id];
Memory.prototype.destroyAll = function destroyAll(model, where, callback) {
if(!callback && 'function' === typeof where) {
callback = where;
where = undefined;
}
var cache = this.cache[model];
var filter = null;
if (where) {
filter = applyFilter({where: where});
}
Object.keys(cache).forEach(function (id) {
if(!filter || filter(this.fromDb(model, cache[id]))) {
delete cache[id];
}
}.bind(this));
if(!where) {
this.cache[model] = {};
}
process.nextTick(callback);
};

View File

@ -469,18 +469,29 @@ DataAccessObject.findOne.http = [
];
/**
* Destroy all records
* @param {Function} cb - callback called with (err)
* Destroy all matching records
* @param {Object} [where] An object that defines the criteria
* @param {Function} [cb] - callback called with (err)
*/
DataAccessObject.remove =
DataAccessObject.deleteAll =
DataAccessObject.destroyAll = function destroyAll(cb) {
DataAccessObject.destroyAll = function destroyAll(where, cb) {
if (stillConnecting(this.dataSource, this, arguments)) return;
this.dataSource.connector.destroyAll(this.modelName, function (err) {
if ('function' === typeof cb) {
cb(err);
if(!cb && 'function' === typeof where) {
cb = where;
where = undefined;
}
if(!where) {
this.dataSource.connector.destroyAll(this.modelName, function (err, data) {
cb && cb(err, data);
}.bind(this));
} else {
// Support an optional where object
this.dataSource.connector.destroyAll(this.modelName, where, function (err, data) {
cb && cb(err, data);
}.bind(this));
}
};
/**
@ -488,6 +499,7 @@ DataAccessObject.destroyAll = function destroyAll(cb) {
* @param {*} id The id value
* @param {Function} cb - callback called with (err)
*/
DataAccessObject.removeById =
DataAccessObject.deleteById =
DataAccessObject.destroyById = function deleteById(id, cb) {
if (stillConnecting(this.dataSource, this, arguments)) return;
@ -633,6 +645,7 @@ DataAccessObject.prototype._adapter = function () {
*
* @triggers `destroy` hook (async) before and after destroying object
*/
DataAccessObject.prototype.remove =
DataAccessObject.prototype.delete =
DataAccessObject.prototype.destroy = function (cb) {
if (stillConnecting(this.constructor.dataSource, this, arguments)) return;

View File

@ -291,6 +291,27 @@ describe('basic-querying', function() {
});
describe('destroyAll', function() {
before(seed);
it('should check whether record not exist', function(done) {
User.destroyAll({name: 'John Lennon'}, function() {
User.find({where: {name: 'John Lennon'}}, function(err, data) {
should.not.exist(err);
data.length.should.equal(0);
User.find({where: {name: 'Paul McCartney'}}, function(err, data) {
should.not.exist(err);
data.length.should.equal(1);
done();
});
});
});
});
});
});