/objection-softdelete

Automatically handle soft-deleting with your Objection.js models.

Primary LanguageJavaScriptMIT LicenseMIT

objection-softdelete

npm

Automatically handle soft-deleting with your Objection.js models.

Installation

Install from npm:

npm install objection-softdelete

Register the plugin with an instance of objection:

const objectionSoftDelete = require('objection-softdelete');
objectionSoftDelete.register(objection);

Configuration

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'
});

Usage

When soft-delete is enabled on a model, the delete timestamp will be set to new Date() on deletion.

Enable soft-delete for a model

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.

Include deleted records in query

MyModel.query().includeDeleted();

Force delete

MyModel.query().forceDelete();