Skip to content

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

License

Notifications You must be signed in to change notification settings

ackerdev/objection-softdelete

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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();

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published