diff --git a/features/payments/backend-api/src/data-source.ts b/features/payments/backend-api/src/data-source.ts new file mode 100644 index 000000000..fa87c0b3e --- /dev/null +++ b/features/payments/backend-api/src/data-source.ts @@ -0,0 +1,48 @@ +import { join, dirname } from 'path'; +import { fileURLToPath } from 'url'; +import { readFileSync } from 'fs'; + +import { DataSource } from 'typeorm'; + +// Load .env file for CLI usage (NestJS ConfigModule handles this in-app) +const envPath = join(dirname(fileURLToPath(import.meta.url)), '..', '.env'); +try { + const envContent = readFileSync(envPath, 'utf-8'); + for (const line of envContent.split('\n')) { + const trimmed = line.trim(); + if (!trimmed || trimmed.startsWith('#')) continue; + const eqIndex = trimmed.indexOf('='); + if (eqIndex === -1) continue; + const key = trimmed.slice(0, eqIndex); + const value = trimmed.slice(eqIndex + 1); + if (!process.env[key]) { + process.env[key] = value; + } + } +} catch { + // .env file may not exist in CI/production +} + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +/** + * TypeORM DataSource for migrations + * + * Usage: + * bun run migration:run - Run pending migrations + * bun run migration:revert - Revert last migration + * bun run migration:generate - Generate new migration from entity diff + */ +export default new DataSource({ + type: 'postgres', + host: process.env.DATABASE_HOST || 'localhost', + port: parseInt(process.env.DATABASE_PORT || '25432', 10), + username: process.env.DATABASE_POSTGRES_USER || 'lilith', + password: process.env.DATABASE_POSTGRES_PASSWORD || 'lilith', + database: process.env.DATABASE_POSTGRES_NAME || 'lilith_payments', + entities: [join(__dirname, 'entities/*.{ts,js}')], + migrations: [join(__dirname, 'database/migrations/*.{ts,js}')], + synchronize: false, + logging: true, +});