Automatically handle soft-deleting with your Objection.js models.
Install from npm:
npm install objection-softdelete
Register the plugin with an instance of objection:
const objectionSoftDelete = require('objection-softdelete');
objectionSoftDelete.register(objection);
By default, objection-softdelete uses the deletedAt
attribute for soft-deletes. You can optionally pass in an options object as the second argument to register to specify a custom attribute to use:
objectionSoftDelete.register(objection, {
deleteAttr: 'deletedOn'
});
When soft-delete is enabled on a model, the delete timestamp will be set to new Date()
on deletion.
Set the softDelete
static property on your model to true:
class MyModel {
static get softDelete() {
return true;
}
}
When softDelete is enabled, all delete queries to this model will instead update the model with a delete timestamp, and all queries to find these models will omit deleted instances.
MyModel.query().includeDeleted();
MyModel.query().forceDelete();