44 lines
1.4 KiB
JavaScript
44 lines
1.4 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;
|
|
}
|
|
|
|
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.User = this.dbConnection.model('user', UserSchema)
|
|
}
|
|
}
|
|
|
|
const DBModel = new MongoDBSchema()
|
|
export { DBModel };
|
|
|