# loopback-changes-history-mixin

> Loopback mixin to register all changes in a record model.

Latest version **0.2.5** (published 2020-10-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install loopback-changes-history-mixin
pnpm add loopback-changes-history-mixin
yarn add loopback-changes-history-mixin
bun add loopback-changes-history-mixin
```

## 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.2.5 |
| Published | 2020-10-08 |
| First published | 2018-09-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 3 |
| Unpacked size | 51.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Alex J. Rondon |
| Maintainers | arondn2 |
| Keywords | nodejs, template, modules |

## Links

- npm: https://www.npmjs.com/package/loopback-changes-history-mixin
- Repository: https://github.com/arondn2/loopback-changes-history-mixin
- Homepage: https://github.com/arondn2/loopback-changes-history-mixin#readme
- Issues: https://github.com/arondn2/loopback-changes-history-mixin/issues
- npm.io page: https://npm.io/package/loopback-changes-history-mixin

## Dependencies (3)

- [md5](https://npm.io/package/md5.md) ^2.3.0
- [loopback](https://npm.io/package/loopback.md) ^3.27.0
- [supertest-as-promised](https://npm.io/package/supertest-as-promised.md) ^4.0.2

## Recent versions

- 0.2.5 (latest) — 2020-10-08
- 0.2.4 — 2020-10-08
- 0.2.3 — 2020-08-09
- 0.2.2 — 2020-08-09
- 0.2.1 — 2018-12-07
- 0.2.0 — 2018-10-22
- 0.1.0 — 2018-10-21
- 0.0.7 — 2018-10-10
- 0.0.5 — 2018-09-25
- 0.0.4 — 2018-09-25
- 0.0.3 — 2018-09-25
- 0.0.2 — 2018-09-23
- 0.0.1 — 2018-09-23

## README

loopback-changes-history-mixin
===============

[![npm version](https://badge.fury.io/js/loopback-changes-history-mixin.svg)](https://badge.fury.io/js/loopback-changes-history-mixin) [![Build Status](https://travis-ci.org/arondn2/loopback-changes-history-mixin.svg?branch=master)](https://travis-ci.org/arondn2/loopback-changes-history-mixin)
[![Coverage Status](https://coveralls.io/repos/github/arondn2/loopback-changes-history-mixin/badge.svg?branch=master)](https://coveralls.io/github/arondn2/loopback-changes-history-mixin?branch=master)

Loopback mixin to generate a new model to save history changes by record of a model.

## Installation

`npm install loopback-changes-history-mixin --save`

## Usage

Add the mixins property to your `server/model-config.json`:

```json
{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "../node_modules/loopback-changes-history-mixin",
      "../common/mixins"
    ]
  }
}
```

Add mixin params in model definition. Example:
```
{
  "name": "Person",
  "properties": {
    "name": "string",
    "email": "string",
    "status": "string",
    "description": "string"
  },
  "mixins": {
    "ChangeHistory": true
  }
}
```

In the above definition it will define the following:
- A model called `Person_history` with properties indicated in `fields`: `name`, `email`, `status` and `description`.
- Relation `Person` has many `history` (model `Person_history`, foreign key `_recordId`).
- Relation `Person_history` belongs `record` (model `Person`, foreign key `_recordId`).
- Properties `_version` and `_hash` as `string` in models `Person` and `Person_history`.
- Properties `_action` as `string` and `_update` as `date` in model `Person_history`.

Every time a change is made in a record of `Person`, it will be saved in `Person_history` a record with new values
and the following fields:
- `_version = <version code>`
- `_hash    = <hash code>`
- `_action  = <'create' or 'update' or 'delete'>`
- `_update  = <date of change>`

## Options

The mixin supports the following parameters:

 Name                 | Type                    | Default                | Optional | Description
----------------------|-------------------------|------------------------|----------|------------
 `fields`             | `array` or `string` `*` | `*`                    | No       | Array with the fields to version
 `modelName`          | `string`                | `${ModelName}_history` | No       | Name to history model
 `relationName`       | `string`                | `history`              | No       | Model has many history model relation name
 `relationParentName` | `string`                | `_record`              | No       | History model belongs to model relation name
 `relationForeignKey` | `string`                | `_recordId`            | No       | Foreign key for relations
 `versionFieldName`   | `string`                | `_version`             | No       | Field name to version code
 `versionFieldLen`    | `number`                | 5                      | No       | Length to `versionField`
 `hashFieldName`      | `string` or `false`     | `_hash`                | Yes      | Field name to hash code
 `hashFieldLen`       | `number`                | 10                     | No       | Length to `hashField`
 `actionFieldName`    | `string` or `false`     | `_action`              | Yes      | Field name to action name
 `updatedFieldName`   | `string` or `false`     | `_update`              | Yes      | Field name to update date

Notes:
- `hashFieldName` allow create a history change only if any property in fields was alter. If setup `hashFieldName: false` then a history change will be creates with update method called.
- If `hashFieldName` is `false` then `hashFieldLen` is. ignoored.

## Loopback methods

Generates create records
  - `Model.create`
  - `Model.updateOrCreate` (AKA `Model.upsert`)
  - `Model.findOrCreate`
  - `Model.replaceOrCreate`
  - `Model.upsertWithWhere` (view `Model.upsertWithWhere` section below)

Generates update records
  - `Model.updateOrCreate` (AKA `Model.upsert`)
  - `Model.replaceOrCreate`
  - `Model.upsertWithWhere` (view `Model.upsertWithWhere` section below)
  - `Model.replaceById`
  - `Model.prototype.save`
  - `Model.prototype.updateAttribute`
  - `Model.prototype.updateAttributes`
  - `Model.prototype.replaceAtributes`

Generates destroy records
  - `Model.prototype.delete` (AKA `Model.prototype.destroy`)
  - `Model.deleteById` (AKA `Model.destroyById`)

Unsupported methods
  - `Model.updateAll`
  - `Model.deleteAll` (AKA `Model.destroyAll`)

### Model.upsertWithWhere
To create history change with method `upsertWithWhere` option `instanceByWhere: true` must be passed:
```js
const where = { /* ... */};
const data = { /* ... */ };
Person.upsertWithWhere(where, data, { instanceByWhere: true });
```

## Troubles

If you have any kind of trouble with it, just let me now by raising an issue on the GitHub issue tracker here:

https://github.com/arondn2/loopback-changes-history-mixin/issues

Also, you can report the orthographic errors in the READMEs files or comments. Sorry for that, English is not my main language.

## Tests

`npm test` or `npm run cover`

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