From 22121e32794f0d59a2239eacbdbb4d2b7e82e9eb Mon Sep 17 00:00:00 2001 From: King Wang Date: Mon, 28 Jun 2021 23:50:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mongodb-crud/backend/src/models/Global.ts | 23 ++++--------------- .../backend/test/api/post.test.ts | 1 + 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/examples/mongodb-crud/backend/src/models/Global.ts b/examples/mongodb-crud/backend/src/models/Global.ts index 7e31c83..ff5f945 100644 --- a/examples/mongodb-crud/backend/src/models/Global.ts +++ b/examples/mongodb-crud/backend/src/models/Global.ts @@ -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 { - logger?.log(`Start connecting db...(${uri})`) - let promise = new Promise((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(col: T): Collection { diff --git a/examples/mongodb-crud/backend/test/api/post.test.ts b/examples/mongodb-crud/backend/test/api/post.test.ts index f443a39..4c939c8 100644 --- a/examples/mongodb-crud/backend/test/api/post.test.ts +++ b/examples/mongodb-crud/backend/test/api/post.test.ts @@ -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';