35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import { join } from 'node:path';
|
|
import { DataSource } from 'typeorm';
|
|
import { PHASE_A_ENTITIES } from './entities';
|
|
|
|
function requiredEnvironmentVariable(key: string): string {
|
|
const value = process.env[key];
|
|
if (!value) throw new Error(`Missing required environment variable: ${key}`);
|
|
return value;
|
|
}
|
|
|
|
function databasePort(): number {
|
|
const port = Number(process.env.DB_PORT ?? 5432);
|
|
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
throw new Error('DB_PORT must be a valid TCP port');
|
|
}
|
|
return port;
|
|
}
|
|
|
|
export const migrationDataSource = new DataSource({
|
|
type: 'postgres',
|
|
host: requiredEnvironmentVariable('DB_HOST'),
|
|
port: databasePort(),
|
|
database: requiredEnvironmentVariable('DB_NAME'),
|
|
username: requiredEnvironmentVariable('DB_MIGRATION_USER'),
|
|
password: requiredEnvironmentVariable('DB_MIGRATION_PASSWORD'),
|
|
entities: PHASE_A_ENTITIES,
|
|
migrations: [join(__dirname, 'migrations', '*.{js,ts}')],
|
|
migrationsTableName: 'typeorm_migrations',
|
|
synchronize: false,
|
|
migrationsRun: false,
|
|
logging: false,
|
|
applicationName: 'dhv2-migrations',
|
|
connectTimeoutMS: 5000,
|
|
});
|