@cme-pro/mongoose-audit
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

npm node downloads

mongoose-audit

Mongoose plugin to store audit information like userAgent, ip, country, etc...

This plugin simply adds the following fields:

  • createdBy
  • updatedBy
  • deletedBy
  • createdFrom
  • updatedFrom
  • deletedFrom

Where:

  1. createdBy, updatedBy and deletedBy are a user id,
  2. createdFrom, updatedFrom and deletedFrom have the following structure:
    • userAgent
    • ip
    • country
    • continent
    • region
    • city
    • zip
    • lat
    • lng
    • tz

Note: deletedBy and deletedFrom are only useful if combined with mongoose-delete plugin.

Installation

$ npm install mongoose-audit --save

Overview

Adding plugin to the schema

BlogPost.plugin(mongooseAudit, { userModel: 'Author' });

Plugin options

  • userModel - optional (default is "User"). The mongoose Model managing users. Used to populate the field).

Usage

On create:

    const post = new BlogPost({});
    post.createdBy = currentUserId;
    post.createdFrom = {{ ip, userAgent, country };
    await post.save();

On update:

    post.set();
    post.updatedBy = currentUserId;
    post.updatedFrom = {{ ip, userAgent, country };
    await post.save();

On delete (only useful if combined with mongoose-delete plugin)

    post.deletedBy = currentUserId;
    post.deletedFrom = {{ ip, userAgent, country };
    await post.save();
    await post.delete();

Readme

Keywords

Package Sidebar

Install

npm i @cme-pro/mongoose-audit

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

14.5 kB

Total Files

17

Last publish

Collaborators

  • svengau
  • martinratinaud