猫鼬:扩展架构 [英] Mongoose: extending schemas

查看:94
本文介绍了猫鼬:扩展架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有两个几乎相同的架构:

Currently I have two almost identical schemas:

var userSchema = mongoose.Schema({

    email: {type: String, unique: true, required: true, validate: emailValidator},
    passwordHash: {type: String, required: true},

    firstname: {type: String, validate: firstnameValidator},
    lastname: {type: String, validate: lastnameValidator},
    phone: {type: String, validate: phoneValidator},

});

还有

var adminSchema = mongoose.Schema({

    email: {type: String, unique: true, required: true, validate: emailValidator},
    passwordHash: {type: String, required: true},

    firstname: {type: String, validate: firstnameValidator, required: true},
    lastname: {type: String, validate: lastnameValidator, required: true},
    phone: {type: String, validate: phoneValidator, required: true},

});

它们唯一的区别在于验证:用户不需要名字,姓氏或电话.但是,管理员必须定义这些属性.

Their only difference is in validation: Users do not need a firstname, lastname or phone. Admins however must have these properties defined.

不幸的是,上面的代码不是很干,因为它们几乎是相同的.因此,我想知道是否有可能基于userSchema构建adminSchema.例如:

Unfortunately the above code is not very DRY, as they're almost identical. Therefore I am wondering if it is possible to build an adminSchema based on the userSchema. E.g.:

var adminSchema = mongoose.Schema(userSchema);
adminSchema.change('firstname', {required: true});
adminSchema.change('lastname', {required: true});
adminSchema.change('phone', {required: true});

很明显,这只是伪代码.这样可能吗?

Obviously that's just pseudocode. Is something like this possible?

另一个非常相似的问题是,是否有可能基于另一个创建新的架构,并为其添加更多属性.例如:

Another very similar question is if it is possible to create a new schema based on another, and add some more properties to it. For example:

var adminSchema = mongoose.Schema(userSchema);
    adminSchema.add(adminPower: Number);

推荐答案

有些人在其他地方建议使用utils.inherits 扩展架构.另一种简单的方法是简单地使用设置来设置对象并从中创建模式,如下所示:

Some people have in other places suggested using utils.inherits to extend schemas. Another simple way would be to simply set up an object with settings and create Schemas from it, like so:

var settings = {
  one: Number
};

new Schema(settings);

settings.two = Number;
new Schema(settings);

但是,这有点丑陋,因为您要修改同一对象.另外,我希望能够扩展插件和方法等.因此,我的首选方法如下:

It's a bit ugly though, since you're modifying the same object. Also I'd like to be able to extend plugins and methods etc. Thus my preferred method is the following:

function UserSchema (add) {
  var schema = new Schema({
    someField: String
  });

  if(add) {
    schema.add(add);
  }

  return schema;
}

var userSchema = UserSchema();
var adminSchema = UserSchema({
  anotherField: String
});

碰巧回答了第二个问题,即是的,您可以add()字段.因此,要修改Schema的某些属性,可以对上述函数进行修改以解决您的问题:

Which happens to answer your second question that yes, you can add() fields. So to modify some properties of the Schema, a modified version of the above function would solve your problem:

function UserSchema (add, nameAndPhoneIsRequired) {
  var schema = new Schema({
    //...
    firstname: {type: String, validate: firstnameValidator, required: nameAndPhoneIsRequired},
    lastname: {type: String, validate: lastnameValidator, required: nameAndPhoneIsRequired},
    phone: {type: String, validate: phoneValidator, required: nameAndPhoneIsRequired},
  });

  if(add) {
    schema.add(add);
  }

  return schema;
}

这篇关于猫鼬:扩展架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆