Loopback mixin to register all changes in a record model.
npm install loopback-changes-history-mixinloopback-changes-history-mixin
===============
 

Loopback mixin to generate a new model to save history changes by record of a model.
npm install loopback-changes-history-mixin --save
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.Person
- Relation has many history (model Person_history, foreign key _recordId).Person_history
- Relation belongs record (model Person, foreign key _recordId)._version
- Properties and _hash as string in models Person and Person_history._action
- Properties 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_version =
and the following fields:
- _hash =
- _action = <'create' or 'update' or 'delete'>
- _update =
-
The mixin supports the following parameters:
Name | Type | Default | Optional | Description
----------------------|-------------------------|------------------------|----------|------------
fields | array or string | | No | Array with the fields to versionmodelName
| string | ${ModelName}_history | No | Name to history modelrelationName
| string | history | No | Model has many history model relation namerelationParentName
| string | _record | No | History model belongs to model relation namerelationForeignKey
| string | _recordId | No | Foreign key for relationsversionFieldName
| string | _version | No | Field name to version codeversionFieldLen
| number | 5 | No | Length to versionFieldhashFieldName
| string or false | _hash | Yes | Field name to hash codehashFieldLen
| number | 10 | No | Length to hashFieldactionFieldName
| string or false | _action | Yes | Field name to action nameupdatedFieldName
| 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.hashFieldName
- If is false then hashFieldLen is. ignoored.
Generates create records
- Model.createModel.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.updateAllModel.deleteAll
- (AKA Model.destroyAll)
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`