Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
mongoose-tree-ancestors
Advanced tools
Mongoose Tree Structure with an Array of Ancestors plugin for mongoose
Do you ever wanted to integrate a Tree Structure with documents refering to a parent, and the parent of the parent, avoiding a recursive function? Probably not, but if you ever think about that, you will be loosing a lot of time.
Instead of that, you can apply a data structure called ** Mongoose Tree Structure with an Array of Ancestors ** . What's that?
Take a look in the official Mongoose Documentation clicking here to know more about that.
It keeps track of all the changes for you. It's hooked to all the mongoose operations (update, findOneAndUpdate, save, remove and findOneAndRemove) you do through your model.
Everytime you do an operation over a document, this module will keep updated the array of ancestors for you.
Now, let's see how to use it...
npm install mongoose-tree-ancestors --save
Just include the call to this module in the same place you declare your Mongoose Schema:
'use strict';
const mongoose = require('mongoose');
const mongooseTreeAncestors = require('mongoose-tree-ancestors');
// Declare a Model name
const modelName = 'product_category';
// Prepare the Schema
const schema = new mongoose.Schema({
// Not required, but it's useful to keep awareness of this field
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: 'group'
},
// Not required, but it's useful to keep awareness of this field
ancestors: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'group'
}],
categoryName: String,
createdAt: {
type: Date,
default: Date.now
},
updatedAt: Date
}, {
collection: 'products_categories',
timestamps: true
});
// Add the mongooseTreeAncestors Plugin to the schema
mongooseTreeAncestors(schema, {
// Set the parent field name and model reference
parentFieldName: 'parent',
parentFieldRefModel: modelName,
// Set the ancestors field name and model reference
ancestorsFieldName: 'ancestors',
ancestorsFieldRefModel: modelName
});
module.exports = mongoose.model(modelName, schema);
After that, you will have implemented an automated model to handle the data in the ancestor
's field.
If you already have a parent
field, you can call the status method buildAncestors
to build (or rebuild) all your ancestor's structure
yourModel.buildAncestors();
FAQs
Mongoose Tree Structure with an Array of Ancestors plugin for mongoose
The npm package mongoose-tree-ancestors receives a total of 13 weekly downloads. As such, mongoose-tree-ancestors popularity was classified as not popular.
We found that mongoose-tree-ancestors demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.