-
Notifications
You must be signed in to change notification settings - Fork 151
/
ormconfig.js
56 lines (49 loc) · 1.78 KB
/
ormconfig.js
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
47
48
49
50
51
52
53
54
55
56
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const { DataSource } = require('typeorm');
// config.yml 読み込み
const configFilePath = path.join('config', 'config.yml');
const config = yaml.load(fs.readFileSync(configFilePath, 'utf-8'));
// dist 下のディレクトリ設定
const distDBBasePath = path.join('dist', 'db');
const entitie = path.join(distDBBasePath, 'entities', '**', '*.js');
const subscriber = path.join(distDBBasePath, 'subscribers', '**', '*.js');
const migrations = [path.join(distDBBasePath, 'migrations', config.dbtype, '**', '*.js')];
// database の種類に応じた設定
let ormConfig;
switch (config.dbtype) {
case 'sqlite':
ormConfig = new DataSource({
type: 'sqlite',
database: path.join(__dirname, 'data', 'database.db'),
synchronize: false,
logging: false,
entities: [entitie],
subscribers: [subscriber],
migrationsRun: false,
migrations: migrations,
});
break;
case 'mysql':
ormConfig = new DataSource({
type: 'mysql',
host: config.mysql.host,
port: config.mysql.port,
username: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database,
charset: typeof config.mysql.charset === 'undefined' ? 'utf8mb4' : config.mysql.charset,
bigNumberStrings: false,
synchronize: false,
logging: false,
entities: [entitie],
subscribers: [subscriber],
migrationsRun: false,
migrations: migrations,
});
break;
default:
throw new Error('db config error');
}
module.exports = { ormConfig };