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

mongoose-plugin-meta-nt

v1.6.0

Published

Simple meta plugin for Mongoose. It manages the soft deletion and modification history.

Downloads

30

Readme

Mongoose Plugin Meta

This plugin enables soft deletion of documents, history of updates and date of creation in MongoDB. It creates new functions that act like middlewares. At any time, it is possible to use the provided functions by Mongoose.

Installation

$ npm install mongoose-plugin-meta-nt --save

Documentation

Example with TagModel

  • Tag.js
const mongoose = require('mongoose');
const metaPlugin = require('mongoose-plugin-meta-nt');

const tagSchema = new mongoose.Schema(
{
  label: {type: String, required: true}
},
{
  collection: "tags"
});

// Add plugin to the schema
tagSchema.plugin(metaPlugin.metaManager, {entity: false});

const Tag = mongoose.model('Tag', tagSchema);

### Statics


createMeta(obj, user, entities_id = []);

findMeta(obj = {}, user = {}, arrPopulate = [], entity_filter = null);
findOneMeta(obj, user = {}, arrPopulate = [], entity_filter = null);
findByIdMeta(id, user = {}, arrPopulate = [], entity_filter = null);
countMeta(obj = {}, user = {}, entity_filter = null);


findOneAndUpdateMeta(obj, newObj, user, entity_filter = null);
findByIdAndUpdateMeta(id, obj, user, entity_filter = null);


findOneAndDeleteMeta(obj, user, isSoftDelete = true, entity_filter = null);
findOneAndRemoveMeta(obj, user, isSoftDelete = true, entity_filter = null);
findByIdAndRemoveMeta(id, user, isSoftDelete = true, entity_filter = null);
findByIdAndDeleteMeta(id, user, isSoftDelete = true, entity_filter = null);
deleteOneMeta(obj, user, isSoftDelete = true, entity_filter = null);
deleteMeta(obj, user, isSoftDelete = true, entity_filter = null);

// Get all documents
findWithDeleted(obj = {}, user = {}, arrPopulate = [], entity_filter = null);
findOneWithDeleted(obj, user = {}, arrPopulate = [], entity_filter = null);
countWithDeleted(obj = {}, user = {}, entity_filter = null);


// Search

// Filter the given fields with the query value.
simpleSearch(query, fields, user = {}, entity_filter = null); //  (NOT WORKING FOR NOW)

Methods

updateMeta(newObj, user);

Queries

pagination(offset, limit);
projection(obj = null, showMeta = false, showVersion = false)

Usage


const user =
{
  id: "5b01a8ab8337ed5bfb21f7c6"
};

const id = "5b01a8ab8337ed5bfb21f1a1";

// Find - Promise
let data  = await Tag.findMeta({label: "Test"});
let data = await Tag.findOneMeta({label: "Test"});
let data = await Tag.findByIdMeta(id);

// Find - Projection
let data = await Tag.findOneMeta({label: "Test"}).projection(); // Will unselect 'meta' and '__v' fields
let data = await Tag.findOneMeta({label: "Test"}).projection({label2: 0}); // Will unselect 'label2', 'meta' and '__v' fields
let data = await Tag.findOneMeta({label: "Test"}).projection({label2: 1}); // Will select 'label2' and disabled others (MongoDB behavior) 

// Count - Promise
let data = await Tag.countMeta({label: "Test"});

// Create - Promise
let data = Tag.createMeta({label : "Test"}, user, entity_id);

// Update - Promise
let data = await Tag.findOneAndUpdateMeta({_id: id}, {label: "Custom"}, user);
let data = await Tag.findByIdAndUpdateMeta(id, {label: "CustomFindByIdUpdate"}, user);
let data = await Tag.updateMeta({label: "CustomUpdate"}, user);

// Delete - Promise
let data = await Tag.findOneAndRemoveMeta({label: "Test2"}, user);
let data = await Tag.findOneAndRemoveMeta({label: "Test3"}, user);
let data = await Tag.findByIdAndRemoveMeta(id, user);
let data = await Tag.findByIdAndDeleteMeta(id, user);
let data = await Tag.deleteOneMeta({}, user);
let data = await Tag.deleteMeta({}, user);
// Set to true for soft deletion. It is possible to set to false if you want real deletion 


// Find deleted documents - Promise
let data = await Tag.findDeleted({label: "Test"});
let data = await Tag.findOneDeleted({label: "Test2"});
let data = await Tag.countDeleted({label: "Test"});

Produced document

{ 
  "_id": "5b1995a5a87a731a94aa0abe",
  "label": "Test",
  "meta": 
  {
    "entities_id": [],
    "created":
    {
      "date": "2018-06-07T20:29:25.397Z",
      "user": "5b01a8ab8337ed5bfb21f7c6"
    },
    "deleted":
    {
      "date": "2018-06-07T20:38:06.762Z",
      "user": "5b01a8ab8337ed5bfb21f7c6"
    },
    "updated":
    [
      {
    	  "date": "2018-06-07T20:30:50.370Z",
          "user": "5b01a8ab8337ed5bfb21f7c6",
          "_id": "5b199906252b8d1ef4def526"
      }
    ]
  }
}

The _id is created in every items of the array.

## Entity filtering It is possible to enable the entity_id filtering :

  • Set to true in the Model : tagSchema.plugin(metaPlugin.metaManager, {entity: true});
  • If for any reason, the entity property is set to false in the model definition, it is possible to force the entity filtering by adding the parameter true in all functions.
  • Set a value to the property entity_id in your user object.
  • Put the user object in parameter of all functions :

const entities_id = ["5b01a8ab8337ed5bfb21f7c6"];

const user =
{
  id: "5b01a8ab8337ed5bfb21f7c6",
  entities_id: ["5b01a8ab8337ed5bfb21f7c2", "5b01a8ab8337ed5bfb21f7c6"]
};

const id = "5b01a8ab8337ed5bfb21f1a1";

// Find - Promise
let data  = await Tag.findMeta({label: "Test"}, user);
// if need to force the entity filtering : Tag.findMeta({label: "Test"}, user, true);
let data = await Tag.findOneMeta({label: "Test"}, user);
let data = await Tag.findByIdMeta(id, user);

// Count - Promise
let data = await Tag.countMeta({label: "Test"});

// Create - Promise
let data = Tag.createMeta({label : "Test"}, user);

// Update - Promise
let data = await Tag.findOneAndUpdateMeta({_id: id}, {label: "Custom"}, user);
let data = await Tag.findByIdAndUpdateMeta(id, {label: "CustomFindByIdUpdate"}, user);
let data = await Tag.updateMeta({label: "CustomUpdate"}, user);

// Delete - Promise
let data = await Tag.findOneAndRemoveMeta({label: "Test2"}, user);
let data = await Tag.findOneAndRemoveMeta({label: "Test3"}, user);
let data = await Tag.findByIdAndRemoveMeta(id, user);
let data = await Tag.findByIdAndDeleteMeta(id, user);
let data = await Tag.deleteOneMeta({}, user);
let data = await Tag.deleteMeta({}, user);
// Set to true for soft deletion. It is possible to set to false if you want real deletion 


// Find deleted documents - Promise
let data = await Tag.findDeleted({label: "Test"}, user);
let data = await Tag.findOneDeleted({label: "Test2"}, user);
let data = await Tag.countDeleted({label: "Test"}, user);