{"id":19755327,"url":"https://github.com/awkward/sequelize-reassociate","last_synced_at":"2025-02-28T01:47:21.338Z","repository":{"id":68797534,"uuid":"79361110","full_name":"awkward/sequelize-reassociate","owner":"awkward","description":"A simple abstraction for Sequelize.js to relocate associated models if their parents ever were to be destroyed.","archived":false,"fork":false,"pushed_at":"2017-02-22T16:20:25.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-12T09:47:38.274Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/awkward.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-18T16:44:06.000Z","updated_at":"2018-02-27T11:09:35.000Z","dependencies_parsed_at":"2023-03-02T12:30:29.545Z","dependency_job_id":null,"html_url":"https://github.com/awkward/sequelize-reassociate","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"8fdb4181dacfd7f3ce0a9f1377182bd917a732e4"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awkward%2Fsequelize-reassociate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awkward%2Fsequelize-reassociate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awkward%2Fsequelize-reassociate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awkward%2Fsequelize-reassociate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/awkward","download_url":"https://codeload.github.com/awkward/sequelize-reassociate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241086291,"owners_count":19907276,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T03:10:21.609Z","updated_at":"2025-02-28T01:47:21.312Z","avatar_url":"https://github.com/awkward.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](https://travis-ci.org/awkward/sequelize-reassociate.svg)\n# sequelize-reassociate\nA simple abstraction for the [Sequelize](http://docs.sequelizejs.com/en/v3/) to reassociate a models associations if it ever were to be destroyed.\n\n## What?\nSequelize-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 `Comment`s, we can transfer the `Comment`s to a Ghost-user (like GitHub does) so the context of the conversation can be preserved. It currently only works for `HasMany` associations.\n\n## How?\n\n1) Download the module:\n```\n npm install @awkward/sequelize-reassociate --save\n ```\n \n2) Require the module:\n``` javascript\n const reassociate = require('@awkward/sequelize-reassociate');\n```\n\n3) Setup the module:\nin 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.\n\n``` javascript\n  // pass in the reference to sequelize after models have been initialized\n  reassociate(sequelize);\n```\n\n4) Using the module:\nNow to use the module, let the models know where its associations can be moved to using a public method which returns an object:\n\n``` javascript\nconst User = sequelize.define('user', {\n    // snip ...\n  },\n  {\n    classMethods: {\n      associate: function(models) {\n        User.hasMany(models.Comment);\n      },\n\n      reassociate: function() {\n        // if the User gets destroyed, its Comments go to User with id 1\n        return { \n          Comments: 1, \n        };\n      }\n    }\n  }\n});\n\n```\n\nBasically 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.\n\n## License\n[MIT](https://github.com/awkward/sequelize-reassociate/blob/master/LICENSE)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fawkward%2Fsequelize-reassociate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fawkward%2Fsequelize-reassociate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fawkward%2Fsequelize-reassociate/lists"}