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-auditz

v0.5.0

Published

Adds comprehensive audit trail functionality to Loopback by keeping track of who created/modified/deleted data and when they did it, and adds a revisions model compatible with Sofa/Revisionable for PHP (https://github.com/jarektkaczyk/revisionable)

Downloads

31

Readme

Description

This module is designed for the Strongloop Loopback framework. It provides extensive support for Audit Trails in your LoopBack based application.

It consists of a group of functionalities:

Each of these main functionalities can be turned off individually.

Install

  npm install --save loopback-component-remote-ctx loopback-auditz

Server Config

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-auditz",
      "../common/mixins"
    ]
  }
}

Make sure you enable authentication by putting the following in a boot script (ie server/boot/authentication.js):

'use strict';
module.exports = function enableAuthentication(server) {
  // enable authentication
  server.enableAuth();
};

Enable the loopback-component-remote-ctx by adding the following in your server/component-config.json:

  "loopback-component-remote-ctx": {
    "enabled": true,
    "argName": "remoteCtx",
    "blackList": ["User"]
  }

And finally use the loopback token middleware by adding the following line to your server/server.js:

app.use(loopback.token());

Configure

To use with your Models add the mixins attribute to the definition object of your model config.

  {
    "name": "Widget",
    "properties": {
      "name": {
        "type": "string",
      },
    },
    "mixins": {
      "Auditz" : true,
    },
  },

There are a number of configurable options to the mixin:

  "mixins": {
    "Auditz": {
      "createdAt": "created_at",
      "updatedAt": "updated_at",
      "deletedAt": "deleted_at",
      "createdBy": "created_by",
      "updatedBy": "updated_by",
      "deletedBy": "deleted_by",
      "softDelete": true,
      "unknownUser": 0,
      "remoteCtx": "remoteCtx",
      "scrub": true,
      "required": false,
      "validateUpsert": true,
      "silenceWarnings": false,
      "revisions": {
        "name": "other_revisions_table",
        "idType": "Number",
        "dataSource": "db",
        "autoUpdate": false
      }
     },
  },

createdAt

This allows you to define an alternative name for the createdAt field. When set to false, this property will not be defined nor used.

updatedAt

This allows you to define an alternative name for the updatedAt field. When set to false, this property will not be defined nor used.

deletedAt

This allows you to define an alternative name for the deletedAt field. If you don't want the soft delete functionality, specify 'softDelete': false in the options.

createdBy

This allows you to define an alternative name for the createdBy field. When set to false, this property will not be defined nor used.

updatedBy

This allows you to define an alternative name for the updatedBy field. When set to false, this property will not be defined nor used.

deletedBy

This allows you to define an alternative name for the deletedBy field.If you don't want the soft delete functionality, specify 'softDelete': false in the options.

softDelete

By default, soft delete functionality is turned on and uses the deletedAt and deletedBy configuration options. If you set softDelete to false, it completely ignores deletedAt, deletedBy and scrub, and all deletes will become hard deletes.

unknownUser

This allows you to define which userId should be filled out when no current user can be determined

remoteCtx

The value you provided in component-config.json for argName of loopback-component-remote-ctx

scrub

If true, this sets all but the "id" fields to null. If an array, it will only scrub properties with those names. However, if you specified softDelete: false this option will be ignored.

required

This defines the requiredness of the createdAt and updatedAt fields. The deletedAt field is never required

validateUpsert

This defines whether or not the validateUpsert property is set for the model

silenceWarnings

This defines if the warnings should be suppressed or not

revisions

If set to false, this disables keeping track of changes in a revisions model. If it's true or an object, keeping track of changes in a revisions model is enabled, and the following configuration options are availabe if it's an object:

name

The name for the revisions model in which to keep changes to the model.

idType

The data type to assume for id fields. The default is 'Number', which is fine for most databases, but (for example) MongoDB uses strings, so in that case provide "String" as the value for idType.

dataSource

The dataSource to connect the revisions model to. This dataSource needs to be defined in datasources.json first.

autoUpdate

If set to false, it will assume the model exists in the dataSource already, and we don't need to create or alter the table. If set to true, it will run autoupdate on the dataSource for the given revisions model name to make sure the table exists with the right columns.

Usage with MongoDB

In case you use MongoDB with this module, and also use the 'revisions' table option, you need to configure the idType as a 'String'. Otherwise this module will attempt to store the non-numeric id in the row_id property of the revisions model, which is a Number by default.

Operation Options

Skipping updatedAt updating

By passing in additional options to an update or save operation you can control when this mixin updates the updatedAt field.
The passing true to the option skipUpdatedAt will skip updating the updatedAt field.

In this example we assume a book object with the id of 2 already exists. Normally running this operation would change the updatedAt field to a new value.

Book.updateOrCreate({name: 'New name', id: 2}, {skipUpdatedAt: true}, function(err, book) {
  // book.updatedAt will not have changed
});

Retrieving soft deleted entities

Unless you specified softDelete to be turned off, you can run queries that include deleted items in the response, by adding { deleted: true } to the query object (at the same level as where, include etc).

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Unless stated otherwise all works are:

and licensed under: