/
manager.ts
181 lines (158 loc) 路 3.84 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {
DataSourceManager,
delegateTargetMethod,
delegateTargetProperties,
MidwayCommonError,
Config,
Init,
Inject,
Logger,
Provide,
Scope,
ScopeEnum,
} from '@midwayjs/core';
import * as mongoose from 'mongoose';
@Provide()
@Scope(ScopeEnum.Singleton)
export class MongooseDataSourceManager extends DataSourceManager<mongoose.Connection> {
@Config('mongoose')
config;
@Logger('coreLogger')
logger;
@Inject()
baseDir: string;
@Init()
async init() {
if (this.config.client) {
this.logger.warn(
'[midway:mongoose] mongoose.client is deprecated, please use new config format.'
);
this.config.dataSource = {
default: this.config.client,
};
}
if (this.config.clients) {
this.logger.warn(
'[midway:mongoose] mongoose.clients is deprecated, please use new config format.'
);
this.config.dataSource = this.config.clients;
}
await this.initDataSource(this.config, this.baseDir);
}
protected async createDataSource(config: any, name: string) {
const connection = (await mongoose.createConnection(
config.uri,
config.options
)) as any;
connection.on('error', err => {
err.message = `[midway:mongoose] ${err.message}`;
this.logger.error(err);
});
/* istanbul ignore next */
connection.on('disconnected', () => {
this.logger.info(`[midway:mongoose] ${name} disconnected`);
});
connection.on('connected', () => {
this.logger.info(`[midway:mongoose] ${name} connected successfully`);
});
/* istanbul ignore next */
connection.on('reconnected', () => {
this.logger.info(`[midway:mongoose] ${name} reconnected successfully`);
});
if (config.entities) {
(connection as any).entities = config.entities;
}
return connection;
}
getName() {
return 'mongoose';
}
async destroyDataSource(dataSource: mongoose.Connection) {
await dataSource.close();
}
protected async checkConnected(
dataSource: mongoose.Connection
): Promise<boolean> {
return dataSource.readyState === mongoose.ConnectionStates.connected;
}
}
/**
* @deprecated
*/
@Provide()
@Scope(ScopeEnum.Singleton)
export class MongooseConnectionServiceFactory {
@Inject()
mongooseDataSourceManager: MongooseDataSourceManager;
createInstance(
config: any,
clientName: any
): Promise<void | mongoose.Connection> {
return this.mongooseDataSourceManager.createInstance(config, clientName);
}
get(id: string): mongoose.Connection {
return this.mongooseDataSourceManager.getDataSource(id);
}
getName(): string {
return 'mongoose';
}
has(id: string): boolean {
return this.mongooseDataSourceManager.hasDataSource(id);
}
}
/**
* @deprecated
*/
@Provide()
@Scope(ScopeEnum.Singleton)
export class MongooseConnectionService implements mongoose.Connection {
@Inject()
private mongooseDataSourceManager: MongooseDataSourceManager;
private instance: mongoose.Connection;
@Init()
async init() {
this.instance = this.mongooseDataSourceManager.getDataSource('default');
if (!this.instance) {
throw new MidwayCommonError('mongoose default instance not found.');
}
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MongooseConnectionService extends mongoose.Connection {
// empty
}
delegateTargetMethod(MongooseConnectionService, [
'close',
'collection',
'createCollection',
'deleteModel',
'dropCollection',
'dropDatabase',
'get',
'getClient',
'model',
'modelNames',
'openUri',
'plugin',
'set',
'setClient',
'startSession',
'transaction',
'useDb',
'watch',
]);
delegateTargetProperties(MongooseConnectionService, [
'client',
'collections',
'config',
'db',
'host',
'id',
'models',
'name',
'pass',
'plugins',
'port',
'readyState',
'user',
]);