ms

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.

Showing:

Popularity

Downloads/wk

0

GitHub Stars

2

Maintenance

Last Commit

6yrs ago

Contributors

1

Package

Dependencies

0

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial