sequelize-model-trail
v1.0.1
Published
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.
Downloads
10
Maintainers
Readme
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.
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
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - 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:
GitHub's issue tracker
Thanks
This project was inspired by: