loopback-changes-history-mixin v0.2.5
loopback-changes-history-mixin
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:
{
"_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_historywith properties indicated infields:name,email,statusanddescription. - Relation
Personhas manyhistory(modelPerson_history, foreign key_recordId). - Relation
Person_historybelongsrecord(modelPerson, foreign key_recordId). - Properties
_versionand_hashasstringin modelsPersonandPerson_history. - Properties
_actionasstringand_updateasdatein modelPerson_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:
hashFieldNameallow create a history change only if any property in fields was alter. If setuphashFieldName: falsethen a history change will be creates with update method called.- If
hashFieldNameisfalsethenhashFieldLenis. ignoored.
Loopback methods
Generates create records
Model.createModel.updateOrCreate(AKAModel.upsert)Model.findOrCreateModel.replaceOrCreateModel.upsertWithWhere(viewModel.upsertWithWheresection below)
Generates update records
Model.updateOrCreate(AKAModel.upsert)Model.replaceOrCreateModel.upsertWithWhere(viewModel.upsertWithWheresection below)Model.replaceByIdModel.prototype.saveModel.prototype.updateAttributeModel.prototype.updateAttributesModel.prototype.replaceAtributes
Generates destroy records
Model.prototype.delete(AKAModel.prototype.destroy)Model.deleteById(AKAModel.destroyById)
Unsupported methods
Model.updateAllModel.deleteAll(AKAModel.destroyAll)
Model.upsertWithWhere
To create history change with method upsertWithWhere option instanceByWhere: true must be passed:
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