# mongoose-schema-extend

> mongoose schema inheritance and discriminator key extension

Latest version **0.2.2** (published 2016-02-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-schema-extend
pnpm add mongoose-schema-extend
yarn add mongoose-schema-extend
bun add mongoose-schema-extend
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2016-02-13 |
| First published | 2012-08-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 294 |
| Author | Brian Kirchoff |
| Maintainers | briankircho, sieira |
| Keywords | mongoose, inheritance, schema, discriminator, model |

## Links

- npm: https://www.npmjs.com/package/mongoose-schema-extend
- Repository: https://github.com/briankircho/mongoose-schema-extend
- Issues: https://github.com/briankircho/mongoose-schema-extend/issues
- npm.io page: https://npm.io/package/mongoose-schema-extend

## Dependencies (2)

- [owl-deepcopy](https://npm.io/package/owl-deepcopy.md) ~0.0.1
- [harmony-reflect](https://npm.io/package/harmony-reflect.md) ^1.4.2

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.2.2 (latest) — 2016-02-13
- 0.2.1 — 2016-02-09
- 0.2.0 — 2015-11-11
- 0.1.7 — 2013-11-21
- 0.1.6 — 2013-11-21
- 0.1.5 — 2013-09-28
- 0.1.4 — 2013-07-18
- 0.1.3 — 2013-01-20
- 0.1.2 — 2013-01-18
- 0.1.1 — 2012-08-10
- 0.1.0 — 2012-08-10

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

```javascript
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

```javascript
...
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

```javascript
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

---
_Source: https://npm.io/package/mongoose-schema-extend · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
