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-schema-extend

v0.2.2

Published

mongoose schema inheritance and discriminator key extension

Downloads

2,734

Readme

mongoose-schema-extend

Implements schema inheritance and an optional discriminator key which is useful for storing different types of related documents in a collection and fetching them with the correct model type.

Notice

From version 0.2.1 mongoose-schema-extend is using harmony proxies. You will need to add the flag --harmony_proxies to your start command if you're using it, or use a previous version.

Usage

Install via NPM

$ npm install mongoose-schema-extend

Schema Inheritance

You just require the library to add schema extend method

var mongoose = require('mongoose'),
    extend = require('mongoose-schema-extend');
var Schema = mongoose.Schema;

var PersonSchema = new Schema({
  name : String
}, { collection : 'users' });

var EmployeeSchema = PersonSchema.extend({
  department : String
});

var Person = mongoose.model('Person', PersonSchema),
    Employee = mongoose.model('Employee', EmployeeSchema);

var Brian = new Employee({
  name : 'Brian Kirchoff',
  department : 'Engineering'
});

...

Discriminator Key

By adding the discriminatorKey schema option, a key is added to your saved documents with the model name and is used when finding documents of different types to set them to the correct model

...
var VehicleSchema = mongoose.Schema({ 
  make : String,
}, { collection : 'vehicles', discriminatorKey : '_type' });

var CarSchema = VehicleSchema.extend({
  year : Number
});
var BusSchema = VehicleSchema.extend({
  route : Number
})

var Vehicle = mongoose.model('vehicle', VehicleSchema),
    Car = mongoose.model('car', CarSchema),
    Bus = mongoose.model('bus', BusSchema);

var accord = new Car({ 
  make : 'Honda',
  year : 1999
});
var muni = new Bus({
  make : 'Neoplan',
  route : 33
});

accord.save(function(err) {
  muni.save(function(err) {
    // vehicles are saved with the _type key set to 'car' and 'bus'
  });
})

At this point in MongoDB you will have documents similar to this

{ "_type" : "car", "make" : "Honda", "year" : 1999, "_id" : ObjectId("5024460368368a3007000002"), "__v" : 0 }
{ "_type" : "bus", "make" : "Neoplan", "route" : 33, "_id" : ObjectId("5024460368368a3007000003"), "__v" : 0 }

When querying, the objects model prototype will be set based on the _type field, so they are fully functional

Vehicle.find({}, function(err, vehicles) {
  console.log(vehicles[0]); // vehicles[0] instanceof Car === true
  console.log(vehicles[1]); // vehicles[1] instanceof Bus === true
});

Tests

To run the tests install mocha

npm install mocha -g

and then run

mocha