Sequelize.js delete request?

Is there a way to write a delete / deleteAll query like findAll?

For example, I want to do something like this (assuming MyModel is a Sequelize model ...):

MyModel.deleteAll({ where: ['some_field != ?', something] }) .on('success', function() { /* ... */ }); 
+72
Dec 6 2018-11-11T00:
source share
8 answers

For those using Sequelize version 3 and above, use:

 Model.destroy({ where: { // criteria } }) 

Sequelize Documentation - Expand Tutorial

+157
Nov 11 '15 at 21:52
source share

I searched the code in the code, step by step, in the following files:

https://github.com/sdepold/sequelize/blob/master/test/Model/destroy.js

https://github.com/sdepold/sequelize/blob/master/lib/model.js#L140

https://github.com/sdepold/sequelize/blob/master/lib/query-interface.js#L207-217

https://github.com/sdepold/sequelize/blob/master/lib/connectors/mysql/query-generator.js

What I found:

There is no deleteAll method, there is a destroy () method that you can call in a record, for example:

 Project.find(123).on('success', function(project) { project.destroy().on('success', function(u) { if (u && u.deletedAt) { // successfully deleted the project } }) }) 
+18
Dec 06 '11 at 16:17
source share

I don't know if the question remains relevant, but I found the following in the Sequelize documentation.

 User.destroy('`name` LIKE "J%"').success(function() { // We just deleted all rows that have a name starting with "J" }) 

http://sequelizejs.com/blog/state-of-v1-7-0

Hope this helps!

+14
Jan 30 '14 at 9:29
source share

This example shows how you promises instead of a callback.

 Model.destroy({ where: { id: 123 //this will be your id that you want to delete } }).then(function(rowDeleted){ // rowDeleted will return number of rows deleted if(rowDeleted === 1){ console.log('Deleted successfully'); } }, function(err){ console.log(err); }); 

Check out this link for more information http://docs.sequelizejs.com/en/latest/api/model/#destroyoptions-promiseinteger

+8
Dec 31 '15 at 14:20
source share

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!

+2
Oct 12 '12 at 6:30
source share

In the new version, you can try something like this

 function (req,res) { model.destroy({ where: { id: req.params.id } }) .then(function (deletedRecord) { if(deletedRecord === 1){ res.status(200).json({message:"Deleted successfully"}); } else { res.status(404).json({message:"record not found"}) } }) .catch(function (error){ res.status(500).json(error); }); 
+2
Jan 31 '17 at 7:20
source share

Here is ES6 using the Await / Async example:

  async deleteProduct(id) { if (!id) { return {msg: 'No Id specified..', payload: 1}; } try { return !!await products.destroy({ where: { id: id } }); } catch (e) { return false; } } 

Please note that I use !! Bang Bang Operator on a wait result that changes the result to a boolean.

+1
Apr 01 '18 at 11:23
source share

last @ 2019 asynchronous and waiting

 const id = 12; const result = await User.destroy({ where: { id }); then return result 
0
Apr 18 '19 at 21:13
source share



All Articles