From 5114ae830bc32770754d84e9089fc6345dd9369f Mon Sep 17 00:00:00 2001 From: Bas Meeuwissen Date: Fri, 10 Jan 2025 10:38:31 +0100 Subject: [PATCH 1/2] #366: first draft --- .../database/implementations/mongodb/MongoDb.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/integrations/database/implementations/mongodb/MongoDb.ts b/src/integrations/database/implementations/mongodb/MongoDb.ts index 00ee1fd4..ca3eb9fe 100644 --- a/src/integrations/database/implementations/mongodb/MongoDb.ts +++ b/src/integrations/database/implementations/mongodb/MongoDb.ts @@ -51,10 +51,7 @@ export default class MongoDB implements Driver this.#databaseName = databaseName; } - get connected() - { - return this.#connected; - } + get connected() { return this.#connected; } async connect(): Promise { @@ -62,7 +59,6 @@ export default class MongoDB implements Driver { this.#client = await this.#createClient(this.#connectionString); - this.#client.on('open', () => { this.#connected = true; }); this.#client.on('close', () => { this.#connected = false; }); this.#client.on('serverHeartbeatSucceeded', () => { this.#connected = true; }); @@ -271,7 +267,11 @@ export default class MongoDB implements Driver { try { - return await MongoClient.connect(connectionString); + const client = await MongoClient.connect(connectionString); + + this.#connected = true; + + return client; } catch (error: unknown) { From 57f787920f241e28d3e82be8d96212d6167c8cd3 Mon Sep 17 00:00:00 2001 From: Bas Meeuwissen Date: Fri, 10 Jan 2025 10:49:00 +0100 Subject: [PATCH 2/2] #366: createClient doesn't need it's own error The error itself is confusing, because it says Not connected. But the connection setup itself failed, and this indicated that there was no connection at all. The more generic DatabaseError is now thrown from the connect(). --- .../implementations/mongodb/MongoDb.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/integrations/database/implementations/mongodb/MongoDb.ts b/src/integrations/database/implementations/mongodb/MongoDb.ts index ca3eb9fe..c8ede90c 100644 --- a/src/integrations/database/implementations/mongodb/MongoDb.ts +++ b/src/integrations/database/implementations/mongodb/MongoDb.ts @@ -60,11 +60,12 @@ export default class MongoDB implements Driver this.#client = await this.#createClient(this.#connectionString); this.#client.on('close', () => { this.#connected = false; }); - this.#client.on('serverHeartbeatSucceeded', () => { this.#connected = true; }); this.#client.on('serverHeartbeatFailed', () => { this.#connected = false; }); this.#database = this.#getDatabase(this.#databaseName); + + this.#connected = true; } catch (error: unknown) { @@ -265,20 +266,7 @@ export default class MongoDB implements Driver async #createClient(connectionString: string): Promise { - try - { - const client = await MongoClient.connect(connectionString); - - this.#connected = true; - - return client; - } - catch (error: unknown) - { - const message = error instanceof Error ? error.message : undefined; - - throw new NotConnected(message); - } + return MongoClient.connect(connectionString); } #buildRecordData(data: Document, fields?: RecordField[]): RecordData