npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loopback-ds-model-changes-mixin

v0.1.1

Published

Track create, update and delete actions on a loopback model

Downloads

31

Readme

Loopback Model Change Mixin

Inspired by Loopback Auditz. Offers less functionality but more customization.

Usage

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.

Installation

npm install loopback-ds-model-changes-mixin

mode-config.json:

{
  "_meta": {
    "mixins": [
      "../node_modules/loopback-ds-model-changes-mixin"
    ]
  },
}

Basic

Minimum configuration in mymodel.json (note the myModelChanges model must be configured and attached to your app manually)

{
  ...
  "mixins": {
    "ModelChanges": {
      "changeModel": "myModelChanges",
      "idKeyName": "myModelId"
    }
  }
}

This will log all CUD operations to the model myModelChanges using the foreign key myModelId e.g.

Default Behaviour

The default behavior is as follows:

  1. Every model key will be recorded for every action
  2. Updates will record the full new state of the model (not just the changes)
  3. The property for the action name is called 'action'

All of this can be customised (see below)

Tracking Date

The 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"`.

Whitelist and Blacklist Properties

If you only wish to log changes to specific properties, put the property names in the whitelist array option:

{
  "properties": {
    "name": "string",
    "description": "string"
  },
  "mixins": {
    "ModelChanges": {
      "changeModel": "myModelChanges",
      "idKeyName": "myModelId",
      "whitelist": ["name"]
    }
  }
}

The above will only log modifications to the "name" property on MyModel

You can also blacklist if you prefer

{
  "properties": {
    "name": "string",
    "secret": "string"
  },
  "mixins": {
    "ModelChanges": {
      "changeModel": "myModelChanges",
      "idKeyName": "myModelId",
      "blacklist": ["secret"]
    }
  }
}

Only Log Changes

The default behaviour logs the full model state for every update. If you would like to only store what changed, enable deltas:

{
  ...
  "mixins": {
    "ModelChanges": {
      "changeModel": "myModelChanges",
      "idKeyName": "myModelId",
      "deltas": true
    }
  }
}

In the above config, if you only change the name property in an update, no value for description would be stored in myModelChanges.

Changing the Action Property

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

{
  ...
  "mixins": {
    "ModelChanges": {
      "changeModel": "myModelChanges",
      "idKeyName": "myModelId",
      "actionKey": "model_action"
    }
  }
}

Tracking User ID

To track user id you will need to install Loopback Component Remote Context (requires Loopback 2.37+) and ensure that it is configured for any models you wish to track:

component-config.json

  "loopback-component-remote-ctx": {
    "enabled": true,
    "whiteList": ["myModel"],
    "argName": "remoteCtx",
    "argName": "remoteCtx" // optional
  }

my-model.json

  "mixins": {
    "ModelChanges": {
      // ...
      "trackUsersAs": "userId",
      "trackUsersFrom": "userId", //optional
      "remoteCtx": "remoteCtx" // optional
    }
  }

Where 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 argName you specified for loopback-component-remote-ctx in component-config.json (defaults to remoteCtx).

Tracking Remote Method

To track remote method names you will need to install Loopback Component Remote Context (requires Loopback 2.37+) and ensure that it is configured for any models you wish to track (See Tracking User Id for setup).

If you wish to track the remote method name used for actions, specify the remoteMethod property in your model config:

  "mixins": {
    "ModelChanges": {
      // ...
      "remoteMethod": "remoteMethod"
    }
  }

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

  "mixins": {
    "ModelChanges": {
      // ...
      "remoteMethod": "remoteMethod",
      "remoteOnly": true"
    }
  }

Custom Action Names

By default, the actions are stored as 'create', 'update' and 'delete. These can be customised with the mixin properties createActionName, updateActionName and deleteActionName

Dev

Tests

npm install && npm test