From d45fc41bc72ec8e2fb2fe1b13bcf9c38276e916a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 3 Oct 2022 23:14:32 +0200 Subject: [PATCH] backend-common: tweak DatabaseManager Signed-off-by: Patrik Oldsberg --- .../src/database/DatabaseManager.ts | 72 +++++++++---------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/packages/backend-common/src/database/DatabaseManager.ts b/packages/backend-common/src/database/DatabaseManager.ts index a58799a724..5adb862e38 100644 --- a/packages/backend-common/src/database/DatabaseManager.ts +++ b/packages/backend-common/src/database/DatabaseManager.ts @@ -312,52 +312,48 @@ export class DatabaseManager { return this.databaseCache.get(pluginId)!; } - const clientPromise = new Promise(async (resolve, reject) => { - try { - const pluginConfig = new ConfigReader( - this.getConfigForPlugin(pluginId) as JsonObject, - ); + const clientPromise = Promise.resolve().then(async () => { + const pluginConfig = new ConfigReader( + this.getConfigForPlugin(pluginId) as JsonObject, + ); - const databaseName = this.getDatabaseName(pluginId); - if (databaseName && this.getEnsureExistsConfig(pluginId)) { + const databaseName = this.getDatabaseName(pluginId); + if (databaseName && this.getEnsureExistsConfig(pluginId)) { + try { + await ensureDatabaseExists(pluginConfig, databaseName); + } catch (error) { + throw new Error( + `Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`, + ); + } + } + + let schemaOverrides; + if (this.getPluginDivisionModeConfig() === 'schema') { + schemaOverrides = this.getSchemaOverrides(pluginId); + if (this.getEnsureExistsConfig(pluginId)) { try { - await ensureDatabaseExists(pluginConfig, databaseName); + await ensureSchemaExists(pluginConfig, pluginId); } catch (error) { throw new Error( - `Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`, + `Failed to connect to the database to make sure that schema for plugin '${pluginId}' exists, ${error}`, ); } } - - let schemaOverrides; - if (this.getPluginDivisionModeConfig() === 'schema') { - schemaOverrides = this.getSchemaOverrides(pluginId); - if (this.getEnsureExistsConfig(pluginId)) { - try { - await ensureSchemaExists(pluginConfig, pluginId); - } catch (error) { - throw new Error( - `Failed to connect to the database to make sure that schema for plugin '${pluginId}' exists, ${error}`, - ); - } - } - } - - const databaseClientOverrides = mergeDatabaseConfig( - {}, - this.getDatabaseOverrides(pluginId), - schemaOverrides, - ); - - const client = createDatabaseClient( - pluginConfig, - databaseClientOverrides, - ); - this.startKeepaliveLoop(pluginId, client); - resolve(client); - } catch (e) { - reject(e); } + + const databaseClientOverrides = mergeDatabaseConfig( + {}, + this.getDatabaseOverrides(pluginId), + schemaOverrides, + ); + + const client = createDatabaseClient( + pluginConfig, + databaseClientOverrides, + ); + this.startKeepaliveLoop(pluginId, client); + return client; }); this.databaseCache.set(pluginId, clientPromise);