46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
import mongoose from 'mongoose';
|
|
import { UserSchema } from "./schema/user.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.Hospital = null;
|
|
}
|
|
|
|
init() {
|
|
mongoose.set("debug", config.mongodb.debug);
|
|
|
|
this.dbConnection = mongoose.createConnection(config.mongodb.str, config.mongodb.option);
|
|
this.dbConnection.on("error", () => {
|
|
logger.error.bind(logger, "...mongodb connect error ...")
|
|
});
|
|
this.dbConnection.on("connected", async () => {
|
|
logger.info("Mongodb: " + config.mongodb.str + " connected");
|
|
});
|
|
this.dbConnection.on("disconnected", () =>
|
|
logger.warn("Mongodb: " + config.mongodb.str + " disconnected")
|
|
);
|
|
this.dbConnection.on("reconnected", () =>
|
|
logger.info("Mongodb: " + config.mongodb.str + " reconnected")
|
|
);
|
|
this.dbConnection.on("disconnecting", () =>
|
|
logger.warn("Mongodb: " + config.mongodb.str + " disconnecting")
|
|
);
|
|
this.dbConnection.on("close", () =>
|
|
logger.warn("Mongodb: " + config.mongodb.str + " closed")
|
|
);
|
|
|
|
this.User = this.dbConnection.model('user', UserSchema)
|
|
}
|
|
}
|
|
|
|
const DBModel = new MongoDBSchema()
|
|
export { DBModel };
|
|
|