1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.ConnectionManager = void 0;
|
12 |
|
13 | const utils_1 = require("@poppinss/utils");
|
14 | const index_1 = require("./index");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | class ConnectionManager {
|
21 | constructor(logger, emitter) {
|
22 | Object.defineProperty(this, "logger", {
|
23 | enumerable: true,
|
24 | configurable: true,
|
25 | writable: true,
|
26 | value: logger
|
27 | });
|
28 | Object.defineProperty(this, "emitter", {
|
29 | enumerable: true,
|
30 | configurable: true,
|
31 | writable: true,
|
32 | value: emitter
|
33 | });
|
34 | |
35 |
|
36 |
|
37 | Object.defineProperty(this, "connections", {
|
38 | enumerable: true,
|
39 | configurable: true,
|
40 | writable: true,
|
41 | value: new Map()
|
42 | });
|
43 | |
44 |
|
45 |
|
46 |
|
47 | Object.defineProperty(this, "orphanConnections", {
|
48 | enumerable: true,
|
49 | configurable: true,
|
50 | writable: true,
|
51 | value: new Set()
|
52 | });
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | handleDisconnect(connection) {
|
58 | |
59 |
|
60 |
|
61 |
|
62 | if (this.orphanConnections.has(connection)) {
|
63 | this.orphanConnections.delete(connection);
|
64 | this.emitter.emit('db:connection:disconnect', connection);
|
65 | this.logger.trace({ connection: connection.name }, 'disconnecting connection inside manager');
|
66 | return;
|
67 | }
|
68 | const internalConnection = this.get(connection.name);
|
69 | |
70 |
|
71 |
|
72 |
|
73 | if (!internalConnection) {
|
74 | return;
|
75 | }
|
76 | this.emitter.emit('db:connection:disconnect', connection);
|
77 | this.logger.trace({ connection: connection.name }, 'disconnecting connection inside manager');
|
78 | delete internalConnection.connection;
|
79 | internalConnection.state = 'closed';
|
80 | }
|
81 | |
82 |
|
83 |
|
84 | handleConnect(connection) {
|
85 | const internalConnection = this.get(connection.name);
|
86 | if (!internalConnection) {
|
87 | return;
|
88 | }
|
89 | this.emitter.emit('db:connection:connect', connection);
|
90 | internalConnection.state = 'open';
|
91 | }
|
92 | |
93 |
|
94 |
|
95 | monitorConnection(connection) {
|
96 | connection.on('disconnect', ($connection) => this.handleDisconnect($connection));
|
97 | connection.on('connect', ($connection) => this.handleConnect($connection));
|
98 | connection.on('error', (error, $connection) => {
|
99 | this.emitter.emit('db:connection:error', [error, $connection]);
|
100 | });
|
101 | }
|
102 | |
103 |
|
104 |
|
105 |
|
106 | add(connectionName, config) {
|
107 | |
108 |
|
109 |
|
110 |
|
111 | if (this.has(connectionName)) {
|
112 | return;
|
113 | }
|
114 | this.logger.trace({ connection: connectionName }, 'adding new connection to the manager');
|
115 | this.connections.set(connectionName, {
|
116 | name: connectionName,
|
117 | config: config,
|
118 | state: 'registered',
|
119 | });
|
120 | }
|
121 | |
122 |
|
123 |
|
124 | connect(connectionName) {
|
125 | const connection = this.connections.get(connectionName);
|
126 | if (!connection) {
|
127 | throw new utils_1.Exception(`Cannot connect to unregistered connection ${connectionName}`, 500, 'E_UNMANAGED_DB_CONNECTION');
|
128 | }
|
129 | |
130 |
|
131 |
|
132 | if (this.isConnected(connection.name)) {
|
133 | return;
|
134 | }
|
135 | |
136 |
|
137 |
|
138 | connection.connection = new index_1.Connection(connection.name, connection.config, this.logger);
|
139 | this.monitorConnection(connection.connection);
|
140 | connection.connection.connect();
|
141 | }
|
142 | |
143 |
|
144 |
|
145 | patch(connectionName, config) {
|
146 | const connection = this.get(connectionName);
|
147 | |
148 |
|
149 |
|
150 | if (!connection) {
|
151 | return this.add(connectionName, config);
|
152 | }
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 | if (connection.connection) {
|
159 | this.orphanConnections.add(connection.connection);
|
160 | connection.connection.disconnect();
|
161 | }
|
162 | |
163 |
|
164 |
|
165 |
|
166 | connection.state = 'migrating';
|
167 | connection.config = config;
|
168 | |
169 |
|
170 |
|
171 |
|
172 | delete connection.connection;
|
173 | }
|
174 | |
175 |
|
176 |
|
177 | get(connectionName) {
|
178 | return this.connections.get(connectionName);
|
179 | }
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 | has(connectionName) {
|
186 | return this.connections.has(connectionName);
|
187 | }
|
188 | |
189 |
|
190 |
|
191 |
|
192 | isConnected(connectionName) {
|
193 | if (!this.has(connectionName)) {
|
194 | return false;
|
195 | }
|
196 | const connection = this.get(connectionName);
|
197 | return !!connection.connection && connection.state === 'open';
|
198 | }
|
199 | |
200 |
|
201 |
|
202 |
|
203 | async close(connectionName, release = false) {
|
204 | if (this.isConnected(connectionName)) {
|
205 | const connection = this.get(connectionName);
|
206 | await connection.connection.disconnect();
|
207 | connection.state = 'closing';
|
208 | }
|
209 | if (release) {
|
210 | await this.release(connectionName);
|
211 | }
|
212 | }
|
213 | |
214 |
|
215 |
|
216 | async closeAll(release = false) {
|
217 | await Promise.all(Array.from(this.connections.keys()).map((name) => this.close(name, release)));
|
218 | }
|
219 | |
220 |
|
221 |
|
222 |
|
223 | async release(connectionName) {
|
224 | if (this.isConnected(connectionName)) {
|
225 | await this.close(connectionName, true);
|
226 | }
|
227 | else {
|
228 | this.connections.delete(connectionName);
|
229 | }
|
230 | }
|
231 | |
232 |
|
233 |
|
234 | async report() {
|
235 | const reports = await Promise.all(Array.from(this.connections.keys())
|
236 | .filter((one) => this.get(one).config.healthCheck)
|
237 | .map((one) => {
|
238 | this.connect(one);
|
239 | return this.get(one).connection.getReport();
|
240 | }));
|
241 | const healthy = !reports.find((report) => !!report.error);
|
242 | return {
|
243 | displayName: 'Database',
|
244 | health: {
|
245 | healthy,
|
246 | message: healthy
|
247 | ? 'All connections are healthy'
|
248 | : 'One or more connections are not healthy',
|
249 | },
|
250 | meta: reports,
|
251 | };
|
252 | }
|
253 | }
|
254 | exports.ConnectionManager = ConnectionManager;
|