From 68a4cc25dcf7581e2171b13ab7668177c64d3e43 Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Tue, 29 Oct 2024 15:11:34 +0000 Subject: [PATCH] refactor: better postgres connection param typing (#13800) --- server/src/interfaces/config.interface.ts | 4 ++-- server/src/interfaces/database.interface.ts | 16 ++++++++++++++++ server/src/repositories/config.repository.ts | 3 ++- server/src/services/database.service.spec.ts | 3 +++ .../test/repositories/config.repository.mock.ts | 2 ++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/server/src/interfaces/config.interface.ts b/server/src/interfaces/config.interface.ts index cf1bc18e3e..f221b7ee4f 100644 --- a/server/src/interfaces/config.interface.ts +++ b/server/src/interfaces/config.interface.ts @@ -3,7 +3,7 @@ import { QueueOptions } from 'bullmq'; import { RedisOptions } from 'ioredis'; import { OpenTelemetryModuleOptions } from 'nestjs-otel/lib/interfaces'; import { ImmichEnvironment, ImmichTelemetry, ImmichWorker, LogLevel } from 'src/enum'; -import { VectorExtension } from 'src/interfaces/database.interface'; +import { DatabaseConnectionParams, VectorExtension } from 'src/interfaces/database.interface'; import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions.js'; export const IConfigRepository = 'IConfigRepository'; @@ -37,7 +37,7 @@ export interface EnvData { }; database: { - config: PostgresConnectionOptions; + config: PostgresConnectionOptions & DatabaseConnectionParams; skipMigrations: boolean; vectorExtension: VectorExtension; }; diff --git a/server/src/interfaces/database.interface.ts b/server/src/interfaces/database.interface.ts index da0938c8b7..7745f54a32 100644 --- a/server/src/interfaces/database.interface.ts +++ b/server/src/interfaces/database.interface.ts @@ -7,6 +7,22 @@ export enum DatabaseExtension { export type VectorExtension = DatabaseExtension.VECTOR | DatabaseExtension.VECTORS; +export type DatabaseConnectionURL = { + connectionType: 'url'; + url: string; +}; + +export type DatabaseConnectionParts = { + connectionType: 'parts'; + host: string; + port: number; + username: string; + password: string; + database: string; +}; + +export type DatabaseConnectionParams = DatabaseConnectionURL | DatabaseConnectionParts; + export enum VectorIndex { CLIP = 'clip_index', FACE = 'face_index', diff --git a/server/src/repositories/config.repository.ts b/server/src/repositories/config.repository.ts index 077a19175e..3cb1ada412 100644 --- a/server/src/repositories/config.repository.ts +++ b/server/src/repositories/config.repository.ts @@ -132,8 +132,9 @@ const getEnv = (): EnvData => { connectTimeoutMS: 10_000, // 10 seconds parseInt8: true, ...(databaseUrl - ? { url: databaseUrl } + ? { connectionType: 'url', url: databaseUrl } : { + connectionType: 'parts', host: process.env.DB_HOSTNAME || 'database', port: Number(process.env.DB_PORT) || 5432, username: process.env.DB_USERNAME || 'postgres', diff --git a/server/src/services/database.service.spec.ts b/server/src/services/database.service.spec.ts index 79845e4297..958fb158a0 100644 --- a/server/src/services/database.service.spec.ts +++ b/server/src/services/database.service.spec.ts @@ -61,6 +61,7 @@ describe(DatabaseService.name, () => { mockEnvData({ database: { config: { + connectionType: 'parts', type: 'postgres', host: 'database', port: 5432, @@ -290,6 +291,7 @@ describe(DatabaseService.name, () => { mockEnvData({ database: { config: { + connectionType: 'parts', type: 'postgres', host: 'database', port: 5432, @@ -313,6 +315,7 @@ describe(DatabaseService.name, () => { mockEnvData({ database: { config: { + connectionType: 'parts', type: 'postgres', host: 'database', port: 5432, diff --git a/server/test/repositories/config.repository.mock.ts b/server/test/repositories/config.repository.mock.ts index 08c2394e85..ba85cc7d09 100644 --- a/server/test/repositories/config.repository.mock.ts +++ b/server/test/repositories/config.repository.mock.ts @@ -17,6 +17,8 @@ const envData: EnvData = { database: { config: { + connectionType: 'parts', + database: 'immich', type: 'postgres', host: 'database', port: 5432,