/sequelize-model-trail

Package that helps you track changes to your models, for auditing or versioning. See how a model looked at any stage in its life-cycle and also allows you to Record the user who created the version.

Primary LanguageJavaScriptMIT LicenseMIT

Sequelize Model Trail

Package that helps you track changes to your models, for auditing or versioning. See how a model looked at any stage in its life-cycle and also allows you to Record the user who created the version.

node-version npm-version

GitHub release GitHub tag GitHub commits npm-downloads

license GitHub Sponsor

Table of Contents

Installation

npm install --save sequelize-model-trail

Usage

Sequelize Model Trail assumes that you have already set up your Sequelize connection, for example, like this:

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

then adding Sequelize Model Trail is as easy as:

const ModelTrail = require('sequelize-model-trail').init(sequelize);
ModelTrail.enableAndLoadModelTrail();

which loads the Model Trail library, and the enableAndLoadModelTrail() method sets up a revisions table and Model.

Note: You need to Pass userId as options if you want to enable user tracking

User Tracking

Currently, there is only one step to enable user tracking, ie, recording the user who created a particular revision.

Model.update({
  /* ... */
}, {
  userId: user.id
}).then(() {
  /* ... */
});

How data is stored

+----+-------+--------------------------------------+-----------+------------+---------------------+---------------------+--------+ | id | model | document | operation | documentId | createdAt | updatedAt | userId | +----+-------+--------------------------------------+-----------+------------+---------------------+---------------------+--------+ | 1 | users | {"id": [1, null], "name": ["Salman", null]} | create | 1 | 2022-08-22 01:17:34 | 2022-08-22 01:17:34 | NULL | +----+-------+--------------------------------------+-----------+------------+---------------------+---------------------+--------+ +----+-------+--------------------------------------+-----------+------------+---------------------+---------------------+--------+ | 2 | users | {"name": ["Salman Zafar", "Salman"]} | update | 1 | 2022-08-22 01:20:10 | 2022-08-22 01:20:10 | 1 | +----+-------+--------------------------------------+-----------+------------+---------------------+---------------------+--------+ +----+-------+--------------------------------------+-----------+------------+---------------------+---------------------+--------+ | 3 | users | {} | destroy | 1 | 2022-08-22 01:21:30 | 2022-08-22 01:21:30 | 1 | +----+-------+--------------------------------------+-----------+------------+---------------------+---------------------+--------+

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Author

© Salman Zafar@salmanzafar949

Distributed under the MIT license. See LICENSE for more information. https://github.com/salmanzafar949/sequelize-model-trail

Contributors: https://github.com/salmanzafar949/sequelize-paper-trail/graphs/contributors

example

Upcoming features

  • Specify Models for Model Trails
  • UUID
  • CUSTOM MODEL/TABLE NAME
  • CUSTOM OPTIONS

Support

If this project help you reduce time to develop, you can give me a cup of coffee :)

Please use:

Thanks

This project was inspired by:

Happy Coding.!