I wrote something like this for Sails some time ago, if it saves you some time:
Using an example:
// Delete the user with id=4 User.findAndDelete(4,function(error,result){ // all done }); // Delete all users with type === 'suspended' User.findAndDelete({ type: 'suspended' },function(error,result){ // all done });
Source:
/** * Retrieve models which match `where`, then delete them */ function findAndDelete (where,callback) { // Handle *where* argument which is specified as an integer if (_.isFinite(+where)) { where = { id: where }; } Model.findAll({ where:where }).success(function(collection) { if (collection) { if (_.isArray(collection)) { Model.deleteAll(collection, callback); } else { collection.destroy(). success(_.unprefix(callback)). error(callback); } } else { callback(null,collection); } }).error(callback); } /** * Delete all `models` using the query chainer */ deleteAll: function (models) { var chainer = new Sequelize.Utils.QueryChainer(); _.each(models,function(m,index) { chainer.add(m.destroy()); }); return chainer.run(); }
from: orm.js.
Hope this helps!
mikermcneil Oct 12 '12 at 6:30 2012-10-12 06:30
source share