-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata-source.ts
More file actions
36 lines (31 loc) · 1.11 KB
/
data-source.ts
File metadata and controls
36 lines (31 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// data-source.ts
import { existsSync } from 'fs';
import { config as loadEnv } from 'dotenv';
import { DataSource, DataSourceOptions } from 'typeorm';
import configuration from './src/config/configuration';
import { join } from 'path';
const nodeEnv = process.env.NODE_ENV || 'development';
const envPath = `.env.${nodeEnv}`;
if (existsSync(envPath)) {
console.log(`🔑 Loading environment from ${envPath}`);
loadEnv({ path: envPath });
} else {
console.log(`🔑 No env file at ${envPath}; using process.env (${nodeEnv})`);
}
const config = configuration();
const dbConfig = config.postgres;
const appEnv = config.node?.env ?? nodeEnv;
const options: DataSourceOptions = {
type: 'postgres',
host: dbConfig.host,
port: dbConfig.port,
username: dbConfig.username,
password: dbConfig.password,
database: dbConfig.database,
synchronize: false,
logging: appEnv !== 'production' ? 'all' : ['error'],
entities: [__dirname + '/**/*.schema.{ts,js}'],
migrations: [join(__dirname, 'src/migrations/*.{ts,js}')],
migrationsTableName: 'typeorm_migrations',
};
export const AppDataSource = new DataSource(options);