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?