-
Notifications
You must be signed in to change notification settings - Fork 568
/
dataSourceManager.ts
66 lines (57 loc) 路 1.37 KB
/
dataSourceManager.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {
Provide,
Config,
Init,
Scope,
Inject,
ScopeEnum,
ApplicationContext,
DataSourceManager,
IMidwayContainer,
MidwayLoggerService,
} from '@midwayjs/core';
import { DataSource } from 'typeorm';
import { TypeORMLogger } from './logger';
@Provide()
@Scope(ScopeEnum.Singleton)
export class TypeORMDataSourceManager extends DataSourceManager<DataSource> {
@Config('typeorm')
typeormConfig;
@ApplicationContext()
applicationContext: IMidwayContainer;
@Inject()
baseDir: string;
@Inject()
loggerService: MidwayLoggerService;
@Init()
async init() {
await this.initDataSource(this.typeormConfig, this.baseDir);
}
getName(): string {
return 'typeorm';
}
protected async createDataSource(
config: any,
dataSourceName: string
): Promise<DataSource> {
if (config['migrations']) {
delete config['migrations'];
}
if (config['logging'] === undefined) {
config['logger'] = new TypeORMLogger(
this.loggerService.getLogger('typeormLogger')
);
}
const dataSource = new DataSource(config);
await dataSource.initialize();
return dataSource;
}
protected async checkConnected(dataSource: DataSource) {
return dataSource.isInitialized;
}
protected async destroyDataSource(dataSource: DataSource) {
if (dataSource.isInitialized) {
await dataSource.destroy();
}
}
}