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

mongoosefiller

v0.1.1

Published

mongoose plugin, populate and keep updated references to documents in other collection.

Downloads

44

Readme

mongoosefiller

denormalization plugin for mongoose

This plugins helps you create denormalized schemas by copying references from other collection and keeping them up to date.

Just like model.populate except that data is stored in the collection instead of being populated for each query

Installation

$ npm install mongoosefiller

Plugin options

// plugin options
options = {
  id:     String // (optional default to path._id) path of the id of our reference model
  path:   String // (optional default to '') path to property to keep in sync with ref model
  pos:    String // (optional) pos operator prefix used to update model in collection array
  ref :   String // reference Model name (collection we are copying data from)
  dest:   String // destination Model name (collection we are copying data to)
  select: String // (optional default to all) list of space separated field to include or exclude
  sync:   String // (optional default to select) list of space separated field to keep in sync after first save.
}
// examples

var UserSchema = new Schema({
  firstname: {type: String},
  lastname : {type: String},
  email    : {type: String}
});

var PostSchema = new Schema({
  message: {type: String}
});

// add user.firstname, user.lastname, user.email path to schema
// fill user.* with data from User
// update documents every time a change occur in User
PostSchema.plugin(filler, {
  path: 'user',
  ref : 'User',
  dest: 'Post'
});

friendSchema = new Schema({
  date: {type: Date}
});

ListSchema = new Schema({
  name: {type: String},
  friends: [friendSchema]
});

// add firstname, lastname path to schema
// fill friends with data from User
// update friends in List (using positional operator List.friends.$._id)
// every time a change occur in User
friendSchema.plugin(filler, {
  ref   : 'User',
  dest  : 'List',
  pos   : 'friends.$.',
  select: 'firstname lastname'
});

Examples

  • Embedded array check examples/friends.js
  • Embedded doc check examples/post.js

Custom Schema event

a fill event is triggered on the denormalized schema when the the ref doc change and collection is updated

user.set('name', 'new-name').save();
PostSchema.on('fill', function(err, user) {
  // all post docs have have been saved
});

Custom id virtual path

if you define a path attribute, like in the following example, the plugin will create a virtual id accessor

PostSchema.plugin(filler, {
  path: 'user',
  ref : 'User',
  dest: 'Post'
});

// ...

Post.create(doc, function(err, post) {
  console.log(post.user.id == post.user._id.toString());
});

perf

I have done a basic test on one doc

  • read perfs with 10 embedded friends: 5ms
  • read perfs with 10 populated friends: 16ms
  • read perfs with 100 embedded friends: 36ms
  • read perfs with 100 populated friends: 67ms
  • read perfs with 1000 embedded friends: 94ms
  • read perfs with 1000 populated friends: 254m

License

MIT