By default, Sequelize will assume the table uses an id column as its primary key. Now you can call destroy() from the Invoice model to delete a row by its id value: const count = await Invoice. destroy({ where: { id: 2 } }); console.
User. destroy( {where: undefined}, {truncate: false} ). then(() => { return }). then(() => done()); db.
Sequelize instance comes with the query() method which you can use to run a raw query. The syntax of the method is as shown below: const [results, metadata] = await sequelize. query( "Your query here", { options } );
For anyone using Sequelize version 3 and above, use:
Model.destroy({
where: {
// criteria
}
})
Sequelize Documentation - Sequelize Tutorial
I've searched deep into the code, step by step into 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 isn't a deleteAll method, there's a destroy() method you can call on 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
}
})
})
Don't know if the question is still relevant but I have found the following on Sequelize's 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 it helps!
This example shows how to you promises instead of 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 this link out for more info http://docs.sequelizejs.com/en/latest/api/model/#destroyoptions-promiseinteger
In 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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With