Giter VIP home page Giter VIP logo

bookshelf-cascade-delete's Introduction

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

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.

Setting up

  • Fork and clone the bookshelf-cascade-delete repository.
  • Duplicate test/postgres/knexfile.js.dist and test/mysql/knexfile.js.dist files and update them to your needs.
  • Make sure all the tests pass:
$ npm test

Linting

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

Pull Request

Please follow these advices to simplify the pull request workflow:

  • If you add or enhance functionality, an update of README.md usage section should be part of the PR.
  • If your PR fixes a bug you should include tests that at least fail before your code changes and pass after.
  • Keep your branch rebased and fix all conflicts before submitting.
  • Make sure Travis build status is ok.

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

bookshelf-cascade-delete's People

Contributors

abelsoares avatar nunorafaelrocha avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.