Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bookshelf-cascade-delete

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf-cascade-delete

Cascade delete with Bookshelf.js

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2K
decreased by-33.82%
Maintainers
1
Weekly downloads
 
Created
Source

bookshelf-cascade-delete

This Bookshelf.js plugin provides cascade delete with a simple configuration on your models.

Status

npm version build status coverage status

Installation

Install the package via npm:

$ npm install --save bookshelf-cascade-delete

Usage

Require and register the bookshelf-cascade-delete plugin:

var bookshelf = require('bookshelf')(knex);
var cascadeDelete = require('bookshelf-cascade-delete');

// You need to access the `default` property since the plugin is transpilled from es6 modules syntax.
bookshelf.plugin(cascadeDelete.default);

Define which relations depend on your model when it's destroyed with the dependents prototype property:

var Post = bookshelf.Model.extend({
  tableName: 'Post'
});

var Author = bookshelf.Model.extend({
  tableName: 'Author',
  posts: function() {
    return this.hasMany(Post);
  }
}, {
  dependents: ['posts']
});

NOTE: This plugin extends the destroy method of Bookshelf's Model, so if you are extending or overriding it on your models make sure to call its prototype after your work is done:

var Author = bookshelf.Model.extend({
  tableName: 'Author',
  posts: function() {
    return this.hasMany(Post);
  },
  destroy: function() {
    // Do some stuff.
    sendDeleteAccountEmail(this);

    // Call the destroy prototype method.
    bookshelf.Model.prototype.destroy.apply(this, arguments);
  }
}, {
  dependents: ['posts']
});

Contributing

Feel free to fork this repository and submit pull requests. To run the tests, duplicate the test/knexfile.js.dist file, update it to your needs and run:

$ npm test

Credits

This plugin's code is heavily inspired on the tkellen contribution for this issue, so cheers to him for making our job really easy!

License

MIT

Keywords

FAQs

Package last updated on 21 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc