1

We are developing MicroServices using TypeScript. The backend is MongoDB and we are using Mongoose. One MicroService deals with School and Another MicroService deals with Training. Each MicroService maintains it's own database.

The School Model needs to have a reference to Training based on the trainingID field and not _ID field provided by MongoDB.

We are defining this reference as a plain String, such that the value of trainingID from Training Collection will be held in School Collection with the same field name as trainingID.

Here is the code so far:

SchoolModel.ts

import { Document, Schema, Model, model } from "mongoose";
import { ISchoolModel } from './ISchoolModel';

export const schoolSchema = new Schema({
  schoolId: String,
  schoolName: String,
  trainingId: String
});

export interface ISchool extends ISchoolModel, Document {

}

export const SchoolModel: Model<ISchool> = model<ISchool>("School", schoolSchema);

TrainingModel.ts

import { Document, Schema, Model, model } from "mongoose";
import { ITrainingModel } from './ITrainingModel';

export const trainingSchema = new Schema({
  trainingId: String,
  trainingName: String,
  trainingDescription: String
});

export interface ITraining extends ITrainingModel, Document {

}

export const TrainingModel: Model<ITraining> = model<ITraining>("Training", trainingSchema);

Question

Is this the best way to define reference to an entity from other database?

Travis
  • 12,001
  • 8
  • 39
  • 52
  • When I think of microservices, I think of self contained applications that can standalone and not have another service as a dependency. – user602525 Aug 03 '17 at 20:23
  • But when they must, they simply hold something like s primary key. I don't see anyway else to do this besides the way you describe. I'm guessing those trainingID girls are immutable. – Robert Moskal Aug 03 '17 at 20:26

1 Answers1

1

Unlikely, I would say.

The way you model your data here seems to indicate that a single school can have only one training assigned. That, to me, sounds kind of semantically wrong to begin with...?

If that really was your intention, you would want to put everything into the same document and simply project different parts of it from your individual microservices (if they continue to make sense given that setup).

Otherwise, if a school can indeed reference multiple trainings, you would need to have a reference to a collection of trainings in your schema. Read up on populate() to see how to best model relationships:

http://mongoosejs.com/docs/populate.html

dnickless
  • 10,733
  • 1
  • 19
  • 34
  • Thanks to everyone for their replies. @dnickless - In order to use populate() , I will have to define the relationship via "ref" and it will refer to "_ID". I don't want to do that. The trainings can be multiple. So, we may have an array of Trainings. But from School Service, we will be calling the Training Service to verify the validity of trainingIDs. Also, I don't think this is exact duplicate of the other question. The other question refers to not being able to maintain 2 different connections in Mongoose. – Santosh Thakur Aug 04 '17 at 15:17