loopback-soft-delete-cascade

1.0.5 • Public • Published

loopback-soft-delete-cascade

Features

  • Soft delete your model
  • Soft deleted records are not displayed
  • Display soft deleted records with query or remote method
  • Restore your soft deleted model
  • Cascade supports 'hasMany' relations
  • Possibility to choose on which relations to activate the cascade
  • Restore cascade
  • Use as mixin

Installation

npm install loopback-soft-delete-cascade --save

How to use

Add the mixins property to your server/model-config.json like the following:

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "../common/mixins",
      "../node_modules/loopback-soft-delete-cascade"
    ]
  }
}

To use with your Models add the mixins attribute to the definition object of your model config.

{
  "name": "YourModel",
  "mixins": {
	"SoftDelete": {
      "cascade": true, // If you want to activate the cascade on related models
      "relations": [
        "relationName"
      ] // If you want to activate cascade only on specific relations. By default, it applies it to all of them
    },
  },
  "properties": {}
} 

Soft delete mixin add these properties to your model

{
  "is_deleted": {"type": "Boolean"},
  "deleted_at": {"type": "Date"}
} 

Remote method to soft deletation of the Model

POST /YourModel/{id}/soft_delete

Remote method to restore a Model soft deletated

POST /YourModel/{id}/soft_delete_restore

Cascade method add this property to related models

{
  "YourModel_deleted": {"type": "Boolean"}
} 

To also view soft deleted records use filter

YourModel.find({
  where: {
    all: true
  }
}

Or use the remote method to also view soft deleted records

GET /YourModel/all

License

MIT

Package Sidebar

Install

npm i loopback-soft-delete-cascade

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

6.19 kB

Total Files

3

Last publish

Collaborators

  • odranoel