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

@awkward/sequelize-reassociate

Package Overview
Dependencies
Maintainers
6
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@awkward/sequelize-reassociate

A simple abstraction for Sequelize to reassociate a models associations if it ever were to be destroyed.

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created
Source

sequelize-reassociate

A simple abstraction for the Sequelize to reassociate a models associations if it ever were to be destroyed.

What?

Sequelize-reassociate basically moves a models associations to a provided record if the parent model were to be destroyed. For example, if a User model owns many Comments, we can transfer the Comments to a Ghost-user (like GitHub does) so the context of the conversation can be preserved. It currently only works for HasMany associations.

How?

  1. Download the module:
 npm install @awkward/sequelize-reassociate --save
  1. Require the module:
 const reassociate = require('@awkward/sequelize-reassociate');
  1. Setup the module: in your index.js file (or wherever you initialize Sequelize) just call the function and pass in your initialized sequelize instance. Make sure the models already have been initialized at this point.
  // ..sequelize setting up code
 
  db.sequelize = sequelize;
  db.Sequelize = Sequelize;

  reassociate(db);
  1. Using the module: Now to use the module, let the models know where its associations can be moved to using a public method which returns an object:
const User = sequelize.define('User', {
    // snip ...
  },
  {
    classMethods: {
      associate: function(models) {
        User.hasMany(models.Comment);
      },

      reassociate: function() {
        // if the User gets destroyed, its Comments go to User with id 1
        return { 
          Comments: 1, 
        };
      }
    }
  }
});

Basically the heart is in the reassociate() method. The return value should be an object containing the association (either the Model name or the reference when using as in the association) and the value is the foreign key pointing to the new owner.

License

MIT

Keywords

FAQs

Package last updated on 19 Jan 2017

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