{"id":22413536,"url":"https://github.com/nextfaze/loopback-mixin-model-changes","last_synced_at":"2025-07-31T23:31:29.360Z","repository":{"id":57290713,"uuid":"86773724","full_name":"NextFaze/loopback-mixin-model-changes","owner":"NextFaze","description":"Track create, update and delete actions on a loopback model","archived":false,"fork":false,"pushed_at":"2019-01-30T15:32:37.000Z","size":29,"stargazers_count":8,"open_issues_count":4,"forks_count":7,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-11-14T09:31:39.316Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NextFaze.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-31T03:24:02.000Z","updated_at":"2019-12-24T17:05:44.000Z","dependencies_parsed_at":"2022-08-28T07:00:17.067Z","dependency_job_id":null,"html_url":"https://github.com/NextFaze/loopback-mixin-model-changes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NextFaze%2Floopback-mixin-model-changes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NextFaze%2Floopback-mixin-model-changes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NextFaze%2Floopback-mixin-model-changes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NextFaze%2Floopback-mixin-model-changes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NextFaze","download_url":"https://codeload.github.com/NextFaze/loopback-mixin-model-changes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228312126,"owners_count":17900219,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-05T14:13:22.670Z","updated_at":"2024-12-05T14:13:24.164Z","avatar_url":"https://github.com/NextFaze.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Loopback Model Change Mixin\n\nInspired by [Loopback Auditz](https://github.com/jouke/loopback-auditz). Offers less functionality but more customization.\n\n## Usage\n\n*Note*: This mixin attempts to make not assumption about how you have setup your models and will not setup any models or properties for you. You must setup your tracking models yourself, either by extending the model they track or creating a new model with the properties you with to track on them.\n\n### Installation\n\n`npm install loopback-ds-model-changes-mixin`\n\nmode-config.json:\n\n```json\n{\n  \"_meta\": {\n    \"mixins\": [\n      \"../node_modules/loopback-ds-model-changes-mixin\"\n    ]\n  },\n}\n```\n\n### Basic\n\nMinimum configuration in mymodel.json (note the `myModelChanges` model must be configured and attached to your app manually)\n\n```json\n{\n  ...\n  \"mixins\": {\n    \"ModelChanges\": {\n      \"changeModel\": \"myModelChanges\",\n      \"idKeyName\": \"myModelId\"\n    }\n  }\n}\n```\n\nThis will log all CUD operations to the model `myModelChanges` using the foreign key `myModelId` e.g.\n\n### Default Behaviour\n\nThe default behavior is as follows:\n\n1. Every model key will be recorded for every action\n2. Updates will record the full new state of the model (not just the changes)\n3. The property for the action name is called `'action'`\n\nAll of this can be customised (see below)\n\n### Tracking Date\n\nThe mixin does not track the timestamp of actions by default. This can be done easily by applying the `[https://www.npmjs.com/package/loopback-ds-timestamp-mixin](Loopback Timestamp Mixin) to your change tracking model or by adding a date property to your change model with `defaultFn: \"now\"`.\n\n### Whitelist and Blacklist Properties\n\nIf you only wish to log changes to specific properties, put the property names in the `whitelist` array option:\n\n```json\n{\n  \"properties\": {\n    \"name\": \"string\",\n    \"description\": \"string\"\n  },\n  \"mixins\": {\n    \"ModelChanges\": {\n      \"changeModel\": \"myModelChanges\",\n      \"idKeyName\": \"myModelId\",\n      \"whitelist\": [\"name\"]\n    }\n  }\n}\n```\n\nThe above will only log modifications to the `\"name\"` property on `MyModel`\n\nYou can also blacklist if you prefer\n\n```json\n{\n  \"properties\": {\n    \"name\": \"string\",\n    \"secret\": \"string\"\n  },\n  \"mixins\": {\n    \"ModelChanges\": {\n      \"changeModel\": \"myModelChanges\",\n      \"idKeyName\": \"myModelId\",\n      \"blacklist\": [\"secret\"]\n    }\n  }\n}\n```\n\n### Only Log Changes\n\nThe default behaviour logs the full model state for every update. If you would like to only store what changed, enable `deltas`:\n\n```json\n{\n  ...\n  \"mixins\": {\n    \"ModelChanges\": {\n      \"changeModel\": \"myModelChanges\",\n      \"idKeyName\": \"myModelId\",\n      \"deltas\": true\n    }\n  }\n}\n```\n\nIn the above config, if you only change the `name` property in an `update`, no value for `description` would be stored in `myModelChanges`.\n\n### Changing the Action Property\n\nBy default, the column used for the name of the logged action (`'create'`, `'update'` or `'delete'`) is `'action'`. Change this by passing `actionKey` to the config:\n\n```json\n{\n  ...\n  \"mixins\": {\n    \"ModelChanges\": {\n      \"changeModel\": \"myModelChanges\",\n      \"idKeyName\": \"myModelId\",\n      \"actionKey\": \"model_action\"\n    }\n  }\n}\n```\n\n\n### Tracking User ID\n\nTo track user id you will need to install [Loopback Component Remote Context](https://github.com/snowyu/loopback-component-remote-ctx.js) (requires Loopback 2.37+) and ensure that it is configured for any models you wish to track:\n\ncomponent-config.json\n\n```json\n  \"loopback-component-remote-ctx\": {\n    \"enabled\": true,\n    \"whiteList\": [\"myModel\"],\n    \"argName\": \"remoteCtx\",\n    \"argName\": \"remoteCtx\" // optional\n  }\n```\n\nmy-model.json\n\n```json\n  \"mixins\": {\n    \"ModelChanges\": {\n      // ...\n      \"trackUsersAs\": \"userId\",\n      \"trackUsersFrom\": \"userId\", //optional\n      \"remoteCtx\": \"remoteCtx\" // optional\n    }\n  }\n```\n\nWhere `trackUsersAs` signifies the property that you wish to use to store the `userId` on the tracking model, `trackUsersFrom` is the property on `accessToken` that stores the user id (default is `userId`) and `remoteCtx` is the custom\n`argName` you specified for `loopback-component-remote-ctx` in `component-config.json` (defaults to `remoteCtx`).\n\n### Tracking Remote Method\n\nTo track remote method names you will need to install [Loopback Component Remote Context](https://github.com/snowyu/loopback-component-remote-ctx.js) (requires Loopback 2.37+) and ensure that it is configured for any models you wish to track (See Tracking User Id for setup).\n\nIf you wish to track the remote method name used for actions, specify the `remoteMethod` property in your model config:\n\n```json\n  \"mixins\": {\n    \"ModelChanges\": {\n      // ...\n      \"remoteMethod\": \"remoteMethod\"\n    }\n  }\n```\n\nThis will store the remote method name in the `remoteMethod` property of your tracking model. You can also specify if you _only_ watch to track actions that come from remote methods:\n\n```json\n  \"mixins\": {\n    \"ModelChanges\": {\n      // ...\n      \"remoteMethod\": \"remoteMethod\",\n      \"remoteOnly\": true\"\n    }\n  }\n```\n\n### Custom Action Names\n\nBy default, the actions are stored as 'create', 'update' and 'delete. These can be customised with the mixin properties `createActionName`, `updateActionName` and `deleteActionName`\n\n# Dev\n\n### Tests\n\n`npm install \u0026\u0026 npm test`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnextfaze%2Floopback-mixin-model-changes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnextfaze%2Floopback-mixin-model-changes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnextfaze%2Floopback-mixin-model-changes/lists"}