Search code examples
node.jsmongodbmongoosemicroservicesmongoose-schema

use mongoose schema over multiple microservices


my application is split into multiple microservices that are running on heroku dynos (they can't access each others files). Sometimes, there are multiple microservices working with one collection. Therefore, both of the microservices need the according mongoose schema.

However, not both microservices need the full schema. For example, microservice A needs the full schema whereas microservice B only needs a few fields of that schema.

Example schema inside microservice A:

var AccountSchema = mongoose.Schema({
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    phone: { type: String, required: true, unique: true },
    forename: { type: String, required: true },
    surname: { type: String, required: true },
    middleInitals: { type: String, required: false },
    failedLoginAttempts: { type: Number, required: true, default: 0 },
    lockUntil: { type: Number },
    createdAt: { type: Date, default: Date.now }
})

Example Schema inside microservice B:

var AccountSchema = mongoose.Schema({
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    failedLoginAttempts: { type: Number, required: true, default: 0 },
    lockUntil: { type: Number },
    createdAt: { type: Date, default: Date.now }
})

My approach

I would just go ahead and create a new schema in each microservice, containing only the needed fields. However, I am not sure if there will be any problems when multiple microservices register a new schema to the MongoDB database? For example, both microservices would attempt to create an index for the unique field. Will there be any performance issues?

Does anybody have a different approach I could use? Is this even a valid approach to go with?

Thanks in advance :)


Solution

  • It's a valid approach. you can have 2 schemas pointing to the same collection. i have tested it and it works.

    Mongoose is an Object Data Modeling (ODM) library, and you can have 2 objects looking at the same collection /(Table or view in SQL) - no problem with that.

    No reason for performance problems, so long you got the right index. no relation to Object Data Modeling.

    You might want to add some type key, so you can find only type1/type2 accounts on get request. On find, you can restrict getting the right fields with projection.

    I think you should have only 2 keys in the index – email + password. If you have phone index and microservice B: don't include a phone –you will have a violation at the unique index of phone.

    But if you really want a unique phone index you can make an override. You can generate temp unique value for phone for mircoservice B (using auto-Generated or duplicate the email value), you will ignore this value on mircoservice B and only find/ update / present phone in microsaervice A, where you have a valid phone. When user change from accountB type to accountA type – you must make sure to replace the wrong phone number with a valid one.

    I see no problem in 2 schemas to same collection - you just need to mange your indexes the right way – to avoid collisions, and to insure you can differentiate the different accounts type in the collection.