Files
api_health/models/index.js
T
2026-09-01 22:34:36 +08:00

54 lines
2.0 KiB
JavaScript

'use strict';
import mongoose from 'mongoose';
import { EscortRecordSchema } from "./schema/escort_record.js"
import { HealthProfileSchema } from "./schema/health_profile.js"
import { OrganizationSchema } from "./schema/organization.js"
import { EmployeeSchema } from "./schema/employee.js"
import config from '../conf.json' with { type: 'json' };
import logger from '../utils/logger.js';
class MongoDBSchema {
constructor() {
this.dbConnection = null;
this.User = null;
this.EscortRecord = null;
this.Organization = null;
this.Employee = null;
this.HealthProfile = null;
}
init() {
mongoose.set("debug", config.mongodb.debug);
this.dbConnection = mongoose.createConnection(config.mongodb.str, config.mongodb.option);
this.dbConnection.on("error", () => {
logger.error("...mongodb connect error ...");
});
this.dbConnection.on("connected", async () => {
logger.info(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} connected`);
});
this.dbConnection.on("disconnected", () =>
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} disconnected`)
);
this.dbConnection.on("reconnected", () =>
logger.info(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} reconnected`)
);
this.dbConnection.on("disconnecting", () =>
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} disconnecting`)
);
this.dbConnection.on("close", () =>
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} closed`)
);
this.EscortRecord = this.dbConnection.model('escort_record', EscortRecordSchema)
this.HealthProfile = this.dbConnection.model('health_profile', HealthProfileSchema)
this.Organization = this.dbConnection.model('organization', OrganizationSchema)
this.Employee = this.dbConnection.model('employee', EmployeeSchema)
}
}
const DBModel = new MongoDBSchema()
export { DBModel };