2021-07-20 09:47:16 +00:00
|
|
|
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as dotenv from 'dotenv';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
function buildConnectionOptions(filePath: string, env: string | undefined): TypeOrmModuleOptions {
|
2021-07-30 03:51:18 +00:00
|
|
|
let data: any = process.env;
|
2021-09-21 13:48:28 +00:00
|
|
|
|
|
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
|
|
data = { ...data, ...dotenv.parse(fs.readFileSync(filePath)) };
|
2021-07-30 03:51:18 +00:00
|
|
|
}
|
2021-07-20 09:47:16 +00:00
|
|
|
|
2021-07-30 04:14:20 +00:00
|
|
|
/* use the database connection URL if available ( Heroku postgres addon uses connection URL ) */
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const connectionParams = process.env.DATABASE_URL
|
|
|
|
|
? {
|
2021-12-10 03:13:05 +00:00
|
|
|
url: process.env.DATABASE_URL,
|
|
|
|
|
ssl: { rejectUnauthorized: false },
|
|
|
|
|
}
|
2021-09-21 13:48:28 +00:00
|
|
|
: {
|
2021-12-10 03:13:05 +00:00
|
|
|
database: data.PG_DB,
|
|
|
|
|
port: +data.PG_PORT || 5432,
|
|
|
|
|
username: data.PG_USER,
|
|
|
|
|
password: data.PG_PASS,
|
|
|
|
|
host: data.PG_HOST,
|
|
|
|
|
connectTimeoutMS: 5000,
|
|
|
|
|
extra: {
|
|
|
|
|
max: 25,
|
|
|
|
|
},
|
|
|
|
|
};
|
2021-07-30 04:14:20 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
const entitiesDir =
|
2021-09-21 13:48:28 +00:00
|
|
|
process.env.NODE_ENV === 'test' ? [__dirname + '/**/*.entity.ts'] : [__dirname + '/**/*.entity{.js,.ts}'];
|
2021-08-30 11:43:27 +00:00
|
|
|
|
2021-07-20 09:47:16 +00:00
|
|
|
return {
|
|
|
|
|
type: 'postgres',
|
2021-07-30 04:14:20 +00:00
|
|
|
...connectionParams,
|
2021-08-30 11:43:27 +00:00
|
|
|
entities: entitiesDir,
|
2021-07-20 09:47:16 +00:00
|
|
|
synchronize: false,
|
|
|
|
|
uuidExtension: 'pgcrypto',
|
|
|
|
|
migrationsRun: false,
|
2021-12-27 11:41:35 +00:00
|
|
|
migrationsTransactionMode: 'all',
|
2021-11-16 11:44:09 +00:00
|
|
|
logging: data.ORM_LOGGING || false,
|
2021-08-13 04:43:22 +00:00
|
|
|
migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
|
2022-01-04 08:04:12 +00:00
|
|
|
keepConnectionAlive: true,
|
2021-07-20 09:47:16 +00:00
|
|
|
cli: {
|
|
|
|
|
migrationsDir: 'migrations',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function determineFilePathForEnv(env: string | undefined): string {
|
|
|
|
|
if (env === 'test') {
|
|
|
|
|
return path.resolve(process.cwd(), '../.env.test');
|
|
|
|
|
} else {
|
|
|
|
|
return path.resolve(process.cwd(), '../.env');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function throwErrorIfFileNotPresent(filePath: string, env: string): void {
|
|
|
|
|
if (!fs.existsSync(filePath)) {
|
2021-07-30 03:51:18 +00:00
|
|
|
console.log(
|
2021-08-02 08:21:48 +00:00
|
|
|
`Unable to fetch database config from env file for environment: ${env}\n` +
|
2021-12-10 03:13:05 +00:00
|
|
|
'Picking up config from the environment'
|
2021-07-20 09:47:16 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fetchConnectionOptions(): TypeOrmModuleOptions {
|
|
|
|
|
const env: string | undefined = process.env.NODE_ENV;
|
|
|
|
|
const filePath: string = determineFilePathForEnv(env);
|
|
|
|
|
throwErrorIfFileNotPresent(filePath, env);
|
|
|
|
|
|
|
|
|
|
return buildConnectionOptions(filePath, env);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ormconfig: TypeOrmModuleOptions = fetchConnectionOptions();
|
|
|
|
|
export default ormconfig;
|