Skip to content

Instantly share code, notes, and snippets.

@jirevwe
Created October 23, 2018 14:39
Show Gist options
  • Save jirevwe/d848a1e98f0968ea5abf79999a805f67 to your computer and use it in GitHub Desktop.
Save jirevwe/d848a1e98f0968ea5abf79999a805f67 to your computer and use it in GitHub Desktop.
Mongoose Schema Inheritance
const mongoose = require('mongoose')
const { PersonSchema } = require('./person')
class ManSchema extends PersonSchema {
constructor(obj, options) {
super(obj, options)
this.add({ age: { type: String, required: true } })
}
}
const Man = new ManSchema({}, { timestamps: true }, { usePushEach: true })
module.exports = mongoose.model('Man', Man)
const mongoose = require('mongoose')
const { Schema } = mongoose
class PersonSchema extends Schema {
constructor(obj, options) {
super(obj, options)
this.add({ name: { type: String, required: true } })
}
}
const Person = new PersonSchema({}, { timestamps: true }, { usePushEach: true })
exports.PersonSchema = PersonSchema
exports.Person = mongoose.model('Person', Person)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment