Merge pull request #3 from strongloop/where-for-delete
Fix the where option for delete
This commit is contained in:
commit
c87b9db902
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
25
lib/dao.js
25
lib/dao.js
|
@ -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;
|
||||
|
|
|
@ -176,7 +176,14 @@ DataSource.prototype.setup = function(name, settings) {
|
|||
|
||||
// just save everything we get
|
||||
this.name = name;
|
||||
this.settings = settings;
|
||||
this.settings = settings || {};
|
||||
|
||||
// Check the debug env settings
|
||||
var debugEnv = process.env.DEBUG || process.env.NODE_DEBUG || '';
|
||||
var debugModules = debugEnv.split(/[\s,]+/);
|
||||
if(debugModules.indexOf('loopback') !== -1) {
|
||||
this.settings.debug = true;
|
||||
}
|
||||
|
||||
// Disconnected by default
|
||||
this.connected = false;
|
||||
|
|
|
@ -291,6 +291,27 @@ describe('basic-querying', function() {
|
|||
|
||||
});
|
||||
|
||||
describe('destroyAll with where option', function() {
|
||||
|
||||
before(seed);
|
||||
|
||||
it('should only delete instances that satisfy the where condition', 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue