Merge pull request #3 from strongloop/where-for-delete

Fix the where option for delete
This commit is contained in:
Raymond Feng 2013-08-19 11:20:38 -07:00
commit c87b9db902
4 changed files with 67 additions and 13 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

@ -176,7 +176,14 @@ DataSource.prototype.setup = function(name, settings) {
// just save everything we get // just save everything we get
this.name = name; 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 // Disconnected by default
this.connected = false; this.connected = false;

View File

@ -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();
});
});
});
});
});
}); });