Câu trả lời:
Đối với bất kỳ ai sử dụng Sequelize phiên bản 3 trở lên, hãy sử dụng:
Model.destroy({
where: {
// criteria
}
})
Tôi đã tìm kiếm sâu vào mã, từng bước vào các tệp sau:
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
Những gì tôi tìm thấy:
Không có phương thức deleteAll, có một phương thức hủy () bạn có thể gọi trên bản ghi, ví dụ:
Project.find(123).on('success', function(project) {
project.destroy().on('success', function(u) {
if (u && u.deletedAt) {
// successfully deleted the project
}
})
})
Không biết câu hỏi có còn liên quan không nhưng tôi đã tìm thấy những điều sau trên tài liệu của Sequelize.
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
Hy vọng nó giúp!
where
đối tượng nào (ví dụ {someId: 123}
).
Ví dụ này cho thấy cách bạn hứa thay vì gọi lại.
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);
});
Kiểm tra liên kết này để biết thêm thông tin http://docs.sequelizejs.com/en/latest/api/model/#destroyoptions-promiseinteger
Trong phiên bản mới, bạn có thể thử một cái gì đó như thế này
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);
});
Đây là một ES6 sử dụng ví dụ Await / Async:
async deleteProduct(id) {
if (!id) {
return {msg: 'No Id specified..', payload: 1};
}
try {
return !!await products.destroy({
where: {
id: id
}
});
} catch (e) {
return false;
}
}
Xin lưu ý rằng tôi đang sử dụng !!
Toán tử Bang Bang trên kết quả của thời gian chờ sẽ thay đổi kết quả thành Boolean.
Tôi đã viết một cái gì đó như thế này cho Sails một thời gian trước, trong trường hợp nó giúp bạn tiết kiệm thời gian:
Ví dụ sử dụng:
// 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
});
Nguồn:
/**
* 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();
}
từ: orm.js .
Hy vọng rằng sẽ giúp!
const StudentSequelize = require("../models/studientSequelize"); const StudentWork = StudentSequelize.Student; const id = req.params.id; StudentWork.findByPk(id) // here i fetch result by ID sequelize V. 5 .then( resultToDelete=>{ resultToDelete.destroy(id); // when i find the result i deleted it by destroy function }) .then( resultAfterDestroy=>{ console.log("Deleted :",resultAfterDestroy); }) .catch(err=> console.log(err));