mongoose-tree-ancestors - Introduction
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.
- Ok, nice introduction, but... What does your module do?
- Well, let's say you are implementing the Mongoose Structure described above. Now, you will need to keep updated an array with all the ancestors, in each of the documents.
- Wait, but if I do that, I'll have to update everything everytime I do a change in the tree structure :(
- Yes, you will have to keep track by yourself of all the modifications in your collection, knowing everytime a parent changes, it's deleted, or it's edited in the tree.
Ok, just tell me what does mongoose-tree-ancestors do already!
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...
Installation
npm install mongoose-tree-ancestors --save
Usage
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');
const modelName = 'product_category';
const schema = new mongoose.Schema({
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: 'group'
},
ancestors: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'group'
}],
categoryName: String,
createdAt: {
type: Date,
default: Date.now
},
updatedAt: Date
}, {
collection: 'products_categories',
timestamps: true
});
mongooseTreeAncestors(schema, {
parentFieldName: 'parent',
parentFieldRefModel: modelName,
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.
NOTE
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();
TODO: