【问题标题】:Inheritance in mongoose猫鼬的继承
【发布时间】:2012-12-23 03:01:46
【问题描述】:

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

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

【问题讨论】:

    标签: javascript node.js inheritance express mongoose


    【解决方案1】:

    您可能想看看使用 Mongoose 插件:

    这里有一个“开箱即用”的插件:

    【讨论】:

    • 感谢官方网站的示例。但我想看到一个完整的解决方案。我想我并不孤单。
    • 查看第二个链接。
    • 感谢您的链接。这是我需要的。
    • Mongoose 现在默认包含此功能。您不必使用该插件。 github.com/LearnBoost/mongoose/pull/1647
    • 在我看来,默认行为非常丑陋; mongoose-schema-extend 真的很性感,因为它实际上扩展了模式原型,因此它的行为更像Backbone.Model.extend({}) 之类的东西。对我来说感觉更自然! (并且有更好的记录)
    【解决方案2】:

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

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

    【讨论】:

    【解决方案3】:

    检查 Mongoose 框架的模型:

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

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

    首先在一个新的“rode model”中定义你的架构:

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

    现在你应该调用extend方法:

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

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

    【讨论】:

      【解决方案4】:

      我知道这是一个老歌,但我来到这里是为了寻找同一个问题的答案,最后做了一些不同的事情。我不想使用鉴别器,因为所有文档都存储在同一个集合中。

      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');
      

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

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

      【讨论】:

      • 这是最好的解决方案!
      【解决方案5】:

      如果您想使用不同的集合

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

      用法:

      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

      【讨论】:

        猜你喜欢
        • 2014-07-15
        • 2013-09-15
        • 1970-01-01
        • 2015-05-12
        • 2012-12-14
        • 2017-06-15
        • 2019-06-04
        • 2017-08-18
        • 2015-06-27
        相关资源
        最近更新 更多