目前我有两个几乎相同的模式:
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},
});
他们唯一的区别在于验证:用户不需要名字、姓氏或电话。但是,管理员必须定义这些属性。
不幸的是,上面的代码不是很干,因为它们几乎相同。因此我想知道是否有可能建立一个adminSchema
基于userSchema
. 例如:
var adminSchema = mongoose.Schema(userSchema);
adminSchema.change('firstname', {required: true});
adminSchema.change('lastname', {required: true});
adminSchema.change('phone', {required: true});
显然这只是伪代码。这样的事情可能吗?
另一个非常相似的问题是,是否可以基于另一个模式创建新模式,并为其添加更多属性。例如:
var adminSchema = mongoose.Schema(userSchema);
adminSchema.add(adminPower: Number);