Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose: extending schemas

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},  }); 

And

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.

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); 
like image 462
Tom Avatar asked Aug 19 '13 15:08

Tom


People also ask

Can Mongoose schema be changed?

There's nothing built into Mongoose regarding migrating existing documents to comply with a schema change. You need to do that in your own code, as needed.

Is schema a class in Mongoose?

Mongoose allows creating schemas from ES6 classes. The loadClass() function lets you pull in methods, statics, and virtuals from an ES6 class. A class method maps to a schema method, a static method maps to a schema static, and getters/setters map to virtuals.

What does $Set do in Mongoose?

The $set operator replaces the value of a field with the specified value.

Is Mongoose schema based?

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.


1 Answers

Mongoose 3.8.1 now has support for Discriminators. A sample, from here: http://mongoosejs.com/docs/api.html#model_Model.discriminator

function BaseSchema() {   Schema.apply(this, arguments);    this.add({     name: String,     createdAt: Date   }); } util.inherits(BaseSchema, Schema);  var PersonSchema = new BaseSchema(); var BossSchema = new BaseSchema({ department: String });  var Person = mongoose.model('Person', PersonSchema); var Boss = Person.discriminator('Boss', BossSchema); 
like image 153
regretoverflow Avatar answered Sep 21 '22 22:09

regretoverflow