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

v0.0.3

Published

Augments Mongoose.js models with super() to call parent model methods. Augmentation is done in an inherit() function that returns a child model based on a parent model and a child schema.

Downloads

14

Readme

mongoose-super

Augments Mongoose.js models with super() to call parent model methods. Augmentation is done in an inherit() function that returns a child model based on a parent model and a child schema.

Installation

npm install mongoose-super

Usage

Load the mongoose-super module and create the inherit function.

var MongooseSuper   = require('mongoose-super');
var inherit         = MongooseSuper.createInherit();

It also possible to pass a configuration object, for now you can only specify an external logging object as configuration option:

var inherit         = MongooseSuper.createInherit({
    logger : myLogger
});

Before creating schemas and models you need to load the Mongoose.js module:

var mongoose = require('mongoose');

The definition of the parent schema and model

var AnimalSchema = new mongoose.Schema({ numberOfLegs : Number });
AnimalSchema.methods.whoami = function() {
    console.log("I have " + this.numberOfLegs + " legs.");
};

var Animal = mongoose.model('Animal', AnimalSchema);

Now we are going to inherit the Human model from the Animal model and extend it using the Human schema extension. The whoami() method calls the parent version of the method using the super() method and defines additional functionality.

//Human inherits from Animal
var HumanSchemaExtension = new mongoose.Schema({ name : String,  numberOfLegs : { type: Number, default: 2 }});

HumanSchemaExtension.methods.whoami = function() {
    this.super('whoami')();
    console.log("My name is " + this.name);
};

//Human inherits from Animal
var Human = inherit(Animal, 'Human', HumanSchemaExtension);

When we create an instance of the Human model whoami() prints the logs from the parent and child methods as expected:

var human = new Human({ name : "Jim" });
human.whoami();
// Prints:
//
// I have 2 legs.
// My name is Jim

The first time you use a parent method it is retrieved and bound to the current instance you call it on. After the first time the bound parent method is cached such that the second time you use it, during the lifetime of your model instance, it is retrieved from cache.

// Calling the method a second time uses a cached version of the parent method bound to the instance:
human.whoami();
// Prints:
//
// I have 2 legs.
// My name is Jim