Skip to content

Commit

Permalink
refactor: better postgres connection param typing (immich-app#13800)
Browse files Browse the repository at this point in the history
  • Loading branch information
zackpollard authored and bdavis2-PCTY committed Nov 18, 2024
1 parent 11a9184 commit 993f505
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions server/src/interfaces/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -37,7 +37,7 @@ export interface EnvData {
};

database: {
config: PostgresConnectionOptions;
config: PostgresConnectionOptions & DatabaseConnectionParams;
skipMigrations: boolean;
vectorExtension: VectorExtension;
};
Expand Down
16 changes: 16 additions & 0 deletions server/src/interfaces/database.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion server/src/repositories/config.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions server/src/services/database.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe(DatabaseService.name, () => {
mockEnvData({
database: {
config: {
connectionType: 'parts',
type: 'postgres',
host: 'database',
port: 5432,
Expand Down Expand Up @@ -290,6 +291,7 @@ describe(DatabaseService.name, () => {
mockEnvData({
database: {
config: {
connectionType: 'parts',
type: 'postgres',
host: 'database',
port: 5432,
Expand All @@ -313,6 +315,7 @@ describe(DatabaseService.name, () => {
mockEnvData({
database: {
config: {
connectionType: 'parts',
type: 'postgres',
host: 'database',
port: 5432,
Expand Down
2 changes: 2 additions & 0 deletions server/test/repositories/config.repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const envData: EnvData = {

database: {
config: {
connectionType: 'parts',
database: 'immich',
type: 'postgres',
host: 'database',
port: 5432,
Expand Down

0 comments on commit 993f505

Please sign in to comment.