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 db: Db;
static async init(logger?: Logger) { static async init(logger?: Logger) {
this.db = await this._getMongoDb(BackConfig.mongoDb, logger); logger?.log(`Start connecting db...`)
} const client = await new MongoClient(BackConfig.mongoDb).connect();
logger?.log(`Db connected successfully...`)
private static _getMongoDb(uri: string, logger?: Logger): Promise<Db> { this.db = client.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;
} }
static collection<T extends keyof DbCollectionType>(col: T): Collection<DbCollectionType[T]> { static collection<T extends keyof DbCollectionType>(col: T): Collection<DbCollectionType[T]> {

View File

@ -1,3 +1,4 @@
import assert from 'assert';
import { HttpClient } from 'tsrpc'; import { HttpClient } from 'tsrpc';
import { Post } from '../../src/shared/protocols/models/Post'; import { Post } from '../../src/shared/protocols/models/Post';
import { serviceProto } from '../../src/shared/protocols/serviceProto'; import { serviceProto } from '../../src/shared/protocols/serviceProto';