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-facets

v0.0.3

Published

Facet injection for mongoose models

Downloads

12

Readme

Build Status

Mongoose Facets

Facet injection for your Mongoose models. Read more about facets in MongoDB.

Installation

npm install mongoose-facets

Usage

The convention for naming facet models is FooFacet. They should be defined in files with the corresponding name and export the model returned by the mongoose.model() method. For example, in FooFacet.js:

module.exports = mongoose.model('FooFacet', FooFacetSchema);

Place all the facets in the same directory as the parent model and plug in mongoose facets:

ParentSchema.plugin(require('mongoose-facets'), { dirname: __dirname });

You can place them in a different directory if you choose; just specify the correct directory in the dirname option.

Each facet schema will be added to the parent schema under the key facets.{name} where the name is taken from the name of the model minus the "Facet" suffix, i.e. FooFacet will added be under the key facets.foo.

A boolean flag of the form facetFlags.{name} will also be added to the parent schema for each facet. This value will be automagically set properly by the plugin depending on whether a given facet object exists under the appropriate key. You can use these flags to query by facet type:

Parent p1 = yield Parent.create({name: 'joe smith', facets: {foo:{}}});
Parent p2 = yield Parent.create({name: 'joe jones'});
// find parents with a foo facet and whose name starts with 'joe'
const results = yield Parent.find({'facetFlags.foo': true, name: /^joe/}).exec();
// only p1 is returned
assert.deepEqual(results.map(p=>p.id), [p1.id] "Only p1 should be found");

The directory containing the facets can also include a file called facet_indexes.js which exports indexes that should be applied to the parent collection. This should be an array of arrays where the sub-arrays are arguments to the Schema.index() method:

module.exports = [
  [{fieldA: 1}, {unique: true}],
  [{fieldB: 1, fieldC: -1}],
];

You can export a function instead if you want to make this more dynamic. The function will be passed all of the facet keys that have been discovered and added to the parent schema (these will be relative keys minus the facets. prefix, not the full absolute key):

// create an index by name for each facet, 
// i.e. to find "users with an admin facet and name that starts with ..."
module.exports = (facetKeys) => {
  return facetKeys.map(key => [{[`facetFlags.${key}`]: 1, name: 1}, {sparse: true}]);
};

The facet keys will also be available through the static FACET_KEYS property of the parent model.

A parent property will be automagically added to each facet:

Parent p = new Parent();
p.facets.foo = new FooFacet();
assert.equal(p.facets.foo.parent, p, "Parent not set properly");

License

MIT