javascript 猫鼬中的继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14228882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:05:10  来源:igfitidea点击:

Inheritance in mongoose

javascriptnode.jsinheritanceexpressmongoose

提问by Erik

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

您好,我需要在 mongoose 库中继承我的模式。有没有完整的插件?或者我自己应该怎么做?

I need to inherit all pre, post, init middleware from a Base schema also.

我还需要从 Base 模式继承所有 pre、post、init 中间件。

采纳答案by hunterloftis

You may want to look at using a Mongoose Plugin:

你可能想看看使用猫鼬插件:

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

一个可以“开箱即用”的插件在这里:

回答by regretoverflow

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

对于寻求此功能的其他人,Mongoose 3.8 现在具有通过鉴别器功能的模式继承:

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

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

回答by Michael A. Vickers

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

新模型.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.

所有 4 个字段都将在 NewModelItem 中。将 ModelBase 用于要使用相同字段/选项/等的其他模型。在我的项目中,我把时间戳放在那里。

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.

Schema.add 在 Schema 构造函数中被调用,因此模型应该被组装,就好像所有字段都是在原始构造函数调用中发送的一样。

回答by Do Async

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

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

回答by marian2js

Check the models of this framework for Mongoose:

检查 Mongoose 的这个框​​架的模型:

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

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

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

这是谁使用此框架扩展 shemas 的示例:

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

但是,请记住从框架 github 中提取的此注释:“两个模型将在 MongoDB 上共享相同的集合。扩展模型的文档将具有属性 _type 以进行区分。”