Inheritance in mongoose

后端 未结 5 1184
北海茫月
北海茫月 2021-01-04 01:31

Hello I need to inherit my schemas in mongoose library. Are there complete plugins for that? Or how should I do that myself?

I need to inherit all pre, post, init mi

5条回答
  •  攒了一身酷
    2021-01-04 02:16

    Check the models of this framework for Mongoose:

    https://github.com/marian2js/rode#models-with-mongoose

    This is an example of who to extend shemas with this framework:

    First of all define your schema inside a new "rode model":

    var rode = require('rode');
    
    var User = rode.Model.extend({
      name: 'User',
      schema: {
        name: {
          type: 'String',
          unique: true
        },
        email: String,
        password: String
      }
    });
    

    Now you should call extend method:

    var Admin = User.extend({
      name: 'Admin',
      // The schema for admins is the schema for users + their own schema
      schema: {
        lastAccess: Date
      }
    });
    

    But have in mint this note extracted from the framework github: "Both models will share the same collection on MongoDB. Documents of the extended models will have an attribute _type to differentiate."

提交回复
热议问题