Giter VIP home page Giter VIP logo

sequelize-reassociate's Introduction

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.
  // pass in the reference to sequelize after models have been initialized
  reassociate(sequelize);
  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

sequelize-reassociate's People

Contributors

tmw avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

sketch-hq

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.