6.0.0 • Published 2 years ago

sequelize-revision v6.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Sequelize Revision

npm-version npm-downloads CircleCI license

TIPS | Troubleshooting | Code of Conduct | Contributing | Security Policy | Changelog

Track revisions of your Sequelize models, revert them to any revision or restore them after being destroyed. Written in TypeScript and can be used with sequelize-typescript.

Sequelize Revision is a fork from Sequelize Paper Trail supporting similar options and providing consistent behavior with following improvements:

  • Re-written in TypeScript and support type checks
  • Working well with or without sequelize-typescript
  • Tracking users for making changes with cls-hooked
  • Support JSON data type for storing revisions
  • Exclude revision attributes for each model
  • Passing revision metadata to operations
  • Logging revisions for upsert operations
  • Better coverage in unit tests

Installation

$ npm install --save sequelize-revision

Usage

Sequelize Revision assumes that you already set up your Sequelize connection, for example, like this:

import { Sequelize } from "sequelize";

const sequelize = new Sequelize("sqlite::memory:");

then adding Sequelize Revision is as easy as:

import { SequelizeRevision } from "sequelize-revision";

const sequelizeRevision = new SequelizeRevision(sequelize, options);
const [Revision, RevisionChanges] = sequelizeRevision.defineModels();

which loads the Sequelize Revision library, and the defineModels() method sets up a Revision and RevisionChange models.

Note: If you pass userModel option to the constructor in order to enable user tracking, userModel should be setup before defineModels() is called.

Then for each model that you want to keep a paper trail you simply add:

sequelizeRevision.trackRevision(Model);

Example

import Sequelize from "sequelize";
import { SequelizeRevision } from "sequelize-revision";

const sequelize = new Sequelize("sqlite::memory:");
const sequelizeRevision = new SequelizeRevision(sequelize, options);
const [Revision, RevisionChanges] = sequelizeRevision.defineModels();

const User = sequelize.define("User", { name: Sequelize.STRING });
sequelizeRevision.trackRevision(User);

Options

Sequelize Revision supports various options that can be passed into the initialization. The following are the default options:

Options documentation

OptionTypeDefault ValueDescription
excludeArray["id", "createdAt", "updatedAt", "deletedAt", "created_at", "updated_at", "deleted_at", [options.revisionAttribute]]Array of global attributes to exclude from the Sequelize Revision.
revisionAttributeString"revision"Name of the attribute in the table that corresponds to the current revision.
revisionIdAttributeString"revisionId"Name of the attribute in the RevisionChange model that corresponds to the ID of the Revision model. Attribute name can be modified to "revision_id" by passing underscoredAttributes option.
revisionModelString"Revision"Name of the model that keeps the revision models.
tableNameStringundefinedName of the table that keeps the revision models. Passed to Sequelize.
changeTableNameStringundefinedName of the table that keeps the revision change models. Passed to Sequelize. Table is camelCase or PascalCase.
revisionChangeModelString"RevisionChange"Name of the model that tracks all the attributes that have changed during each create and update call.
enableRevisionChangeModelBooleanfalseDisable the revision change model to save space.
UUIDBooleanfalseThe revisionModel has id attribute of type UUID for PostgreSQL.
underscoredBooleanfalseThe revisionModel and revisionChangeModel have "createdAt" and "updatedAt" columns, by default, setting this option to true changes it to "created_at" and "updated_at". Pass true to underscoredAttributes option as well for using underscored attributes to access those columns.
underscoredAttributesBooleanfalseThe revisionModel has "documentId", and the revisionChangeModel has a defaultAttributes.revisionId "revisionId, by default, setting this option to true changes it to "document_id" and "revision_id".
userModelStringundefinedName of the model that stores users in your application.
userIdAttributeString"userId"Name of the attribute in the RevisionChange model that corresponds to the ID of the User model. Attribute name can be modified to "user_id" by passing underscoredAttributes option.
enableCompressionBooleanfalseCompresses the revision attribute in the revisionModel to only the diff instead of all model attributes.
enableMigrationBooleanfalseAutomatically adds the revisionAttribute via a migration to the models that have paper trails enabled.
enableStrictDiffBooleantrueReports integers and strings as different, e.g. 3.14 !== "3.14"
continuationNamespaceStringundefinedName of the name space used with the cls-hooked module.
continuationKeyString"userId"The cls-hooked key that contains the user id.
belongsToUserOptionsObjectundefinedThe options used for belongsTo between userModel and Revision model
metaDataFieldsObjectundefinedThe keys that will be provided in the meta data object. { key: isRequired (boolean)} format. Can be used to privovide additional fields - other associations, dates, etc to the Revision model
metaDataContinuationKeyString"metaData"The cls-hooked key that contains the meta data object, from where the metaDataFields are extracted.
useJsonDataTypeBooleantrueMicrosoft SQL Server and old versions of MySQL do not support JSON data type. Setting this option to false changes the data type to TEXT("medium").

Sequelize Models

Revision

AttributeData TypeDescription
"id"INTEGER | UUIDPrimary key of the model. Data type is INTEGER by default, but can be modified to UUID for PostgreSQL by passing UUID option.
"model"STRINGName of the tracked model.
"document"JSON | TEXT("medium")Attributes of the model after the operation. The entire attributes are saved by default, but can be compresses only to the diff by passing enableCompression option.
"documentId"INTEGER | UUIDID of the tracked document. Attribute name can be modified to "document_id" by passing underscoredAttributes option.
"operation"STRINGName of the operation such as "create", "update" and "destroy".
revisionAttributeINTEGERVersion of the document starting from 1. The value is incremented every time when the document is created, updated or deleted. Attribute name is configured by revisionAttribute, default to "revision". The same value is saved to revisionAttribute attribute of the tracked document as well.
userIdAttribute(ID data type of of the user model)Foreign key of the user model. Attribute name is configured by userIdAttribute, default to "userId" and modified to "user_id" by passing underscoredAttributes option.

RevisionChnage

AttributeData TypeDescription
"id"INTEGER | UUIDPrimary key of the model. Data type is INTEGER by default, but can be modified to UUID for PostgreSQL by passing UUID option.
"path"TEXTModified attribute name.
"document"JSON | TEXT("medium")Modified attributes.
"diff"JSON | TEXT("medium")Difference of updated attribute, comparing character by character.
revisionIdAttribute(ID data type of of the Revision model)Foreign key of the Revision model. Attribute name is configured by revisionIdAttribute, default to "revisionId" and modified to "revision_id" by passing underscoredAttributes option.
5.2.1

2 years ago

5.2.0

2 years ago

5.0.2

2 years ago

5.1.0

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

6.0.0

2 years ago

4.0.0

2 years ago

3.1.0

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago