
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
bookshelf-cascade-delete
Advanced tools
This Bookshelf.js plugin provides cascade delete with a simple configuration on your models.
Install the package via npm
:
$ npm install --save bookshelf-cascade-delete
Require and register the bookshelf-cascade-delete
plugin:
var bookshelf = require('bookshelf')(knex);
var cascadeDelete = require('bookshelf-cascade-delete');
bookshelf.plugin(cascadeDelete);
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']
});
If you're using the ES6 class syntax, define dependents
as static property:
class Author extends bookshelf.Model {
get tableName() {
return 'Author';
}
posts() {
return this.hasMany(Post);
}
static dependents = ['posts'];
}
Use destroy
to delete your model:
Author.forge({ id: 1 }).destroy();
A transaction is created and all the cascade queries executed:
DELETE FROM "Post" where "author_id" IN (1)
DELETE FROM "Author" where "id" IN (1)
You can pass an existing transaction as you would normally do:
bookshelf.transaction(function(transaction) {
return Author.forge({ id: 1 }).destroy({ transacting: transaction })
}).then(function() {
return Author.forge({ id: 2 }).destroy({ transacting: transaction })
});
It's possible to disable the cascade delete with the cascadeDelete
option:
Author.forge({ id: 1 }).destroy({ cascadeDelete: false });
Since this plugin extends the destroy
method, if you're 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']
});
Contributions are welcome and greatly appreciated, so feel free to fork this repository and submit pull requests.
bookshelf-cascade-delete supports PostgreSQL and MySQL. You can find test suites for each of these database engines in the test/postgres and test/mysql folders.
$ npm test
bookshelf-cascade-delete enforces linting using ESLint with the Seegno-flavored ESLint config. We recommend you to install an eslint plugin in your editor of choice, although you can run the linter anytime with:
$ eslint src test
Please follow these advices to simplify the pull request workflow:
This plugin's code is heavily inspired on the tkellen contribution for this issue, so cheers to him for making our job really easy!
2.0.1 (2016-12-13)
Closed issues:
Merged pull requests:
FAQs
Cascade delete with Bookshelf.js
The npm package bookshelf-cascade-delete receives a total of 986 weekly downloads. As such, bookshelf-cascade-delete popularity was classified as not popular.
We found that bookshelf-cascade-delete demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.