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) { Memory.prototype.destroyAll = function destroyAll(model, where, callback) {
Object.keys(this.cache[model]).forEach(function (id) { if(!callback && 'function' === typeof where) {
delete this.cache[model][id]; 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)); }.bind(this));
if(!where) {
this.cache[model] = {}; this.cache[model] = {};
}
process.nextTick(callback); process.nextTick(callback);
}; };

View File

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