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-strip-paths

v1.0.1

Published

A mongoose plugin that deletes provided paths on a document and its sub documents, if any.

Downloads

6

Readme

mongoose-strip-paths

Build Status Coverage Status

A mongoose plugin that deletes provided paths on a document and its sub documents, if any.

How it works

When instantiated, this plugin adds a static method stripPaths() to your schema. Upon invoking this method, it loops over the provided paths in the options.paths variable and sets them to undefined. If there are any nested sub documents (via a single nested schema or embedded within an array), the stripPaths() method of all respective sub documents is also invoked. This works for nested subdocuments as well as documents added via a populate query.

Note that there will be no validation done upon invoking the stripPaths() method, which means that the document could be in an invalid state. As of version 0.0.2 stripPaths() also returns a reference to the document for easy chaining.

This functionality should ideally only be used to return the document for further processing.

Usage

For example, given schemas as follows:

let mongoose = require("mongoose");
let mongooseStripPaths = require("mongoose-strip-paths").mongooseStripPaths;
let Schema = mongoose.Schema;

let PostSchema = new Schema({
  title: String,
  message: String,
  fieldToStrip: String
});

PostSchema.plugin(mongooseStripPaths, { paths: ["fieldToStrip"] });

let UserSchema = new Schema({
  username: String,
  createdAt: { type: Date, required: true },
  posts: [Post]
});

// note that if you add 'posts', then the posts field will be removed,
// also note that after stripping 'createdAt' mongoose validation will fail on trying to save it
UserSchema.plugin(mongooseStripPaths, { paths: ["createdAt"] });

let User = mongoose.model("User", UserSchema);

Calling the strip fields method on a user will result in the following document:

let user = new User({
  username: 'u1',
  createdAt: new Date(),
  posts: []
});

user.posts.push({
  title: 'first post',
  message: 'hello',
  fieldToStrip: 'some internal data'
});

user.posts.push({
  title: 'second post',
  message: 'world',
  fieldToStrip: 'some more internal data'
});

user.stripPaths();

// user document is now
{
  _id: ...,
  username: 'u1',
  posts: [
    {
      _id: ...,
      title: 'first post',
      message: 'hello'
    },
    {
      _id: ...,
      title: 'second post',
      message: 'world'
    }
  ]
}

// another example

let userObj = user.stripPaths().toObject();

Required options

  • path: an array list of the required paths to remove. See object-path api for more path notations.

Requirements

  • Node >=6
  • MongoDB >=2.6.10
  • Mongoose >=4.11.0

Installation

npm install mongoose-strip-paths

Testing

  1. Install dependencies with npm install and install mongo if you don't have it yet.
  2. Start mongo with mongod.
  3. Run tests with npm test. Additionally you can pass your own mongodb uri as an environment variable if you would like to test against your own database, for e.g. URI='mongodb://username:password@localhost/mongoose-strip-paths-test' npm test

Publishing

release-it

release-it patch,minor,major

Manual

  • npm version patch,minor,major
  • npm publish

Changelog

1.0.1

  • Update development dependencies

1.0.0

  • Fix bug where the _id and __v properties could be stripped on the root object
  • Add mongoose >= 5.x support
  • Update development dependencies
  • Drop Node.js 4.x support

Misc

Issues, comments, PRs all welcome.