diff --git a/server/src/config/services.ts b/server/src/config/services.ts index 5f66c470d..c944e36c2 100644 --- a/server/src/config/services.ts +++ b/server/src/config/services.ts @@ -126,6 +126,7 @@ import { } from "@/repositories/index.js"; import { ILogger } from "@/utils/logger.js"; import TimescaleDB from "@/db/TimescaleDB.js"; +import { AppError } from "@/utils/AppError.js"; export type InitializedServices = { settingsService: ISettingsService; @@ -175,25 +176,18 @@ export const initializeServices = async ({ }): Promise => { // Create DB - let dbType = "mongodb"; - const connectionString = envSettings.dbConnectionString; - - if (connectionString.startsWith("mongodb")) { - dbType = "mongodb"; - } else if (connectionString.startsWith("postgresql")) { - dbType = "postgresql"; - } + const dbType = envSettings.dbType; let db: IDb | null = null; if (dbType === "mongodb") { db = new MongoDB(logger, envSettings); - } else if (dbType === "postgresql") { + } else if (dbType === "timescaledb") { db = new TimescaleDB(logger, envSettings); } if (!db) { - throw new Error("Unsupported database type"); + throw new AppError({ message: "Unsupported database type", status: 500 }); } await db.connect(); diff --git a/server/src/index.ts b/server/src/index.ts index 7f8b4593e..83698b45c 100755 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,8 +1,8 @@ import { initializeServices } from "./config/services.js"; import { initializeControllers } from "./config/controllers.js"; import { createApp } from "./app.js"; -import { initShutdownListener } from "./shutdown.js"; -import { validateEnv } from "./validation/envValidation.js"; +import { initShutdownListener } from "@/shutdown.js"; +import { validateEnv } from "@/validation/envValidation.js"; import { fileURLToPath } from "url"; import path from "path"; import fs from "fs"; diff --git a/server/src/service/system/settingsService.ts b/server/src/service/system/settingsService.ts index ee90accca..f2daff550 100755 --- a/server/src/service/system/settingsService.ts +++ b/server/src/service/system/settingsService.ts @@ -1,5 +1,5 @@ import { ISettingsRepository } from "@/repositories/index.js"; -import { Settings, SettingsUpdate } from "@/types/index.js"; +import { type DbType, Settings, SettingsUpdate } from "@/types/index.js"; import { AppError } from "@/utils/AppError.js"; import { ValidatedEnv } from "@/validation/envValidation.js"; import type { StringValue } from "ms"; @@ -12,6 +12,7 @@ export type EnvConfig = { logLevel: string; clientHost: string; dbConnectionString: string; + dbType: DbType; }; export interface ISettingsService { @@ -35,6 +36,7 @@ export class SettingsService implements ISettingsService { logLevel: env.LOG_LEVEL, clientHost: env.CLIENT_HOST, dbConnectionString: env.DB_CONNECTION_STRING, + dbType: env.DB_TYPE, }; } diff --git a/server/src/types/settings.ts b/server/src/types/settings.ts index 405dc3efe..bcedfbe89 100644 --- a/server/src/types/settings.ts +++ b/server/src/types/settings.ts @@ -1,3 +1,6 @@ +export const DbTypes = ["timescaledb", "mongodb"] as const; +export type DbType = (typeof DbTypes)[number]; + export interface SettingsThresholds { cpu?: number; memory?: number; diff --git a/server/src/validation/envValidation.ts b/server/src/validation/envValidation.ts index 2b0e36fcf..447748516 100644 --- a/server/src/validation/envValidation.ts +++ b/server/src/validation/envValidation.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import { DbTypes } from "@/types/settings.js"; const envSchema = z.object({ // Server Configuration @@ -8,6 +9,7 @@ const envSchema = z.object({ // Database DB_CONNECTION_STRING: z.string().min(1, "Database connection string is required"), + DB_TYPE: z.enum(DbTypes).default("mongodb"), // JWT Authentication JWT_SECRET: z.string().min(1, "JWT_SECRET is required"),