-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathormconfig.ts
46 lines (39 loc) · 1.45 KB
/
ormconfig.ts
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
37
38
39
40
41
42
43
44
45
46
import 'reflect-metadata';
import { DataSource, DataSourceOptions } from 'typeorm';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import * as fs from 'fs';
import * as dotenv from 'dotenv';
import { ConfigEnum } from './src/enums/config.enum';
// 通过环境变量读取不同的.env文件
function getEnv(env: string): Record<string, unknown> {
if (fs.existsSync(env)) {
return dotenv.parse(fs.readFileSync(env));
}
}
function buildConnectionOptions() {
const defaultConfig = getEnv('.env');
const envConfig = getEnv(`.env.${process.env.NODE_ENV || 'development'}`);
const config = { ...defaultConfig, ...envConfig };
const entitiesDir =
process.env.NODE_ENV === 'test'
? [__dirname + '/**/*.entity.ts']
: [__dirname + '/**/*.entity{.js,.ts}'];
return {
type: config[ConfigEnum.DB_TYPE],
host: config[ConfigEnum.DB_HOST],
port: config[ConfigEnum.DB_PORT],
username: config[ConfigEnum.DB_USERNAME],
password: config[ConfigEnum.DB_PASSWORD],
database: config[ConfigEnum.DB_DATABASE],
synchronize: config[ConfigEnum.DB_SYNC],
entities: entitiesDir,
// logging: process.env.NODE_ENV === 'development', // 接口请求时,控制台打印原始的mysql语句
logging: false,
} as TypeOrmModuleOptions;
}
export const connectionParams = buildConnectionOptions();
export const AppDataSource = new DataSource({
...connectionParams,
migrations: [],
subscribers: [],
} as DataSourceOptions);