-
Notifications
You must be signed in to change notification settings - Fork 567
/
configuration.ts
69 lines (64 loc) 路 1.77 KB
/
configuration.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
67
68
69
import {
Configuration,
HealthResult,
ILifeCycle,
IMidwayContainer,
} from '@midwayjs/core';
import { MongooseDataSourceManager } from './manager';
@Configuration({
namespace: 'mongoose',
importConfigs: [
{
default: {
mongoose: {},
},
},
],
importConfigFilter: config => {
if (config['mongoose']) {
if (config['mongoose']['client']) {
config['mongoose']['dataSource'] =
config['mongoose']['dataSource'] || {};
config['mongoose']['dataSource']['default'] =
config['mongoose']['client'];
delete config['mongoose']['client'];
}
if (config['mongoose']['clients']) {
config['mongoose']['dataSource'] = config['mongoose']['clients'];
delete config['mongoose']['clients'];
}
}
return config;
},
})
export class MongooseConfiguration implements ILifeCycle {
mongooseDataSourceManager: MongooseDataSourceManager;
async onReady(container: IMidwayContainer) {
this.mongooseDataSourceManager = await container.getAsync(
MongooseDataSourceManager
);
}
async onStop(container: IMidwayContainer) {
await this.mongooseDataSourceManager.stop();
}
async onHealthCheck?(container: IMidwayContainer): Promise<HealthResult> {
const clientNames = this.mongooseDataSourceManager.getDataSourceNames();
// find status not ready
let clientName: any;
for (const name of clientNames) {
if (
(await this.mongooseDataSourceManager.isConnected(name)) &&
!this.mongooseDataSourceManager.isLowPriority(name)
) {
clientName = name;
break;
}
}
return {
status: !clientName,
reason: clientName
? `mongoose dataSource "${clientName}" is not ready`
: '',
};
}
}