This commit is contained in:
King Wang
2021-06-28 23:50:42 +08:00
parent a0d1437b2d
commit 22121e3279
2 changed files with 5 additions and 19 deletions

View File

@@ -8,25 +8,10 @@ export class Global {
static db: Db;
static async init(logger?: Logger) {
this.db = await this._getMongoDb(BackConfig.mongoDb, logger);
}
private static _getMongoDb(uri: string, logger?: Logger): Promise<Db> {
logger?.log(`Start connecting db...(${uri})`)
let promise = new Promise<Db>((rs, rj) => {
MongoClient.connect(uri, {
useUnifiedTopology: true,
}, (err, client) => {
if (err) {
logger?.error('× Failed connected db.', err)
rj(err);
} else {
logger?.log(`√ Connect db succ. (${uri})`)
rs(client.db());
}
})
})
return promise;
logger?.log(`Start connecting db...`)
const client = await new MongoClient(BackConfig.mongoDb).connect();
logger?.log(`Db connected successfully...`)
this.db = client.db();
}
static collection<T extends keyof DbCollectionType>(col: T): Collection<DbCollectionType[T]> {