-
Notifications
You must be signed in to change notification settings - Fork 567
/
manager.ts
59 lines (50 loc) 路 1.31 KB
/
manager.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
import {
Config,
Init,
Inject,
Provide,
Scope,
ScopeEnum,
ServiceFactory,
delegateTargetPrototypeMethod,
MidwayCommonError,
} from '@midwayjs/core';
import * as TableStore from 'tablestore';
import { TableStoreClient } from './interface';
@Provide()
@Scope(ScopeEnum.Singleton)
export class TableStoreServiceFactory extends ServiceFactory<TableStoreClient> {
@Config('tableStore')
tableStoreConfig;
@Init()
async init() {
await this.initClients(this.tableStoreConfig);
}
async createClient(config): Promise<TableStoreClient> {
return new TableStore.Client(config) as any;
}
getName() {
return 'tableStore';
}
}
@Provide()
@Scope(ScopeEnum.Singleton)
export class TableStoreService implements TableStoreClient {
@Inject()
private serviceFactory: TableStoreServiceFactory;
private instance: TableStoreClient;
@Init()
async init() {
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('TableStore default instance not found.');
}
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface TableStoreService extends TableStoreClient {
// empty
}
delegateTargetPrototypeMethod(TableStoreService, [TableStore.Client]);