From a9245e42ea4127743051b3a9c41eb22e59851b78 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 3 Dec 2024 09:44:38 +0100 Subject: [PATCH] chore: some small adjustments Signed-off-by: blam --- .../database/connectors/postgres.ts | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/packages/backend-defaults/src/entrypoints/database/connectors/postgres.ts b/packages/backend-defaults/src/entrypoints/database/connectors/postgres.ts index 5f75fb3cc3..e7e973ae4b 100644 --- a/packages/backend-defaults/src/entrypoints/database/connectors/postgres.ts +++ b/packages/backend-defaults/src/entrypoints/database/connectors/postgres.ts @@ -69,7 +69,6 @@ export async function buildPgDatabaseConfig( overrides?: Knex.Config, ) { const config = mergeDatabaseConfig( - { connection: { type: 'default' } }, dbConfig.get(), { connection: getPgConnectionConfig(dbConfig, !!overrides), @@ -78,48 +77,48 @@ export async function buildPgDatabaseConfig( overrides, ); - let transformedConfig = config; + const sanitizedConfig = JSON.parse(JSON.stringify(config)); - if (config.connection.type) { - if (config.connection.type === 'cloudsql') { - if (config.client !== 'pg') { - throw new Error('Cloud SQL only supports the pg client'); - } + // Trim additional properties from the connection object passed to knex + delete sanitizedConfig.connection.type; + delete sanitizedConfig.connection.instance; - if (!config.connection.instance) { - throw new Error('Missing instance connection name for Cloud SQL'); - } - - const { - Connector: CloudSqlConnector, - IpAddressTypes, - AuthTypes, - } = await import('@google-cloud/cloud-sql-connector'); - const connector = new CloudSqlConnector(); - const clientOpts = await connector.getOptions({ - instanceConnectionName: config.connection.instance, - ipType: IpAddressTypes.PUBLIC, - authType: AuthTypes.IAM, - }); - - transformedConfig = { - ...config, - client: 'pg', - connection: { - ...config.connection, - ...clientOpts, - }, - }; - } else if (config.connection.type !== 'default') { - throw new Error(`Unknown connection type: ${config.connection.type}`); - } + if (config.connection.type === 'default' || !config.connection.type) { + return sanitizedConfig; } - // Remove the connection type and instance from the config - delete transformedConfig.connection.type; - delete transformedConfig.connection.instance; + if (config.connection.type !== 'cloudsql') { + throw new Error(`Unknown connection type: ${config.connection.type}`); + } - return transformedConfig; + if (config.client !== 'pg') { + throw new Error('Cloud SQL only supports the pg client'); + } + + if (!config.connection.instance) { + throw new Error('Missing instance connection name for Cloud SQL'); + } + + const { + Connector: CloudSqlConnector, + IpAddressTypes, + AuthTypes, + } = await import('@google-cloud/cloud-sql-connector'); + const connector = new CloudSqlConnector(); + const clientOpts = await connector.getOptions({ + instanceConnectionName: config.connection.instance, + ipType: IpAddressTypes.PUBLIC, + authType: AuthTypes.IAM, + }); + + return { + ...sanitizedConfig, + client: 'pg', + connection: { + ...sanitizedConfig.connection, + ...clientOpts, + }, + }; } /**