Mongoose: extending schemas

前端 未结 9 473
轮回少年
轮回少年 2020-12-05 10:10

Currently I have two almost identical schemas:

var userSchema = mongoose.Schema({

    email: {type: String, unique: true, required: true, validate: emailVal         


        
9条回答
  •  借酒劲吻你
    2020-12-05 10:40

    The simplest way for extending mongoose schema

    import { model, Schema } from 'mongoose';
    
    const ParentSchema = new Schema({
      fromParent: Boolean
    });
    
    const ChildSchema = new Schema({
      ...ParentSchema.obj,
      fromChild: Boolean // new properties come up here
    });
    
    export const Child = model('Child', ChildSchema);
    

提交回复
热议问题