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-tag-everything

v2.0.4-a

Published

A Mongoose plugin that adds a tags path to any schema

Downloads

69

Readme

Tag Everything

Mongoose plugin that adds a tags path to your models.

Installation

npm install mongoose-tag-everything

Breaking Changes in 2.0.0

As of 2.0.0, tags are stored as strings in your models, and the default Tag model stores model names tags appear in under the appearsIn path. So instead of querying for tag.name you can just query for tag. The default model name and path has also been changes to Tag and tags respectively.

Using in your app

var mongoose = require('mongoose');
var tagEverything = require('mongoose-tag-everything');

var animal = new mongoose.Schema({
    name: String
});
schema.plugin(tagEverything);

mongoose.model('Animal', animal);

The Animal model will now have a tags path that can hold an array of tags.

Tags are their own objects, saved in their own collection in your database (tags by default). They even have a Mongoose model registered to them (Tag by default). All of your models that use this plugin will draw from the same pool of tags. Tags are auto-populated on query-time and you can query tags just like any subdoc:

Animal.find({ "tags": ['bright', 'big', 'bulbous']});

Tags are indexed automatically at their name path and should be super fast.

Troubleshooting

Force Mongoose Instance

If you run into problems with models freezing on pre-validation, the tags model may be using a different Mongoose instance than the one your model is defined in. This can happen if you are using Mongoose behind a closure and not in the global namespace. To get around this, supply your mongoose instance in the options. In this use case it is recommended to use the plugin as a global plugin that applies to all schemas.

Example:

var mongoose = require('mongoose');
var tagEverything = require('mongoose-tag-everything');

mongoose.plugin(tagEverything, {
    mongoose: mongoose
});

var animal = new mongoose.Schema({
    name: String
});

mongoose.model('Animal', animal);

mongoose.model('Animal').create({ name: 'Zebra', tags: ['striped'] });

Custom path, collection, and model names

Pass the path, collection, or ModelName options to customize the respective names. It is recommended to load the plugin globally if any custom values are being used so there is consistency across models.

Running tests

npm test