mongoose-strip-paths
v1.0.1
Published
A mongoose plugin that deletes provided paths on a document and its sub documents, if any.
Downloads
12
Maintainers
Readme
mongoose-strip-paths
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
- Install dependencies with
npm install
and install mongo if you don't have it yet. - Start mongo with
mongod
. - 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.