优化
This commit is contained in:
parent
a0d1437b2d
commit
22121e3279
@ -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]> {
|
||||
|
@ -1,3 +1,4 @@
|
||||
import assert from 'assert';
|
||||
import { HttpClient } from 'tsrpc';
|
||||
import { Post } from '../../src/shared/protocols/models/Post';
|
||||
import { serviceProto } from '../../src/shared/protocols/serviceProto';
|
||||
|
Loading…
Reference in New Issue
Block a user