Search code examples
javascriptnode.jsmongodbmongoosemongoose-schema

Inheritance method "virtual" in extend schema mongoose doesn't work


I have below method in model UserSchema:

userSchema.virtual('password')
    .set(function(password) {
            this._password = password;
            this.salt = this.makeSalt();
            this.hashed_password = this.encryptPassword(password);
    })
    .get(function() {
            return this._password;
    }
);

but when I create new teacher I have error hashed_password is required. So I think that method virtual from userSchema isn't inheritance in teacherSchema. How can I set that teacherSchema inheritances method like virtual from userSchema?

model

const mongoose = require('mongoose');
extend = require('mongoose-extend-schema');
const Schema = mongoose.Schema;

const userSchema = new Schema({
    name: {
        type: String,
        trim: true,
        required: true,
        maxLength: 32
    },
    surname: {
        type: String,
        trim: true,
        required: true,
        maxLength: 32
    },
    email: {
        type: String,
        unique: true,
        trim: true,
        required: true,
        lowercase: true
    },
hashed_password: {
        type: String,
        required: true
    },
    initials: String
});

userSchema.virtual('password')
    .set(function(password) {
            this._password = password;
            this.salt = this.makeSalt();
            this.hashed_password = this.encryptPassword(password);
    })
    .get(function() {
            return this._password;
    }
);



const studentSchema = extend(userSchema, {
    teachers: []   
});

const teacherSchema = extend(userSchema, {
    isActiveTutor: {
        type: Boolean,
        default: false
    }
})
  


const User =  mongoose.model('User', userSchema);
const Student = mongoose.model('Student', studentSchema);
const Teacher = mongoose.model('Teacher', teacherSchema);

module.exports = {
    User,
    Student,
    Teacher
}

Solution

  • You are using mongoose-extend-schema NPM and this only merge objects from source schema to new schema, like this,

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

    There is a new method in mongoose called clone(), this returns a deep copy of the schema, and a add(), Adds key path / schema type pairs to this schema and You can also add() another schema and copy over all paths, virtuals, getters, setters, indexes, methods, and statics.

    I am describing 2 approaches, you can use anyone from both,

    1) Clone parent schema and add new properties to it

    // Skipping your 1) User Schema and 2) Virtual Function, because its remain same
    
    const studentSchema = userSchema.clone();
    studentSchema.add({
        teachers: []
    });
    
    const teacherSchema = userSchema.clone();
    teacherSchema.add({
        isActiveTutor: {
            type: Boolean,
            default: false
        }
    });
    
    const User = mongoose.model('User', userSchema);
    const Student = mongoose.model('Student', studentSchema);
    const Teacher = mongoose.model('Teacher', teacherSchema);
    
    module.exports = { User, Student, Teacher }
    

    2) Create new schema and add parent schema to it

    // Skipping your 1) User Schema and 2) Virtual Function, because its remain same
    
    const studentSchema = new Schema({
        teachers: []
    }).add(userSchema);
    
    const teacherSchema = new Schema({
        isActiveTutor: {
            type: Boolean,
            default: false
        }
    }).add(userSchema);
    
    const User = mongoose.model('User', userSchema);
    const Student = mongoose.model('Student', studentSchema);
    const Teacher = mongoose.model('Teacher', teacherSchema);
    
    module.exports = { User, Student, Teacher }