Socket
Socket
Sign inDemoInstall

sequelize-paranoid-cascade

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    sequelize-paranoid-cascade

sequelize-paranoid-cascade allows cascading deletions when using paranoid mode


Version published
Maintainers
1
Created

Readme

Source

sequelize-paranoid-cascade

sequelize-paranoid-cascade allows cascading deletions when using paranoid mode in sequelize

Highlights

  • Written in TypeScript
  • Extrely easy to use
  • Database agnostic (in progress)
  • Integrates with Umzug and any othe library for running migrations with sequelize

Current Support

  • MySql
  • Creating triggers when creating tables that have columns which reference other tables
  • creating a trigger when creating a column which references other table

There are a couple of use case missing:

  • support column delete (remove trigger in referenced table)
  • support rename column (rename trigger)
  • support rename table (rename trigger)
  • support for adding extra conditions to the triggers (multi tenant support)
  • maybe another?

Minimal Example (using Umzug)

The following example uses a MySql database through sequelize and persists the migration data in the database itself through the sequelize storage.

// index.js
const { Sequelize } = require('sequelize');
const { Umzug, SequelizeStorage } = require('umzug');
const { queryInterfaceDecorator } = require('sequelize-paranoid-cascade');

const sequelize = new Sequelize({ dialect: 'mysql', storage: './db.mysql' });

const umzug = new Umzug({
  migrations: { glob: 'migrations/*.js' },
  // as simple as the following line
  context: queryInterfaceDecorator(sequelize.getQueryInterface()),
  storage: new SequelizeStorage({ sequelize }),
  logger: console,
});
// migrations/00_initial.js
const { Sequelize } = require('sequelize');

async function up({ context: queryInterface }) {
  await queryInterface.createTable('resources', {
    id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      primaryKey: true,
    },
    name: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    userId: {
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: 'users',
        key: 'id',
      },
      // here we specify the behavior
      onDelete: 'PARANOID CASCADE',
    }
    createdAt: {
      type: Sequelize.DATE,
      allowNull: false,
    },
    updatedAt: {
      type: Sequelize.DATE,
      allowNull: false,
    },
    deletedAt: {
      type: Sequelize.DATE,
      allowNull: true,
    },
  });
}

This is going to create a trigger in the users table, when a user gets "deleted", the resource will be marked as deleted as well.

If the table was already created, you can use addColumn as well.

const { Sequelize } = require('sequelize');

async function up({ context: queryInterface }) {
  await queryInterface.addColumn('resources', 'userId', {
    type: Sequelize.INTEGER,
    allowNull: false,
    references: {
      model: 'users',
      key: 'id',
    },
    // here we specify the behavior
    onDelete: 'PARANOID CASCADE',
  });
}

Usage

Installation

sequelize-paranoid-cascade is available on npm by specifying the correct tag:

npm install sequelize-paranoid-cascade

OR

yarn add sequelize-paranoid-cascade

License

See the LICENSE file

Keywords

FAQs

Last updated on 02 Jan 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc