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

v0.5.16

Published

Model relationships plugin for Mongoose

Downloads

8

Readme

Mongoose Relationships

A plugin for Mongoose for model relationships and helpers.

Goals

  • Be unobtrusive and compliant with the ways of Mongoose (coding style, testing, API).

Usage

var mongoose = require('mongoose');
require('mongo-relation');

One to Many (belongsTo/hasMany)

var postSchema = new mongoose.Schema({});
postSchema.hasMany('comments', { dependent: 'destroy|delete|nullify' });
var Post = mongoose.model('Post', postSchema);

var commentSchema = new mongoose.Schema({});
commentSchema.belongsTo('post');
var Comment = mongoose.model('Comment', commentSchema);

create

Creates a child document associated to the parent.

  • {Object|Array} objs child document(s) // simple objects
  • {Function} callback callback(err, docs)
    • {Error} err error from mongoose
    • {Document|Array} docs Array or single persisted model
var postSchema = new mongoose.Schema({ content: String });
postSchema.hasMany('comments', { dependent: 'destroy|delete|nullify' });

var commentSchema = new mongoose.Schema({ content: String });
commentSchema.belongsTo('post');

var post = new Post({ content: "Relations are helpful." });

post.comments.create({ content: "I agree!" }, function(err, comment){
  console.assert(comment.content == "I agree!", "comment belongsTo post.");
  console.assert(post._id == comment.post, "comment belongsTo post.");
  console.assert(!comment.isNew, "comment has been saved.");
  console.assert(comment instanceOf Comment, "comment is a Comment.");
});

post.comments.create({ content: "+1" }, { content: "Yup!" }, function(err, comments){
  console.assert(comments.length == 2, "created two documents.");
  comments.forEach(function (comment) {
    console.assert(post._id == comment.post, "comment belongsTo post.");
    console.assert(!comment.isNew, "comment has been saved.");
    console.assert(comment instanceOf Comment, "comment is a Comment.");
  });
});

build

Instantiates children/a child document(s) with the appropriate associations.

  • {Object|Array} objs child document(s) // simple objects
var comment = post.comments.build({comment: "First!"});
console.assert(comment.post == post._id, "associated");
console.assert(comment.isNew, "not saved");

var post = new Post({ content: "Relations are easy." });
var comments = post.comments.build({ comment: "First!" }, { comment: "LOL" });
console.assert(comments.length == 2);
comments.forEach(function (comment) {
  console.assert(comment.post === post._id, "associated");
  console.assert(comment.isNew, "not saved");
});

concat

Associates & saves initialized document(s) with the appropriate associations.

  • {Document|Array} docs documents to associate. // mongoose documents
  • {Function} callback callback(err, docs)
    • {Error} err error from mongoose
    • {Document|Array} docs Array or single persisted document(s)

var post = new Post({ content: "Concating is great." });
var comment = new Comment();
post.comments.concat(comment, function(err, comment){
  console.assert(comment.post === post._id, "associated");
  console.assert(!comment.isNew, "saved");
});

var comments = [new Comment(), new Comment()];
post.comments.concat(comments, function(err, comments){
  console.assert(comments.length == 2);
  comments.forEach(function (comment) {
    console.assert(comment.post === post._id, "associated");
    console.assert(!comment.isNew, "saved");
  });
});

// existing doc

var comment = new Comment();
comment.save(function (err, comment) {
  post.comments.concat(comment, function(err, comment){
	console.assert(comment.post === post._id, "associated");
	console.assert(!comment.isNew, "saved");
  });
});

append

Sugar for #concat

find

Inject params for the association, then fallback on Model.find

  • {Object} docs (optional) query conditions
  • {String} fields (optional) fields to populate
  • {Object} options (optional) query options
  • {String} callback (optional) callback(err, docs)
    • {Error} err error from mongoose
    • {Document} doc Found document
var postOne = new Post({ content: "Find all the things." })
  , postTwo = new Post({ content: "Found all the things." });

postOne.comments.create({ content: "+1" }, function(err){
  postTwo.comments.create({ content: "+1" }, function(err){

    postOne.comments.find({ comment: "+1" }, function(err, comments){
      console.assert(comments.length == 1);
      console.assert(comments[0].post == postOne._id, "found the correct comment");

      var criteria = postTwo.comments.find({ comment: "+1" });
      console.assert(criteria._conditions.post == postTwo._id, "The association has been added to the query.");
      console.assert(criteria._conditions.comment == "+1", "keeps original query.");

      criteria.exec(function(err, comments){
        console.assert(comments.length == 1);
        console.assert(comments[0].post == postTwo._id, "found the correct comment");
      });
    });
  });
});

Todo

  • Document belongsTo
  • add touch to hasMany
  • add touch to belongsTo
  • Refactor hasAndBelongsToMany
  • Document hasAndBelongsToMany
  • Add embedsMany
  • Add emdedsOne