0.0.58 • Published 5 years ago

sequelize-version v0.0.58

Weekly downloads
667
License
MIT
Repository
github
Last release
5 years ago

Build Status

sequelize-version

Automatically version (audit, log) your sequelize models, tracking all the changes (create, update, delete) by generating a version of the model than can be used for easy querying, see the changes made, or whatever you want. The version model uses sequelize hooks to persist the data.

Installation

npm i sequelize-version --save

or

yarn add sequelize-version

Features

  • Custom settings (version prefix, suffix, schema and more)
  • Supports transaction

Usage

const Sequelize = require('sequelize');
const Version = require('sequelize-version');

const sequelize = new Sequelize(...);

const Person = sequelize.define('Person', ...);

const PersonVersion = new Version(Person);

Options

NameTypeDefaultDescription
prefixstring'version'Prefix for table name and version attributes
suffixstringTable name suffix
attributePrefixstringOverrides prefix for version attribute fields
schemastringVersion model schema, uses origin model schema as default
sequelizesequelizeSequelize instance, uses origin model sequelize as default
excludeArray<string>Attributes to ignore
tableUnderscoredbooleantrueUse underscore in version table name
underscoredbooleantrueUse underscore in version attributes

Examples

Checking versions

// let's create a person for test
let person = await Person.build({name: 'Jack'}).save();

// now we change a name
person.name = 'Jack Johnson';

// update 
await person.save();

// and delete
await person.destroy();

// finally, get the versions
const versions = await PersonVersion.findAll({where : {id: person.id}});

// or, even simpler
const versionsByInstance = await person.getVersions();

// this way too
const versionsByModel = await Person.getVersions({where : {id: person.id}});

// versions added
console.log(JSON.parse(JSON.stringify(versions)));
/*
[
    {
        version_id: 1,
        version_type: 1,
        version_timestamp: 2017-08-02T16:08:09.956Z,
        id: 1,
        name: 'Jack'
    },
    {
        version_id: 2,
        version_type: 2,
        version_timestamp: 2017-08-02T16:18:09.958Z,
        id: 1,
        name: 'Jack Johnson'
    },
    {
        version_id: 3,
        version_type: 3,
        version_timestamp: 2017-08-02T16:18:09.959Z,
        id: 1,
        name: 'Jack Johnson'
    },
]
*/

Custom options

//customization examples
const customOptions = {
    
    //table name and version attributes prefix
    prefix: '', 

    //table name suffix
    suffix: 'log', 

    //attribute prefix (overrides default - prefix)
    attributePrefix: 'revision', 

    //version model schema
    schema: 'audit',

    //you can use another sequelize instance (overrides default - sequelize from origin model)
    sequelize: new Sequelize(...), 

    //attributes to ignore from origin model
    exclude: ['createdAt', 'updatedAt'],

    //table name with underscore, true as default
    tableUnderscored: true,

    //attributes with underscore, true as default
    underscored: true,

}

// single options
const VersionModel = new Version(Model, customOptions);

// global options
Version.defaults = customOptions;

Transaction

//version uses the origin model transaction
sequelize.transaction(transaction => {
    return Person.build({name: 'Jack'}).save({transaction});
});

//or, if you are using cls - http://docs.sequelizejs.com/manual/tutorial/transactions.html#automatically-pass-transactions-to-all-queries
sequelize.transaction(() => {
    return Person.build({name: 'Jack'}).save();
})

Querying

// default scopes created in version model (created, updated, deleted)
const versionCreated = await VersionModel.scope('created').find({where: {id: person.id}});

const versionUpdates = await VersionModel.scope('updated').findAll();

const versionDeleted = await VersionModel.scope('deleted').find({where: {id: person.id}});

// using origin model
const allVersions = await Person.getVersions({where : {name: {like: 'Jack%'}}});

// using instance from origin model
const person = await Person.findById(1);
const versionsForOnlyThisPerson = await person.getVersions({where: {name: {like: '%Johnson'}}});

License

The files included in this repository are licensed under the MIT license.

0.0.58

5 years ago

0.0.57

6 years ago

0.0.56

6 years ago

0.0.55

6 years ago

0.0.54

6 years ago

0.0.53

6 years ago

0.0.52

7 years ago

0.0.51

7 years ago

0.0.50

7 years ago

0.0.49

7 years ago

0.0.48

7 years ago

0.0.46

7 years ago

0.0.45

7 years ago

0.0.44

7 years ago

0.0.43

7 years ago

0.0.42

7 years ago

0.0.41

7 years ago

0.0.40

7 years ago

0.0.39

7 years ago

0.0.38

7 years ago

0.0.36

7 years ago

0.0.34

7 years ago

0.0.33

7 years ago

0.0.32

7 years ago

0.0.31

7 years ago

0.0.30

7 years ago

0.0.29

7 years ago

0.0.28

7 years ago

0.0.27

7 years ago

0.0.25

7 years ago

0.0.24

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.21

7 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.8

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago