Record tables for Sequelize
npm install sequelize-recordRecord tables for Sequelize
===============================
Warning: this is a fork of sequelize-temporal that adds the ability to associate data history table to origin tables (table a record is based on) and to specify a different name for the __Record__ tables.
   
What is it?
-----------
___Record__ tables maintain __previous values__ of data. Modifying operations (UPDATE, DELETE) on these tables don't cause permanent changes to entries, but create new versions of them. Hence this might be used to:
- log changes (security/auditing)
- undo functionalities
- track interactions (customer support)
Under the hood a record table with the same structure, but without constraints is created (unless option __addAssociation__ is set to __true__).
The normal singular/plural naming scheme in Sequelize is used:
- model name: modelName + Record
- table name: modelName + Records
Installation
------------
``
`
npm install sequelize-record
`
How to use
----------
$3
`
var Sequelize = require('sequelize');
var Record = require('sequelize-record');
`
Create a sequelize instance and your models, e.g.
`
var sequelize = new Sequelize('', '', '', {
dialect: 'sqlite',
storage: __dirname + '/.test.sqlite'
});
`
$3
`
var User = Record(sequelize.define('User'), sequelize);
Record
The output of is its input model, so assigning it's output to your
`
Model is not necessary, hence it's just the lazy version of:
`
var User = sequelize.define('User', {.types.}, {.options.}); //Vanilla Sequelize
Record(User, sequelize);
Record
Options
-------
The default syntax for is:
Record(model, sequelizeInstance, options)
`
whereas the options are listed here (with default value).
js
``
{
/*
Runs the insert within the sequelize hook chain, disable
for increased performance without warranties */
blocking: true,
/*
By default sequelize-record persist only changes, and saves the previous state in the record table.
The "full" option saves all transactions into the record database
(i.e. this includes the latest state.)
This allows to only query the record table to get the full record of an entity.
*/
full: false,
/*
By default sequelize-record will add 'Record' to the record Model name and 'Records' to the record table.
By updating the modelSuffix value, you can decide what the naming will be.
The value will be appended to the record Model name and its plural will be appended to the record tablename.
examples for table User:
modelSuffix: '_Hist' --> Record Model Name: User_Hist --> Record Table Name: User_Hists
modelSuffix: 'Memory' --> Record Model Name: UserMemory --> Record Table Name: UserMemories
modelSuffix: 'Pass' --> Record Model Name: UserPass --> Record Table Name: UserPasses
*/
modelSuffix: 'Record',
/*
By default sequelize-record will create the record table without associations.
However, setting this flag to true, you can keep association between the record table and the table with the latest value (origin).
example for table User:
model: 'User'
record model: 'UserRecords'
--> This would add function User.getUserRecords() to return all record entries for that user entry.
--> This would add function UserRecords.getUser() to get the original user from an record.
If a model has associations, those would be mirrored to the record table.
Origin model can only get its own records.
Even if a record table is associated to another origin table thought a foreign key field, the record table is not accessible from that origin table
Basically, what you can access in the origin table can be accessed from the record table.
example:
model: User
record model: UserRecords
model: Creation
record model: CreationRecords
User <-> Creation: 1 to many
User.getCreations() exists (1 to many)
Creation.getUser() exists (1 to 1)
User <-> UserRecords: 1 to many
User.getUserRecords() exists (1 to many)
UserRecords.getUser() exists (1 to 1)
Creation <-> CreationRecords: 1 to many
Creation.getCreationRecords() exists (1 to many)
CreationRecords.getCreation() exists (1 to 1)
CreationRecords -> User: many to 1
CreationRecords.getUser() exists (1 to 1) (same as Creation.getUser())
User.GetCreationRecords DOES NOT EXIST. THE ORIGIN TABLE IS NOT MODIFIED.
UserRecords -> Creation: many to many
UserRecords.getCreations() exists (1 to many) (same as User.getCreations())
CreationRecords.getUser() DOES NOT EXIST. THE ORIGIN TABLE IS NOT MODIFIED.
*/
addAssociations: false
Details
--------
@See: https://wiki.postgresql.org/wiki/SQL2011Record