Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo soft delete in Sequelize.js

I'm using Sequelize.js in paranoid mode in my node.js project and while the soft deletion works as expected in finding and deleting data, i'm having trouble finding a way to undelete soft deleted rows.

I know I can get deleted rows by using as explained in the docs

Model.findAll({paranoid: false, where: {deletedAt: {ne: null}}})

but paranoid: false isn't available when updating.

Is undeleting soft deleted rows even possible in Sequelize or am I just missing something?

like image 607
teemup Avatar asked Nov 12 '14 13:11

teemup


People also ask

What is soft delete in Sequelize?

A paranoid table is one that, when told to delete a record, it will not truly delete it. Instead, a special column called deletedAt will have its value set to the timestamp of that deletion request. This means that paranoid tables perform a soft-deletion of records, instead of a hard-deletion.

What is truncate Sequelize?

destory({ truncate: true }), it delete all data in table.

How do you update data in Sequelize?

First, you get an instance of the Model by calling the create() or findOne() method. Once you have the instance, you call the set() method to change the values of the instance. Then, you need to call the save() method on the instance to persist the changes to your table row.


1 Answers

Get your object with {paranoid: false} option, ie. var model = Model.findOne({ where: { something: 1 }, paranoid: false }) and then restore it model.restore() http://docs.sequelizejs.com/en/latest/api/instance/#restoreoptions-promiseundefined

like image 109
Mirek Rusin Avatar answered Oct 01 '22 23:10

Mirek Rusin