1.7.3 • Published 8 months ago

sequelize-central-log v1.7.3

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Sequelize Central Log

styled with prettier license

Maintain Sequelize Model change history in a central table. This is highly configurable and supports composite primary keys ( up to 3 for now). Written in Typescript and provides typing.

Can be used with Sequelize-typescript as well.

Table of Contents

Install

To install run the following npm command:

npm install sequelize-central-log

Usage

Setup

Sequelize Central log needs your initated sequelize connection for example:

import { Sequelize } from 'sequelize';

const sequelize = new Sequelize('database', 'username', 'password');

Import Sequelize Central Log, create a new instance by passing in your sequelize instance and options ( see below ) as well as defineModels() to setup the revision model, which should be done after initalizing them in sequelize.

import { SequelizeCentralLog } from 'sequelize-central-log';
const options = {...}; // define options from list below
const centralLog = new SequelizeCentralLog(sequelize, options);
const Revision = centralLog.defineModels(); // returns your revision model instance for querying

This sets up your central log (revision table) and returns the table instance to be used for querying.

Lastly for each model you want to track history on call the addHistory function passing in the model.

centralLog.addHistory(sequelize.model('ModelName'));

// For those that define their models as a class, you can simply pass that class in.
import SequelizeModelClass from '../sequelize_model_class';
centralLog.addHistory(SequelizeModelClass);

The hasHistory function will add a hasMany association from the model to the revision model so you can include and have revisions returned with your results.

Excluding Columns

There are two ways to exclude columns from being tracked in the revision history table.

You can pass them in as a string array your options when instantiating the Sequelize Central Log ( this will override the defaults). See options below for the defaults. You can override the defaults by passing an empty array.

const centralLog = new SequelizeCentralLog(sequelizeDB, { exclude: ['column1', 'column2']});

or you can pass them in per model as a string array when adding the history to the sequelize model:

centralLog.addHistory(ModelA, {exclude: ['columnModelA']});
centralLog.addHistory(ModelB);

General exclude and model level exclude are added together on the model level. So a globally excluded id column will apply to every model while a column excluded when adding history to the model will be excluded only for that table.

Example based on the setup above: 1. Table A will have the columns id and columnModelA excluded. 2. Table B will only have the id column excluded.

Composite Keys

You can use composite keys while tracking history. Current limitations are limited to 2 keys currently. The second key is tracked in a column added to the revision table modelId2, (which you can override the column name). A Third Composite key works the same with a default of modelId3.

This is configured by passing the option when creating the log and then per model.

const centralLog = new SequelizeCentralLog(sequelizeDB, {useCompositeKeys: true});
centralLog.addHistory(Model, { hasCompositeKey: true });

/**
 * For a Third Composite Key
 */
centralLog.addHistory(Model, { hasCompositeKey: true, thirdCompositeKey: true });

This will still create the hasMany association on the model and will pull back the revisions. YAY!

Tracking Author

You can track the author of the revision by passing the user model as an option when creating the SequelizeCentralLog instance( model must be initialized in sequelize first).

const centralLog = SequelizeCentralLog(sequelizeDB, {userModel: User});

This will create a foreign key ( allowed null ) column on the revision table pointing to the id column in the user model.

There are two ways to set the userid when tracking the author. You can use a namespace (cls-hooked) or pass a userId as an option on the sequelize query. The option passed on the query will override the namespace if you want to for some reason.

import { createNamespace } from 'cls-hooked';

const nameSpace = createNamespace('amespaceName');

// don't forget to call run on the namespace.
nameSpace.run(() => {
  nameSpace.set('keyName', userID);
  // somehwere in your async chain..
  Model.update(...{});
  Model.create(...{});
});

/**
 * OR
 */

Model.update({...values}, {userId: userID});

Bulk Actions

By default Sequelize Central Log will track bulk actions. You can disable this on a per model basis.

CentralLog.hasHistory(Model, {disableHistoryAutoHook: true});

Options

OptionTypeDefault ValueDescription
attributeModelIdStringmodelIDcolumn name for primary key of model tracked.
attributeModelId2StringmodelID2column name for the 2nd primary key for a composite key.
attributeRevisionStringrevisioncolumn name added to model being tracked with hasHistory()
attributeRevisionModelStringRevisionrevision table sequelize model name
attributeRevisionModelTableNameStringRevisionrevision table name in database, if different.
attributeUserIdStringuserIdcolumn name on revision table for the user id.
continuationKeyStringuserIdcontinuation key to set / get when tracking author.
continuationNamespaceStringNULLcontinuation namespace name.
debugBooleanfalseEnables console logging.
enableMigrationBooleanfalseEnables Database sync of only the revision model so the table will be created in database.
enableRevisionAttributeMigrationBooleanfalseEnables addHistory to add the revision column on the model in the database.
excludeArray(string)['id', 'createdAt', 'updatedAt', 'deletedAt', 'created_at', 'updated_at', 'deleted_at', options.revisionAttribute]Array of global attributes to exclude from the revision log.
freezeTableNameBooleanfalseAdds revision model level option of freeze table name.
primaryKeyTypeDataTypeDataTypes.IntegerAllows Defining something Other than Integer as the Column Type. This uses Sequelizes DataTypes Field.
trackFullModelBooleanfalseAdds another column (current) to the table and tracks the full non excluded current values ( not just what changed ).
useCompositeKeysBooleanfalseDenotes that composite keys are used in your models and adds the attributeModelId2 column to the revision table.
underscoredBooleanfalseAdds revision model level option of underscored column names ( created_at ).
userModelModel ObjectNULLSequelize Model instance for the user.

Support

Please use GitHub's Issue Tracker

Author

© Jacob Copeland

Distributed under the MIT license. See LICENSE for more information.

Thanks

This project was inspired by:

1.7.3

8 months ago

1.7.2

8 months ago

1.7.1

8 months ago

1.7.0

8 months ago

1.6.2

1 year ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.1

2 years ago

1.4.2

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago