Created
October 23, 2018 14:39
-
-
Save jirevwe/d848a1e98f0968ea5abf79999a805f67 to your computer and use it in GitHub Desktop.
Mongoose Schema Inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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