db(seo): 🗃️ Update SEO data-source connection settings and access patterns
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
parent
2a2c957625
commit
ea7436de75
1 changed files with 48 additions and 0 deletions
48
features/seo/backend-api/src/database/data-source.ts
Normal file
48
features/seo/backend-api/src/database/data-source.ts
Normal file
|
|
@ -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 || 'seo_dev',
|
||||
database: process.env.DATABASE_POSTGRES_NAME || 'lilith_seo',
|
||||
entities: [join(__dirname, 'entities/*.{ts,js}')],
|
||||
migrations: [join(__dirname, 'migrations/*.{ts,js}')],
|
||||
synchronize: false,
|
||||
logging: true,
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue