0.2.5 • Published 4 years ago

loopback-changes-history-mixin v0.2.5

Weekly downloads
37
License
MIT
Repository
github
Last release
4 years ago

loopback-changes-history-mixin

npm version Build Status Coverage Status

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_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:

NameTypeDefaultOptionalDescription
fieldsarray or string **NoArray with the fields to version
modelNamestring${ModelName}_historyNoName to history model
relationNamestringhistoryNoModel has many history model relation name
relationParentNamestring_recordNoHistory model belongs to model relation name
relationForeignKeystring_recordIdNoForeign key for relations
versionFieldNamestring_versionNoField name to version code
versionFieldLennumber5NoLength to versionField
hashFieldNamestring or false_hashYesField name to hash code
hashFieldLennumber10NoLength to hashField
actionFieldNamestring or false_actionYesField name to action name
updatedFieldNamestring or false_updateYesField 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:

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

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

5 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.7

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago