Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-strip-paths

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-strip-paths

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

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 05 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc