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-tree-ancestors

v1.0.1

Published

Mongoose Tree Structure with an Array of Ancestors plugin for mongoose

Downloads

328

Readme

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');

// 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.

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:

  • Method setParent
  • Method isRoot
  • Method isLeaf
  • Method isDescendant
  • Method isSibling
  • Method getFullTree Probably https://github.com/substack/js-traverse is a good option to make all this happen