Inheritance in mongoose

佐手、 提交于 2019-12-30 07:58:23

问题


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 middleware from a Base schema also.


回答1:


You may want to look at using a Mongoose Plugin:

  • http://mongoosejs.com/docs/plugins.html

A plugin that does what you want 'out of the box' is here:

  • https://github.com/briankircho/mongoose-schema-extend



回答2:


For others looking for this functionality, Mongoose 3.8 now has Schema Inheritance via Discriminator functionality:

https://github.com/LearnBoost/mongoose/pull/1647




回答3:


If you want to use different collections:

function extendSchema (Schema, definition, options) {
  return new mongoose.Schema(
    Object.assign({}, Schema.obj, definition),
    options
  );
}

Usage:

const UserSchema = new mongoose.Schema({
  firstname: {type: String},
  lastname: {type: String}
});

const ClientSchema = extendSchema(UserSchema, {
  phone: {type: String, required: true}
});

https://www.npmjs.com/package/mongoose-extend-schema




回答4:


I know this is an oldie, but I arrived here looking for the answer to the same question and ended up doing something a little different. I don't want to use discriminators because all the documents are stored in the same collection.

ModelBase.js

var db = require('mongoose');

module.exports = function(paths) {
    var schema = new db.Schema({ 
        field1: { type: String, required: false, index: false },
        field2: { type: String, required: false, index: false } 
    }, { 
        timestamps: { 
            createdAt: 'CreatedOn', 
            updatedAt: 'ModifiedOn' 
        } 
    });

    schema.add(paths);

    return schema;
};

NewModel.js

var db = require('mongoose');
var base = require('./ModelBase');
var schema = new base({
    field3: { type: String, required: false, index: false },
    field4: { type: String, required: false, index: false }
});

db.model('NewModelItem', schema, 'NewModelItems');

All 4 fields will be in NewModelItem. Use ModelBase for other models where you want to use the same fields/options/etc. In my project I put the timestamps in there.

Schema.add is called in the Schema constructor so the model should be assembled as if all the fields were sent in the original constructor call.




回答5:


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."



来源:https://stackoverflow.com/questions/14228882/inheritance-in-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!