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

supergoose

v0.2.6

Published

Mongoose plugin for simple addons like findOrCreate

Downloads

31

Readme

supergoose Build Status

Mongoose simple plugin adding some handy functions.

Model functions

Relationship creator functions

Installation

npm install supergoose

Usage

Initialization

Use mongoose's plugin method to extend your models with supergoose

var supergoose = require('supergoose')
var ClickSchema = new Schema({ ... });
ClickSchema.plugin(supergoose, [options]);
var Click = mongoose.model('Click', ClickSchema)

Arguments

  • supergoose - The supergoose function
  • options - Options object

Valid Options

  • instance - The instance of mongoose used in the application (Required for Schema functions)
  • messages - Object of custom messages (Required for errors function)

Adds find or create functionality to mongoose models. This is handy for libraries like passport.js which require it

Click.findOrCreate({ip: '127.0.0.1'}, function(err, doc) {});
Click.findOrCreate({ip: '127.0.0.1'}, {browser: 'Chrome'}, function(err, click) {})

Arguments

  • query - Conditions with which to search for document
  • [doc] - Document to insert if document not found
  • [options]
  • callback

Valid Options

  • upsert - updates the object if it exists. Default: false

Parses the complex validation errors return from mongoose into a simple array of messages to be displayed as flash messages or something similar

var supergoose = require('supergoose')
var ClickSchema = new Schema({ip: {type: String, required: true}});
Click.plugin(supergoose, {messages: {'required': '%s is a required field'}});
var Click = mongoose.model('Click', ClickSchema);

The Click model now has an errors static method

Click.create({}, function(err, click) {
  if(err) {
    Click.errors(err, function(messages) {
      console.log(messages);
      // outputs ['ip is a required field']
    })
  }
});

Arguments

  • errors - error returned from mongoose command
  • callback

  • parentOf / oneToMany - Creates a one to many relationship
  • childOf / manyToOne - Creates a many to one relationship
  • hasA / oneToOne - Creates a one to one relationship
  • hasMany / manyToMany - Creates a many to many relationship

Arguments

  • modelName - Name of related Model
  • [myPath] - Name of path on this schema that refers to related Model. (If not provided, a default is used based on the model name. '_clicks' for the above example)

Returns

  • Relationship

When a relationship is created, it adds a path that refers to the related model on the schema that creates it. The relationship object has one property:

enforceWith

var supergoose = require('supergoose')
var mongoose = require('mongoose')

var ClickSchema = new Schema({ip: {type: String, required: true}, _user: {type: ObjectId}});
var UserSchema = new Schema({name: String})

UserSchema.plugin(supergoose, {instance: mongoose});
UserSchema.parentOf('Click', 'clickCollection').enforceWith('_user')

Arguments

  • itsPath - Name of path on related model that refers back to this schema.
  • [options]

Valid Options

  • delete - Default: false. Only affects one to X relationships. If set to true, when a doc is removed, it will delete related docs.

License

MIT License