import path from "node:path"; import type { AppPersistenceConfig } from "../../presentation/config/AppConfig"; export class CodemationDatabaseUrlParser { parse(url: string, consumerRoot: string): AppPersistenceConfig { const trimmed = url.trim(); if (trimmed.length === 0) { throw new Error("CODEMATION_DATABASE_URL is empty."); } if (trimmed.toLowerCase().startsWith("sqlite://")) { const remainder = trimmed.slice("sqlite://".length); const filePath = path.isAbsolute(remainder) ? remainder : path.resolve(consumerRoot, remainder); return { kind: "sqlite", databaseFilePath: filePath }; } if (trimmed.toLowerCase().startsWith("pgsql://")) { return { kind: "postgresql", databaseUrl: `postgresql://${trimmed.slice("pgsql://".length)}` }; } if (trimmed.toLowerCase().startsWith("postgresql://") || trimmed.toLowerCase().startsWith("postgres://")) { return { kind: "postgresql", databaseUrl: trimmed }; } throw new Error( `Unsupported CODEMATION_DATABASE_URL scheme: "${trimmed}". ` + `Use sqlite://, pgsql://, postgresql://, or postgres://.`, ); } }