MCPcopy
hub / github.com/midwayjs/midway / MikroDataSourceManager

Class MikroDataSourceManager

packages/mikro/src/dataSourceManager.ts:15–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13@Provide()
14@Scope(ScopeEnum.Singleton)
15export class MikroDataSourceManager extends DataSourceManager<
16 MikroORM<IDatabaseDriver<Connection>>
17> {
18 @Config('mikro')
19 mikroConfig;
20
21 @Inject()
22 loggerService: MidwayLoggerService;
23
24 @Init()
25 async init() {
26 await this.initDataSource(this.mikroConfig, {
27 concurrent: true,
28 });
29 }
30
31 getName(): string {
32 return 'mikro';
33 }
34
35 protected async createDataSource(
36 config: any,
37 dataSourceName: string
38 ): Promise<MikroORM<IDatabaseDriver<Connection>>> {
39 if (config.logger && typeof config.logger === 'string') {
40 const logger = this.loggerService.getLogger(config.logger);
41 config.logger = message => {
42 logger.info(message);
43 };
44 }
45 // https://mikro-orm.io/docs/usage-with-nestjs#multiple-database-connections
46 if (!config.contextName) {
47 config.contextName = dataSourceName;
48 }
49 config.registerRequestContext = config.registerRequestContext ?? false;
50 return MikroORM.init(config);
51 }
52
53 protected async checkConnected(
54 dataSource: MikroORM<IDatabaseDriver<Connection>>
55 ): Promise<boolean> {
56 return dataSource.isConnected();
57 }
58
59 protected async destroyDataSource(
60 dataSource: MikroORM<IDatabaseDriver<Connection>>
61 ) {
62 if (dataSource.isConnected()) {
63 await dataSource.close();
64 }
65 }
66}

Callers

nothing calls this directly

Calls 3

ConfigFunction · 0.90
InjectFunction · 0.90
InitFunction · 0.90

Tested by

no test coverage detected