@contember/database 1.1.2 → 1.2.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/builders/DeleteBuilder.d.ts +1 -1
- package/dist/src/builders/DeleteBuilder.d.ts.map +1 -1
- package/dist/src/builders/DeleteBuilder.js.map +1 -1
- package/dist/src/builders/InsertBuilder.d.ts +1 -1
- package/dist/src/builders/InsertBuilder.d.ts.map +1 -1
- package/dist/src/builders/InsertBuilder.js.map +1 -1
- package/dist/src/builders/UpdateBuilder.d.ts +1 -1
- package/dist/src/builders/UpdateBuilder.d.ts.map +1 -1
- package/dist/src/builders/UpdateBuilder.js.map +1 -1
- package/dist/src/client/AcquiredConnection.d.ts +17 -0
- package/dist/src/client/AcquiredConnection.d.ts.map +1 -0
- package/dist/src/client/AcquiredConnection.js +78 -0
- package/dist/src/client/AcquiredConnection.js.map +1 -0
- package/dist/src/client/Client.d.ts +3 -1
- package/dist/src/client/Client.d.ts.map +1 -1
- package/dist/src/client/Client.js +9 -5
- package/dist/src/client/Client.js.map +1 -1
- package/dist/src/client/Connection.d.ts +26 -30
- package/dist/src/client/Connection.d.ts.map +1 -1
- package/dist/src/client/Connection.js +31 -38
- package/dist/src/client/Connection.js.map +1 -1
- package/dist/src/client/PgClient.d.ts +9 -0
- package/dist/src/client/PgClient.d.ts.map +1 -0
- package/dist/src/client/PgClient.js +3 -0
- package/dist/src/client/PgClient.js.map +1 -0
- package/dist/src/client/Pool.d.ts +126 -0
- package/dist/src/client/Pool.d.ts.map +1 -0
- package/dist/src/client/Pool.js +380 -0
- package/dist/src/client/Pool.js.map +1 -0
- package/dist/src/client/Transaction.d.ts +20 -11
- package/dist/src/client/Transaction.d.ts.map +1 -1
- package/dist/src/client/Transaction.js +80 -55
- package/dist/src/client/Transaction.js.map +1 -1
- package/dist/src/client/errors.d.ts +1 -1
- package/dist/src/client/errors.d.ts.map +1 -1
- package/dist/src/client/errors.js +7 -5
- package/dist/src/client/errors.js.map +1 -1
- package/dist/src/client/index.d.ts +1 -1
- package/dist/src/client/index.d.ts.map +1 -1
- package/dist/src/client/index.js +1 -1
- package/dist/src/client/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/types.d.ts +4 -1
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils/Mutex.d.ts +11 -0
- package/dist/src/utils/Mutex.d.ts.map +1 -0
- package/dist/src/utils/Mutex.js +49 -0
- package/dist/src/utils/Mutex.js.map +1 -0
- package/dist/src/utils/asyncIterableTransaction.d.ts +7 -0
- package/dist/src/utils/asyncIterableTransaction.d.ts.map +1 -0
- package/dist/src/utils/asyncIterableTransaction.js +32 -0
- package/dist/src/utils/asyncIterableTransaction.js.map +1 -0
- package/dist/src/utils/createDatabaseIfNotExists.d.ts +2 -2
- package/dist/src/utils/createDatabaseIfNotExists.d.ts.map +1 -1
- package/dist/src/utils/createDatabaseIfNotExists.js +2 -2
- package/dist/src/utils/createDatabaseIfNotExists.js.map +1 -1
- package/dist/src/utils/index.d.ts +3 -0
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +3 -0
- package/dist/src/utils/index.js.map +1 -1
- package/dist/src/utils/pgClientFactory.d.ts +6 -0
- package/dist/src/utils/pgClientFactory.d.ts.map +1 -0
- package/dist/src/utils/pgClientFactory.js +12 -0
- package/dist/src/utils/pgClientFactory.js.map +1 -0
- package/dist/src/utils/sql.d.ts +1 -1
- package/dist/src/utils/sql.d.ts.map +1 -1
- package/dist/src/utils/withDatabaseAdvisoryLock.d.ts +1 -1
- package/dist/src/utils/withDatabaseAdvisoryLock.d.ts.map +1 -1
- package/dist/src/utils/withDatabaseAdvisoryLock.js +1 -1
- package/dist/src/utils/withDatabaseAdvisoryLock.js.map +1 -1
- package/dist/tests/cases/unit/connection.test.d.ts +2 -0
- package/dist/tests/cases/unit/connection.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/connection.test.js +128 -0
- package/dist/tests/cases/unit/connection.test.js.map +1 -0
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts +6 -0
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts.map +1 -0
- package/dist/tests/cases/unit/createConnectionMockAlt.js +47 -0
- package/dist/tests/cases/unit/createConnectionMockAlt.js.map +1 -0
- package/dist/tests/cases/unit/eventManager.test.js +91 -17
- package/dist/tests/cases/unit/eventManager.test.js.map +1 -1
- package/dist/tests/cases/unit/pool.test.d.ts +2 -0
- package/dist/tests/cases/unit/pool.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/pool.test.js +323 -0
- package/dist/tests/cases/unit/pool.test.js.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/builders/DeleteBuilder.ts +1 -1
- package/src/builders/InsertBuilder.ts +1 -1
- package/src/builders/UpdateBuilder.ts +1 -1
- package/src/client/AcquiredConnection.ts +104 -0
- package/src/client/Client.ts +34 -18
- package/src/client/Connection.ts +57 -63
- package/src/client/PgClient.ts +13 -0
- package/src/client/Pool.ts +465 -0
- package/src/client/Transaction.ts +101 -69
- package/src/client/errors.ts +10 -6
- package/src/client/index.ts +1 -1
- package/src/index.ts +2 -0
- package/src/types.ts +4 -1
- package/src/utils/Mutex.ts +49 -0
- package/src/utils/asyncIterableTransaction.ts +33 -0
- package/src/utils/createDatabaseIfNotExists.ts +4 -4
- package/src/utils/index.ts +3 -0
- package/src/utils/pgClientFactory.ts +11 -0
- package/src/utils/withDatabaseAdvisoryLock.ts +4 -4
- package/tests/cases/unit/connection.test.ts +154 -0
- package/tests/cases/unit/createConnectionMockAlt.ts +44 -0
- package/tests/cases/unit/eventManager.test.ts +101 -19
- package/tests/cases/unit/pool.test.ts +349 -0
- package/dist/src/client/SingleConnection.d.ts +0 -20
- package/dist/src/client/SingleConnection.d.ts.map +0 -1
- package/dist/src/client/SingleConnection.js +0 -67
- package/dist/src/client/SingleConnection.js.map +0 -1
- package/dist/src/client/execution.d.ts +0 -8
- package/dist/src/client/execution.d.ts.map +0 -1
- package/dist/src/client/execution.js +0 -74
- package/dist/src/client/execution.js.map +0 -1
- package/src/client/SingleConnection.ts +0 -82
- package/src/client/execution.ts +0 -93
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { expect, it, beforeAll } from 'vitest'
|
|
2
|
+
import { ClientErrorCodes, Pool, PoolLogger } from '../../../src'
|
|
3
|
+
import { Client as PgClient } from 'pg'
|
|
4
|
+
import EventEmitter from 'events'
|
|
5
|
+
|
|
6
|
+
const createPoolLogger = () => {
|
|
7
|
+
const logger: PoolLogger & {messages: string; clear: () => void} = (message, pool) => {
|
|
8
|
+
logger.messages += `${pool.connecting}${pool.active}${pool.idle} ${pool.pending}: ${message}\n`
|
|
9
|
+
}
|
|
10
|
+
logger.messages = '\nCAI P\n'
|
|
11
|
+
logger.clear = () => logger.messages = 'CAI P\n'
|
|
12
|
+
return logger
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const timeout = async (ms = 1) => await new Promise(resolve => setTimeout(resolve, ms))
|
|
16
|
+
|
|
17
|
+
beforeAll(() => {
|
|
18
|
+
expect.addSnapshotSerializer({
|
|
19
|
+
test: val => typeof val === 'string',
|
|
20
|
+
print: val => String(val),
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('acquires and releases new connection from pool', async () => {
|
|
25
|
+
const logger = createPoolLogger()
|
|
26
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
27
|
+
log: logger,
|
|
28
|
+
})
|
|
29
|
+
const connection = await pool.acquire()
|
|
30
|
+
await pool.release(connection)
|
|
31
|
+
await timeout()
|
|
32
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
33
|
+
CAI P
|
|
34
|
+
000 1: Item added to a queue.
|
|
35
|
+
000 1: Creating a new connection
|
|
36
|
+
100 1: Connection established
|
|
37
|
+
010 0: Queued item fulfilled with new connection.
|
|
38
|
+
010 0: Not connecting, queue is empty.
|
|
39
|
+
010 0: Releasing a connection.
|
|
40
|
+
001 0: Connection is idle and available.
|
|
41
|
+
`)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
it('disposes connection when maxIdle is reached', async () => {
|
|
46
|
+
const logger = createPoolLogger()
|
|
47
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
48
|
+
log: logger,
|
|
49
|
+
maxIdle: 0,
|
|
50
|
+
})
|
|
51
|
+
const connection = await pool.acquire()
|
|
52
|
+
await pool.release(connection)
|
|
53
|
+
await timeout()
|
|
54
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
55
|
+
CAI P
|
|
56
|
+
000 1: Item added to a queue.
|
|
57
|
+
000 1: Creating a new connection
|
|
58
|
+
100 1: Connection established
|
|
59
|
+
010 0: Queued item fulfilled with new connection.
|
|
60
|
+
010 0: Not connecting, queue is empty.
|
|
61
|
+
010 0: Releasing a connection.
|
|
62
|
+
000 0: Too many idle connections, connection disposed.
|
|
63
|
+
`)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('disposes connection after max uses', async () => {
|
|
67
|
+
const logger = createPoolLogger()
|
|
68
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
69
|
+
log: logger,
|
|
70
|
+
maxUses: 1,
|
|
71
|
+
})
|
|
72
|
+
const connection = await pool.acquire()
|
|
73
|
+
await pool.release(connection)
|
|
74
|
+
await timeout()
|
|
75
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
76
|
+
CAI P
|
|
77
|
+
000 1: Item added to a queue.
|
|
78
|
+
000 1: Creating a new connection
|
|
79
|
+
100 1: Connection established
|
|
80
|
+
010 0: Queued item fulfilled with new connection.
|
|
81
|
+
010 0: Not connecting, queue is empty.
|
|
82
|
+
010 0: Releasing a connection.
|
|
83
|
+
000 0: Not connecting, queue is empty.
|
|
84
|
+
000 0: Connection has reached max age or usage and was disposed.
|
|
85
|
+
`)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('disposes errored connection', async () => {
|
|
89
|
+
const logger = createPoolLogger()
|
|
90
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
91
|
+
log: logger,
|
|
92
|
+
maxIdle: 0,
|
|
93
|
+
})
|
|
94
|
+
const connection = await pool.acquire()
|
|
95
|
+
await pool.dispose(connection)
|
|
96
|
+
await timeout()
|
|
97
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
98
|
+
CAI P
|
|
99
|
+
000 1: Item added to a queue.
|
|
100
|
+
000 1: Creating a new connection
|
|
101
|
+
100 1: Connection established
|
|
102
|
+
010 0: Queued item fulfilled with new connection.
|
|
103
|
+
010 0: Not connecting, queue is empty.
|
|
104
|
+
010 0: Releasing and disposing a connection.
|
|
105
|
+
000 0: Not connecting, queue is empty.
|
|
106
|
+
000 0: Connection errored and was disposed.
|
|
107
|
+
`)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
it('time outs when waiting for a connection', async () => {
|
|
112
|
+
const logger = createPoolLogger()
|
|
113
|
+
const clientMock = new PgClientMock()
|
|
114
|
+
clientMock.connections.push(createSuccessfulPromise(5))
|
|
115
|
+
const pool = new Pool(() => clientMock as unknown as PgClient, {
|
|
116
|
+
log: logger,
|
|
117
|
+
acquireTimeoutMs: 2,
|
|
118
|
+
})
|
|
119
|
+
await expect(pool.acquire.bind(pool)).rejects.toThrowError('Failed to acquire a connection')
|
|
120
|
+
await timeout(4)
|
|
121
|
+
|
|
122
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
123
|
+
CAI P
|
|
124
|
+
000 1: Item added to a queue.
|
|
125
|
+
000 1: Creating a new connection
|
|
126
|
+
100 0: Queued item timed out
|
|
127
|
+
100 0: Connection established
|
|
128
|
+
001 0: Connection is idle and available.
|
|
129
|
+
001 0: Not connecting, queue is empty.
|
|
130
|
+
`)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
it('acquires idle connection from pool', async () => {
|
|
135
|
+
const logger = createPoolLogger()
|
|
136
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
137
|
+
log: logger,
|
|
138
|
+
})
|
|
139
|
+
const connection = await pool.acquire()
|
|
140
|
+
await pool.release(connection)
|
|
141
|
+
logger.clear()
|
|
142
|
+
const connection2 = await pool.acquire()
|
|
143
|
+
await pool.release(connection2)
|
|
144
|
+
await timeout()
|
|
145
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
146
|
+
CAI P
|
|
147
|
+
010 0: Item fulfilled with an idle connection.
|
|
148
|
+
010 0: Releasing a connection.
|
|
149
|
+
001 0: Connection is idle and available.
|
|
150
|
+
`)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('acquires multiple new connections from pool', async () => {
|
|
154
|
+
const logger = createPoolLogger()
|
|
155
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
156
|
+
log: logger,
|
|
157
|
+
})
|
|
158
|
+
const conn1 = await pool.acquire()
|
|
159
|
+
const conn2 = await pool.acquire()
|
|
160
|
+
await pool.release(conn1)
|
|
161
|
+
await pool.release(conn2)
|
|
162
|
+
await timeout()
|
|
163
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
164
|
+
CAI P
|
|
165
|
+
000 1: Item added to a queue.
|
|
166
|
+
000 1: Creating a new connection
|
|
167
|
+
100 1: Connection established
|
|
168
|
+
010 0: Queued item fulfilled with new connection.
|
|
169
|
+
010 0: Not connecting, queue is empty.
|
|
170
|
+
010 1: Item added to a queue.
|
|
171
|
+
010 1: Creating a new connection
|
|
172
|
+
110 1: Connection established
|
|
173
|
+
020 0: Queued item fulfilled with new connection.
|
|
174
|
+
020 0: Not connecting, queue is empty.
|
|
175
|
+
020 0: Releasing a connection.
|
|
176
|
+
011 0: Connection is idle and available.
|
|
177
|
+
011 0: Releasing a connection.
|
|
178
|
+
002 0: Connection is idle and available.
|
|
179
|
+
`)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
it('releases idle connection', async () => {
|
|
184
|
+
const logger = createPoolLogger()
|
|
185
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
186
|
+
log: logger,
|
|
187
|
+
idleTimeoutMs: 2,
|
|
188
|
+
})
|
|
189
|
+
const conn1 = await pool.acquire()
|
|
190
|
+
await pool.release(conn1)
|
|
191
|
+
await timeout(4)
|
|
192
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
193
|
+
CAI P
|
|
194
|
+
000 1: Item added to a queue.
|
|
195
|
+
000 1: Creating a new connection
|
|
196
|
+
100 1: Connection established
|
|
197
|
+
010 0: Queued item fulfilled with new connection.
|
|
198
|
+
010 0: Not connecting, queue is empty.
|
|
199
|
+
010 0: Releasing a connection.
|
|
200
|
+
001 0: Connection is idle and available.
|
|
201
|
+
000 0: Idle connection disposed after timeout.
|
|
202
|
+
`)
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
it('waits for idle connection when pool is full', async () => {
|
|
207
|
+
const logger = createPoolLogger()
|
|
208
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
209
|
+
maxConnections: 1,
|
|
210
|
+
log: logger,
|
|
211
|
+
})
|
|
212
|
+
const conn1 = await pool.acquire()
|
|
213
|
+
setTimeout(async () => {
|
|
214
|
+
await pool.release(conn1)
|
|
215
|
+
}, 20)
|
|
216
|
+
const conn2 = await pool.acquire()
|
|
217
|
+
await pool.release(conn2)
|
|
218
|
+
await timeout()
|
|
219
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
220
|
+
CAI P
|
|
221
|
+
000 1: Item added to a queue.
|
|
222
|
+
000 1: Creating a new connection
|
|
223
|
+
100 1: Connection established
|
|
224
|
+
010 0: Queued item fulfilled with new connection.
|
|
225
|
+
010 0: Not connecting, queue is empty.
|
|
226
|
+
010 1: Item added to a queue.
|
|
227
|
+
010 1: Not connecting, max connections reached.
|
|
228
|
+
010 1: Releasing a connection.
|
|
229
|
+
010 0: Queued item fulfilled with released connection.
|
|
230
|
+
010 0: Releasing a connection.
|
|
231
|
+
001 0: Connection is idle and available.
|
|
232
|
+
`)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
it('tries to reconnect on recoverable error', async () => {
|
|
237
|
+
const logger = createPoolLogger()
|
|
238
|
+
const pgClientMock = new PgClientMock()
|
|
239
|
+
pgClientMock.on('error', () => {
|
|
240
|
+
|
|
241
|
+
})
|
|
242
|
+
const pool = new Pool(() => pgClientMock as unknown as PgClient, {
|
|
243
|
+
log: logger,
|
|
244
|
+
})
|
|
245
|
+
pgClientMock.connections.push(createRecoverableErrorPromise(), createRecoverableErrorPromise())
|
|
246
|
+
await pool.acquire()
|
|
247
|
+
await timeout()
|
|
248
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
249
|
+
CAI P
|
|
250
|
+
000 1: Item added to a queue.
|
|
251
|
+
000 1: Creating a new connection
|
|
252
|
+
100 1: Connection error occurred: too many connection
|
|
253
|
+
100 1: Recoverable error, retrying in a moment.
|
|
254
|
+
000 1: Retrying
|
|
255
|
+
000 1: Creating a new connection
|
|
256
|
+
100 1: Connection error occurred: too many connection
|
|
257
|
+
100 1: Recoverable error, retrying in a moment.
|
|
258
|
+
000 1: Retrying
|
|
259
|
+
000 1: Creating a new connection
|
|
260
|
+
100 1: Connection established
|
|
261
|
+
010 0: Queued item fulfilled with new connection.
|
|
262
|
+
010 0: Not connecting, queue is empty.
|
|
263
|
+
`)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
it('fails to reconnect on recoverable error', async () => {
|
|
268
|
+
const logger = createPoolLogger()
|
|
269
|
+
const pgClientMock = new PgClientMock()
|
|
270
|
+
pgClientMock.on('error', () => {
|
|
271
|
+
|
|
272
|
+
})
|
|
273
|
+
const pool = new Pool(() => pgClientMock as unknown as PgClient, {
|
|
274
|
+
log: logger,
|
|
275
|
+
reconnectIntervalMs: 5,
|
|
276
|
+
acquireTimeoutMs: 10,
|
|
277
|
+
})
|
|
278
|
+
pool.on('error', () => {
|
|
279
|
+
})
|
|
280
|
+
pgClientMock.connections.push(createRecoverableErrorPromise(), createRecoverableErrorPromise())
|
|
281
|
+
await expect(async () => await pool.acquire()).rejects.toThrowError('Failed to acquire a connection. Last error: too many connection')
|
|
282
|
+
await timeout(2)
|
|
283
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
284
|
+
CAI P
|
|
285
|
+
000 1: Item added to a queue.
|
|
286
|
+
000 1: Creating a new connection
|
|
287
|
+
100 1: Connection error occurred: too many connection
|
|
288
|
+
100 1: Recoverable error, retrying in a moment.
|
|
289
|
+
000 1: Retrying
|
|
290
|
+
000 1: Creating a new connection
|
|
291
|
+
100 1: Connection error occurred: too many connection
|
|
292
|
+
100 1: Recoverable error, max retries reached.
|
|
293
|
+
000 0: Queued item timed out
|
|
294
|
+
`)
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
it('fails to reconnect on unrecoverable error', async () => {
|
|
298
|
+
const logger = createPoolLogger()
|
|
299
|
+
const pgClientMock = new PgClientMock()
|
|
300
|
+
pgClientMock.on('error', () => {
|
|
301
|
+
})
|
|
302
|
+
const pool = new Pool(() => pgClientMock as unknown as PgClient, {
|
|
303
|
+
log: logger,
|
|
304
|
+
})
|
|
305
|
+
pool.on('error', () => {
|
|
306
|
+
|
|
307
|
+
})
|
|
308
|
+
pgClientMock.connections.push(createErrorPromise())
|
|
309
|
+
await expect(async () => await pool.acquire()).rejects.toThrowError('Database client error: my err')
|
|
310
|
+
await timeout()
|
|
311
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
312
|
+
CAI P
|
|
313
|
+
000 1: Item added to a queue.
|
|
314
|
+
000 1: Creating a new connection
|
|
315
|
+
100 1: Connection error occurred: my err
|
|
316
|
+
000 0: Connecting failed, rejecting pending item
|
|
317
|
+
`)
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
const createSuccessfulPromise = (timeout = 1) => () => new Promise<void>(resolve => {
|
|
323
|
+
setTimeout(resolve, timeout)
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
const createRecoverableErrorPromise = () => () => new Promise<void>((resolve, reject) => {
|
|
327
|
+
setTimeout(() => {
|
|
328
|
+
const err: Error & {code?: string} = new Error('too many connection')
|
|
329
|
+
err.code = ClientErrorCodes.TOO_MANY_CONNECTIONS
|
|
330
|
+
reject(err)
|
|
331
|
+
}, 1)
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
const createErrorPromise = () => () => new Promise<void>((resolve, reject) => {
|
|
335
|
+
setTimeout(() => reject(new Error('my err')), 1)
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
const PgClientMock = class extends EventEmitter {
|
|
339
|
+
public connections: (() => Promise<void>)[] = []
|
|
340
|
+
|
|
341
|
+
async connect() {
|
|
342
|
+
const connection = this.connections.shift()
|
|
343
|
+
return connection ? connection() : createSuccessfulPromise()
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async end() {
|
|
347
|
+
return createSuccessfulPromise()
|
|
348
|
+
}
|
|
349
|
+
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { ClientConfig } from 'pg';
|
|
2
|
-
import { EventManager } from './EventManager';
|
|
3
|
-
import { Client } from './Client';
|
|
4
|
-
import { Connection } from './Connection';
|
|
5
|
-
import { DatabaseCredentials } from '../types';
|
|
6
|
-
export declare class SingleConnection implements Connection.ConnectionLike, Connection.ClientFactory {
|
|
7
|
-
private readonly config;
|
|
8
|
-
private readonly queryConfig;
|
|
9
|
-
readonly eventManager: EventManager;
|
|
10
|
-
private readonly pgClient;
|
|
11
|
-
private isConnected;
|
|
12
|
-
constructor(config: ClientConfig & DatabaseCredentials, queryConfig: Connection.QueryConfig, eventManager?: EventManager);
|
|
13
|
-
createClient(schema: string, queryMeta: Record<string, any>): Client;
|
|
14
|
-
transaction<Result>(callback: (connection: Connection.TransactionLike) => Promise<Result> | Result, options?: {
|
|
15
|
-
eventManager?: EventManager;
|
|
16
|
-
}): Promise<Result>;
|
|
17
|
-
end(): Promise<void>;
|
|
18
|
-
query<Row extends Record<string, any>>(sql: string, parameters?: any[], meta?: Record<string, any>, config?: Connection.QueryConfig): Promise<Connection.Result<Row>>;
|
|
19
|
-
}
|
|
20
|
-
//# sourceMappingURL=SingleConnection.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SingleConnection.d.ts","sourceRoot":"","sources":["../../../src/client/SingleConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,YAAY,EAAE,MAAM,IAAI,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAGjC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAA;AAE9C,qBAAa,gBAAiB,YAAW,UAAU,CAAC,cAAc,EAAE,UAAU,CAAC,aAAa;IAK1F,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,WAAW;aACZ,YAAY,EAAE,YAAY;IAN3C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAU;IACnC,OAAO,CAAC,WAAW,CAAQ;gBAGT,MAAM,EAAE,YAAY,GAAG,mBAAmB,EAC1C,WAAW,EAAE,UAAU,CAAC,WAAW,EACpC,YAAY,GAAE,YAAqC;IAU7D,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM;IAIrE,WAAW,CAAC,MAAM,EACvB,QAAQ,EAAE,CAAC,UAAU,EAAE,UAAU,CAAC,eAAe,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,EAC9E,OAAO,GAAE;QAAE,YAAY,CAAC,EAAE,YAAY,CAAA;KAAO,GAC3C,OAAO,CAAC,MAAM,CAAC;IA2BZ,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAKpB,KAAK,CAAC,GAAG,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1C,GAAG,EAAE,MAAM,EACX,UAAU,GAAE,GAAG,EAAO,EACtB,IAAI,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,EAC9B,MAAM,GAAE,UAAU,CAAC,WAAgB,GACjC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;CAYlC"}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SingleConnection = void 0;
|
|
4
|
-
const pg_1 = require("pg");
|
|
5
|
-
const EventManager_1 = require("./EventManager");
|
|
6
|
-
const Client_1 = require("./Client");
|
|
7
|
-
const Transaction_1 = require("./Transaction");
|
|
8
|
-
const execution_1 = require("./execution");
|
|
9
|
-
class SingleConnection {
|
|
10
|
-
constructor(config, queryConfig, eventManager = new EventManager_1.EventManager(null)) {
|
|
11
|
-
this.config = config;
|
|
12
|
-
this.queryConfig = queryConfig;
|
|
13
|
-
this.eventManager = eventManager;
|
|
14
|
-
this.isConnected = false;
|
|
15
|
-
this.pgClient = new pg_1.Client(config);
|
|
16
|
-
this.pgClient.on('error', err => {
|
|
17
|
-
// eslint-disable-next-line no-console
|
|
18
|
-
console.error(err);
|
|
19
|
-
this.eventManager.fire(EventManager_1.EventManager.Event.clientError, err);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
createClient(schema, queryMeta) {
|
|
23
|
-
return new Client_1.Client(this, schema, queryMeta, new EventManager_1.EventManager(this.eventManager));
|
|
24
|
-
}
|
|
25
|
-
async transaction(callback, options = {}) {
|
|
26
|
-
var _a;
|
|
27
|
-
if (!this.isConnected) {
|
|
28
|
-
await this.pgClient.connect();
|
|
29
|
-
this.isConnected = true;
|
|
30
|
-
}
|
|
31
|
-
const eventManager = new EventManager_1.EventManager((_a = options.eventManager) !== null && _a !== void 0 ? _a : this.eventManager);
|
|
32
|
-
await (0, execution_1.executeQuery)(this.pgClient, eventManager, {
|
|
33
|
-
sql: 'BEGIN',
|
|
34
|
-
...this.queryConfig,
|
|
35
|
-
});
|
|
36
|
-
const transaction = new Transaction_1.Transaction(this.pgClient, eventManager, this.queryConfig);
|
|
37
|
-
try {
|
|
38
|
-
const result = await callback(transaction);
|
|
39
|
-
await transaction.commitUnclosed();
|
|
40
|
-
return result;
|
|
41
|
-
}
|
|
42
|
-
catch (e) {
|
|
43
|
-
await transaction.rollbackUnclosed();
|
|
44
|
-
throw e;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async end() {
|
|
48
|
-
this.isConnected = false;
|
|
49
|
-
await this.pgClient.end();
|
|
50
|
-
}
|
|
51
|
-
async query(sql, parameters = [], meta = {}, config = {}) {
|
|
52
|
-
var _a;
|
|
53
|
-
if (!this.isConnected) {
|
|
54
|
-
await this.pgClient.connect();
|
|
55
|
-
this.isConnected = true;
|
|
56
|
-
}
|
|
57
|
-
const query = { sql, parameters, meta, ...this.queryConfig, ...config };
|
|
58
|
-
try {
|
|
59
|
-
return await (0, execution_1.executeQuery)(this.pgClient, (_a = config.eventManager) !== null && _a !== void 0 ? _a : this.eventManager, query, {});
|
|
60
|
-
}
|
|
61
|
-
catch (e) {
|
|
62
|
-
throw e;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
exports.SingleConnection = SingleConnection;
|
|
67
|
-
//# sourceMappingURL=SingleConnection.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SingleConnection.js","sour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Base } from 'pg';
|
|
2
|
-
import { EventManager } from './EventManager';
|
|
3
|
-
import { Connection } from './Connection';
|
|
4
|
-
declare type SomeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
5
|
-
export declare function executeQuery<Row extends Record<string, any>>(pgClient: ClientBase, eventManager: EventManager, { sql, parameters, meta, timing }: SomeOptional<Connection.Query, 'parameters' | 'meta'> & Connection.QueryConfig, context?: Connection.QueryContext): Promise<Connection.Result<Row>>;
|
|
6
|
-
export declare const executeClientOperation: <T>(cb: () => Promise<T>) => Promise<T>;
|
|
7
|
-
export {};
|
|
8
|
-
//# sourceMappingURL=execution.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"execution.d.ts","sourceRoot":"","sources":["../../../src/client/execution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAC/B,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAiBzC,aAAK,YAAY,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;AAE1E,wBAAsB,YAAY,CAAC,GAAG,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACjE,QAAQ,EAAE,UAAU,EACpB,YAAY,EAAE,YAAY,EAC1B,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,YAAY,CAAC,UAAU,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAAC,GAAG,UAAU,CAAC,WAAW,EACjH,OAAO,GAAE,UAAU,CAAC,YAAiB,GACnC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CA0DjC;AAED,eAAO,MAAM,sBAAsB,yCAMlC,CAAA"}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.executeClientOperation = exports.executeQuery = void 0;
|
|
4
|
-
const EventManager_1 = require("./EventManager");
|
|
5
|
-
const errors_1 = require("./errors");
|
|
6
|
-
const errorCodes_1 = require("./errorCodes");
|
|
7
|
-
function prepareSql(sql) {
|
|
8
|
-
let parameterIndex = 0;
|
|
9
|
-
return sql.replace(/(\\*)\?/g, ({}, numOfEscapes) => (numOfEscapes.length % 2 ? '?' : `$${++parameterIndex}`));
|
|
10
|
-
}
|
|
11
|
-
async function executeQuery(pgClient, eventManager, { sql, parameters, meta, timing }, context = {}) {
|
|
12
|
-
parameters !== null && parameters !== void 0 ? parameters : (parameters = []);
|
|
13
|
-
meta !== null && meta !== void 0 ? meta : (meta = {});
|
|
14
|
-
try {
|
|
15
|
-
eventManager.fire(EventManager_1.EventManager.Event.queryStart, { sql, parameters, meta });
|
|
16
|
-
const exec = async () => await pgClient.query(prepareSql(sql), parameters);
|
|
17
|
-
let result;
|
|
18
|
-
if (timing) {
|
|
19
|
-
const startHrTime = process.hrtime();
|
|
20
|
-
const startTimeUs = startHrTime[0] * 1e6 + Math.floor(startHrTime[1] / 1000);
|
|
21
|
-
result = await exec();
|
|
22
|
-
const endHrTime = process.hrtime();
|
|
23
|
-
const endTimeUs = endHrTime[0] * 1e6 + Math.floor(endHrTime[1] / 1000);
|
|
24
|
-
const realStart = context.previousQueryEnd && context.previousQueryEnd > startTimeUs ? context.previousQueryEnd : startTimeUs;
|
|
25
|
-
result = {
|
|
26
|
-
...result,
|
|
27
|
-
timing: {
|
|
28
|
-
selfDuration: endTimeUs - realStart,
|
|
29
|
-
totalDuration: endTimeUs - startTimeUs,
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
context.previousQueryEnd = endTimeUs;
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
result = await exec();
|
|
36
|
-
}
|
|
37
|
-
eventManager.fire(EventManager_1.EventManager.Event.queryEnd, { sql, parameters, meta }, result);
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
if (!(error instanceof Error)) {
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
44
|
-
eventManager.fire(EventManager_1.EventManager.Event.queryError, { sql, parameters, meta }, error);
|
|
45
|
-
switch (error.code) {
|
|
46
|
-
case errorCodes_1.ClientErrorCodes.NOT_NULL_VIOLATION:
|
|
47
|
-
throw new errors_1.NotNullViolationError(sql, parameters, error);
|
|
48
|
-
case errorCodes_1.ClientErrorCodes.FOREIGN_KEY_VIOLATION:
|
|
49
|
-
throw new errors_1.ForeignKeyViolationError(sql, parameters, error);
|
|
50
|
-
case errorCodes_1.ClientErrorCodes.UNIQUE_VIOLATION:
|
|
51
|
-
throw new errors_1.UniqueViolationError(sql, parameters, error);
|
|
52
|
-
case errorCodes_1.ClientErrorCodes.T_R_SERIALIZATION_FAILURE:
|
|
53
|
-
throw new errors_1.SerializationFailureError(sql, parameters, error);
|
|
54
|
-
case errorCodes_1.ClientErrorCodes.INVALID_TEXT_REPRESENTATION:
|
|
55
|
-
case errorCodes_1.ClientErrorCodes.DATETIME_FIELD_OVERFLOW:
|
|
56
|
-
throw new errors_1.InvalidDataError(sql, parameters, error);
|
|
57
|
-
case errorCodes_1.ClientErrorCodes.IN_FAILED_SQL_TRANSACTION:
|
|
58
|
-
throw new errors_1.TransactionAbortedError(sql, parameters, error);
|
|
59
|
-
default:
|
|
60
|
-
throw new errors_1.QueryError(sql, parameters, error);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
exports.executeQuery = executeQuery;
|
|
65
|
-
const executeClientOperation = async (cb) => {
|
|
66
|
-
try {
|
|
67
|
-
return await cb();
|
|
68
|
-
}
|
|
69
|
-
catch (e) {
|
|
70
|
-
throw new errors_1.ClientError(e);
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
exports.executeClientOperation = executeClientOperation;
|
|
74
|
-
//# sourceMappingURL=execution.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"execution.js","sourceRoot":"","sources":["../../../src/client/execution.ts"],"names":[],"mappings":";;;AACA,iDAA6C;AAE7C,qCASiB;AACjB,6CAA+C;AAE/C,SAAS,UAAU,CAAC,GAAW;IAC9B,IAAI,cAAc,GAAG,CAAC,CAAA;IACtB,OAAO,GAAG,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,EAAE,EAAE,YAAY,EAAE,EAAE,CAAC,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,cAAc,EAAE,CAAC,CAAC,CAAA;AAC/G,CAAC;AAGM,KAAK,UAAU,YAAY,CACjC,QAAoB,EACpB,YAA0B,EAC1B,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAkF,EACjH,UAAmC,EAAE;IAErC,UAAU,aAAV,UAAU,cAAV,UAAU,IAAV,UAAU,GAAK,EAAE,EAAA;IACjB,IAAI,aAAJ,IAAI,cAAJ,IAAI,IAAJ,IAAI,GAAK,EAAE,EAAA;IACX,IAAI;QACH,YAAY,CAAC,IAAI,CAAC,2BAAY,CAAC,KAAK,CAAC,UAAU,EAAE,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAA;QAE3E,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC,MAAM,QAAQ,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,CAAA;QAE1E,IAAI,MAA8B,CAAA;QAClC,IAAI,MAAM,EAAE;YACX,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,EAAE,CAAA;YACpC,MAAM,WAAW,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,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U,EAAE,KAAK,CAAC,CAAA;YACxD,KAAK,6BAAgB,CAAC,qBAAqB;gBAC1C,MAAM,IAAI,iCAAwB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,CAAA;YAC3D,KAAK,6BAAgB,CAAC,gBAAgB;gBACrC,MAAM,IAAI,6BAAoB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,CAAA;YACvD,KAAK,6BAAgB,CAAC,yBAAyB;gBAC9C,MAAM,IAAI,kCAAyB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,CAAA;YAC5D,KAAK,6BAAgB,CAAC,2BAA2B,CAAC;YAClD,KAAK,6BAAgB,CAAC,uBAAuB;gBAC5C,MAAM,IAAI,yBAAgB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,CAAA;YACnD,KAAK,6BAAgB,CAAC,yBAAyB;gBAC9C,MAAM,IAAI,gCAAuB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,CAAA;YAC1D;gBACC,MAAM,IAAI,mBAAU,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,CAAA;SAC7C;KACD;AACF,CAAC;AA/DD,oCA+DC;AAEM,MAAM,sBAAsB,GAAG,KAAK,EAAK,EAAoB,EAAc,EAAE;IACnF,IAAI;QACH,OAAO,MAAM,EAAE,EAAE,CAAA;KACjB;IAAC,OAAO,CAAC,EAAE;QACX,MAAM,IAAI,oBAAW,CAAC,CAAC,CAAC,CAAA;KACxB;AACF,CAAC,CAAA;AANY,QAAA,sBAAsB,0BAMlC"}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { Client as PgClient, ClientConfig } from 'pg'
|
|
2
|
-
import { EventManager } from './EventManager'
|
|
3
|
-
import { Client } from './Client'
|
|
4
|
-
import { Transaction } from './Transaction'
|
|
5
|
-
import { executeQuery } from './execution'
|
|
6
|
-
import { Connection } from './Connection'
|
|
7
|
-
import { DatabaseCredentials } from '../types'
|
|
8
|
-
|
|
9
|
-
export class SingleConnection implements Connection.ConnectionLike, Connection.ClientFactory {
|
|
10
|
-
private readonly pgClient: PgClient
|
|
11
|
-
private isConnected = false
|
|
12
|
-
|
|
13
|
-
constructor(
|
|
14
|
-
private readonly config: ClientConfig & DatabaseCredentials,
|
|
15
|
-
private readonly queryConfig: Connection.QueryConfig,
|
|
16
|
-
public readonly eventManager: EventManager = new EventManager(null),
|
|
17
|
-
) {
|
|
18
|
-
this.pgClient = new PgClient(config)
|
|
19
|
-
this.pgClient.on('error', err => {
|
|
20
|
-
// eslint-disable-next-line no-console
|
|
21
|
-
console.error(err)
|
|
22
|
-
this.eventManager.fire(EventManager.Event.clientError, err)
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public createClient(schema: string, queryMeta: Record<string, any>): Client {
|
|
27
|
-
return new Client(this, schema, queryMeta, new EventManager(this.eventManager))
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async transaction<Result>(
|
|
31
|
-
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
32
|
-
options: { eventManager?: EventManager } = {},
|
|
33
|
-
): Promise<Result> {
|
|
34
|
-
if (!this.isConnected) {
|
|
35
|
-
await this.pgClient.connect()
|
|
36
|
-
this.isConnected = true
|
|
37
|
-
}
|
|
38
|
-
const eventManager = new EventManager(options.eventManager ?? this.eventManager)
|
|
39
|
-
await executeQuery(
|
|
40
|
-
this.pgClient,
|
|
41
|
-
eventManager,
|
|
42
|
-
{
|
|
43
|
-
sql: 'BEGIN',
|
|
44
|
-
...this.queryConfig,
|
|
45
|
-
},
|
|
46
|
-
)
|
|
47
|
-
const transaction = new Transaction(this.pgClient, eventManager, this.queryConfig)
|
|
48
|
-
try {
|
|
49
|
-
const result = await callback(transaction)
|
|
50
|
-
|
|
51
|
-
await transaction.commitUnclosed()
|
|
52
|
-
|
|
53
|
-
return result
|
|
54
|
-
} catch (e) {
|
|
55
|
-
await transaction.rollbackUnclosed()
|
|
56
|
-
throw e
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async end(): Promise<void> {
|
|
61
|
-
this.isConnected = false
|
|
62
|
-
await this.pgClient.end()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async query<Row extends Record<string, any>>(
|
|
66
|
-
sql: string,
|
|
67
|
-
parameters: any[] = [],
|
|
68
|
-
meta: Record<string, any> = {},
|
|
69
|
-
config: Connection.QueryConfig = {},
|
|
70
|
-
): Promise<Connection.Result<Row>> {
|
|
71
|
-
if (!this.isConnected) {
|
|
72
|
-
await this.pgClient.connect()
|
|
73
|
-
this.isConnected = true
|
|
74
|
-
}
|
|
75
|
-
const query: Connection.Query = { sql, parameters, meta, ...this.queryConfig, ...config }
|
|
76
|
-
try {
|
|
77
|
-
return await executeQuery<Row>(this.pgClient, config.eventManager ?? this.eventManager, query, {})
|
|
78
|
-
} catch (e) {
|
|
79
|
-
throw e
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|