Mongoose:扩展模式 [英] Mongoose: extending schemas

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

问题描述

目前我有两个几乎相同的模式:

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.

不幸的是,上面的代码不是很 DRY,因为它们几乎相同.因此我想知道是否可以基于 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;
}

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

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