J'ai écrit quelque chose comme ça pour Sails il y a quelque temps, au cas où cela vous ferait gagner du temps:
Exemple d'utilisation:
// 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
});
La 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();
}
de: orm.js .
J'espère que cela pourra aider!