# sequelize-version

> Automatically version (audit, log) your sequelize models

Latest version **0.0.58** (published 2019-04-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install sequelize-version
pnpm add sequelize-version
yarn add sequelize-version
bun add sequelize-version
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.58 |
| Published | 2019-04-16 |
| First published | 2017-08-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 55 |
| Author | Marcos Andrei Ivanechtchuk |
| Maintainers | ivmarcos |
| Keywords | sequelize, version, audit, log, history |

## Links

- npm: https://www.npmjs.com/package/sequelize-version
- Repository: https://github.com/ivmarcos/sequelize-version
- Homepage: https://github.com/ivmarcos/sequelize-version#readme
- Issues: https://github.com/ivmarcos/sequelize-version/issues
- npm.io page: https://npm.io/package/sequelize-version

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 0.0.58 (latest) — 2019-04-16
- 0.0.57 — 2018-06-07
- 0.0.56 — 2018-06-01
- 0.0.55 — 2018-05-29
- 0.0.54 — 2018-01-03
- 0.0.53 — 2018-01-03
- 0.0.52 — 2017-08-11
- 0.0.51 — 2017-08-11
- 0.0.50 — 2017-08-10
- 0.0.49 — 2017-08-09
- 0.0.48 — 2017-08-09
- 0.0.46 — 2017-08-09
- 0.0.45 — 2017-08-09
- 0.0.44 — 2017-08-07
- 0.0.43 — 2017-08-07
- … 34 more at https://npm.io/package/sequelize-version/versions

## README

[![Build Status](https://travis-ci.org/ivmarcos/sequelize-version.svg?branch=master)](https://travis-ci.org/ivmarcos/sequelize-version)
# 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

```shell
npm i sequelize-version --save
```
or
```shell
yarn add sequelize-version
```
## Features

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


## Usage
```js
const Sequelize = require('sequelize');
const Version = require('sequelize-version');

const sequelize = new Sequelize(...);

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

const PersonVersion = new Version(Person);
```

## Options

|Name             |Type               |Default       |Description
|-----------------|-------------------|--------------|--------------------------------
|prefix           | `string`          | `'version'`  | Prefix for table name and version attributes
|suffix           | `string`          |              | Table name suffix
|attributePrefix  | `string`          |              | Overrides prefix for version attribute fields
|schema           | `string`          |              | Version model schema, uses origin model schema as default
|sequelize        | `sequelize`       |              | Sequelize instance, uses origin model sequelize as default
|exclude          | `Array<string>`   |              | Attributes to ignore 
|tableUnderscored | `boolean`         |   `true`     | Use underscore in version table name
|underscored      | `boolean`         |   `true`     | Use underscore in version attributes

## Examples

### Checking versions
```js
// 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
```js
//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
```js
//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
```js
// 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.

---
_Source: https://npm.io/package/sequelize-version · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
