{"id":13578201,"url":"https://github.com/ivmarcos/sequelize-version","last_synced_at":"2025-04-05T16:32:01.207Z","repository":{"id":45540817,"uuid":"99025861","full_name":"ivmarcos/sequelize-version","owner":"ivmarcos","description":"Automatically version (audit, log) your sequelize models","archived":false,"fork":false,"pushed_at":"2021-12-09T13:29:01.000Z","size":148,"stargazers_count":55,"open_issues_count":6,"forks_count":19,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-05T15:49:31.973Z","etag":null,"topics":["audit","log","sequelize","version"],"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/ivmarcos.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-01T17:20:13.000Z","updated_at":"2024-03-26T19:46:24.000Z","dependencies_parsed_at":"2022-07-20T15:32:26.279Z","dependency_job_id":null,"html_url":"https://github.com/ivmarcos/sequelize-version","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivmarcos%2Fsequelize-version","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivmarcos%2Fsequelize-version/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivmarcos%2Fsequelize-version/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivmarcos%2Fsequelize-version/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivmarcos","download_url":"https://codeload.github.com/ivmarcos/sequelize-version/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247366425,"owners_count":20927505,"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":["audit","log","sequelize","version"],"created_at":"2024-08-01T15:01:28.380Z","updated_at":"2025-04-05T16:32:00.904Z","avatar_url":"https://github.com/ivmarcos.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/ivmarcos/sequelize-version.svg?branch=master)](https://travis-ci.org/ivmarcos/sequelize-version)\n# sequelize-version\nAutomatically version (audit, log) your sequelize models, tracking all the changes (create, update, delete) by generating a version of the model than can be used for easy\nquerying, see the changes made, or whatever you want. The version model uses sequelize hooks to persist the data.\n\n## Installation\n\n```shell\nnpm i sequelize-version --save\n```\nor\n```shell\nyarn add sequelize-version\n```\n## Features\n\n* Custom settings (version prefix, suffix, schema and more)\n* Supports transaction \n\n\n## Usage\n```js\nconst Sequelize = require('sequelize');\nconst Version = require('sequelize-version');\n\nconst sequelize = new Sequelize(...);\n\nconst Person = sequelize.define('Person', ...);\n\nconst PersonVersion = new Version(Person);\n```\n\n## Options\n\n|Name             |Type               |Default       |Description\n|-----------------|-------------------|--------------|--------------------------------\n|prefix           | `string`          | `'version'`  | Prefix for table name and version attributes\n|suffix           | `string`          |              | Table name suffix\n|attributePrefix  | `string`          |              | Overrides prefix for version attribute fields\n|schema           | `string`          |              | Version model schema, uses origin model schema as default\n|sequelize        | `sequelize`       |              | Sequelize instance, uses origin model sequelize as default\n|exclude          | `Array\u003cstring\u003e`   |              | Attributes to ignore \n|tableUnderscored | `boolean`         |   `true`     | Use underscore in version table name\n|underscored      | `boolean`         |   `true`     | Use underscore in version attributes\n\n## Examples\n\n### Checking versions\n```js\n// let's create a person for test\nlet person = await Person.build({name: 'Jack'}).save();\n\n// now we change a name\nperson.name = 'Jack Johnson';\n\n// update \nawait person.save();\n\n// and delete\nawait person.destroy();\n\n// finally, get the versions\nconst versions = await PersonVersion.findAll({where : {id: person.id}});\n\n// or, even simpler\nconst versionsByInstance = await person.getVersions();\n\n// this way too\nconst versionsByModel = await Person.getVersions({where : {id: person.id}});\n\n// versions added\nconsole.log(JSON.parse(JSON.stringify(versions)));\n/*\n[\n    {\n        version_id: 1,\n        version_type: 1,\n        version_timestamp: 2017-08-02T16:08:09.956Z,\n        id: 1,\n        name: 'Jack'\n    },\n    {\n        version_id: 2,\n        version_type: 2,\n        version_timestamp: 2017-08-02T16:18:09.958Z,\n        id: 1,\n        name: 'Jack Johnson'\n    },\n    {\n        version_id: 3,\n        version_type: 3,\n        version_timestamp: 2017-08-02T16:18:09.959Z,\n        id: 1,\n        name: 'Jack Johnson'\n    },\n]\n*/\n```\n### Custom options\n```js\n//customization examples\nconst customOptions = {\n    \n    //table name and version attributes prefix\n    prefix: '', \n\n    //table name suffix\n    suffix: 'log', \n\n    //attribute prefix (overrides default - prefix)\n    attributePrefix: 'revision', \n\n    //version model schema\n    schema: 'audit',\n\n    //you can use another sequelize instance (overrides default - sequelize from origin model)\n    sequelize: new Sequelize(...), \n\n    //attributes to ignore from origin model\n    exclude: ['createdAt', 'updatedAt'],\n\n    //table name with underscore, true as default\n    tableUnderscored: true,\n\n    //attributes with underscore, true as default\n    underscored: true,\n\n}\n\n// single options\nconst VersionModel = new Version(Model, customOptions);\n\n// global options\nVersion.defaults = customOptions;\n```\n\n\n### Transaction\n```js\n//version uses the origin model transaction\nsequelize.transaction(transaction =\u003e {\n    return Person.build({name: 'Jack'}).save({transaction});\n});\n\n//or, if you are using cls - http://docs.sequelizejs.com/manual/tutorial/transactions.html#automatically-pass-transactions-to-all-queries\nsequelize.transaction(() =\u003e {\n    return Person.build({name: 'Jack'}).save();\n})\n```\n\n### Querying\n```js\n// default scopes created in version model (created, updated, deleted)\nconst versionCreated = await VersionModel.scope('created').find({where: {id: person.id}});\n\nconst versionUpdates = await VersionModel.scope('updated').findAll();\n\nconst versionDeleted = await VersionModel.scope('deleted').find({where: {id: person.id}});\n\n// using origin model\nconst allVersions = await Person.getVersions({where : {name: {like: 'Jack%'}}});\n\n// using instance from origin model\nconst person = await Person.findById(1);\nconst versionsForOnlyThisPerson = await person.getVersions({where: {name: {like: '%Johnson'}}});\n```\n\n### Important Notes\nThis lib uses sequelize hooks to be able to track the changes. When using class methods, it is necessary to use the ```individualHooks: true``` option to make this possible. In such cases, this can cause a dramatic reduction in performance. See more at: https://sequelize.org/master/manual/hooks.html.\n```js\n// This will select all records that are about to be deleted and emit `beforeDestroy` and `afterDestroy` on each instance.\nawait Model.destroy({\n  where: { accessLevel: 0 },\n  individualHooks: true\n});\n\n// This will select all records that are about to be updated and emit `beforeUpdate` and `afterUpdate` on each instance.\nawait Model.update({ username: 'Jack' }, {\n  where: { accessLevel: 0 },\n  individualHooks: true\n});\n\n```\n\n## License\n\nThe files included in this repository are licensed under the MIT license.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivmarcos%2Fsequelize-version","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivmarcos%2Fsequelize-version","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivmarcos%2Fsequelize-version/lists"}