@contember/database 2.2.0-alpha.12 → 2.2.0-alpha.13
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/CLAUDE.md +1 -0
- package/dist/development/src/client/Connection.cjs +40 -4
- package/dist/development/src/client/Connection.cjs.map +1 -1
- package/dist/development/src/client/Connection.js +40 -4
- package/dist/development/src/client/Connection.js.map +1 -1
- package/dist/production/src/client/Connection.cjs +40 -4
- package/dist/production/src/client/Connection.cjs.map +1 -1
- package/dist/production/src/client/Connection.js +40 -4
- package/dist/production/src/client/Connection.js.map +1 -1
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts +6 -0
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts.map +1 -1
- package/dist/tests/cases/unit/staleConnectionRetry.test.d.ts +2 -0
- package/dist/tests/cases/unit/staleConnectionRetry.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/client/Connection.d.ts +11 -2
- package/dist/types/client/Connection.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/client/Connection.ts +57 -2
- package/tests/cases/unit/createConnectionMockAlt.ts +19 -2
- package/tests/cases/unit/staleConnectionRetry.test.ts +86 -0
|
@@ -3,7 +3,8 @@ import { PgClient } from '../../../src/client/PgClient.js'
|
|
|
3
3
|
import EventEmitter from 'node:events'
|
|
4
4
|
import { expect } from 'bun:test'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
// `error` rejects the query; with `lost`, the client first emits `error` as pg does when the server closes the connection.
|
|
7
|
+
type MockQuery = { sql: string; timeout?: number; result?: any; error?: Error; lost?: boolean }
|
|
7
8
|
|
|
8
9
|
export const createConnectionMockAlt = (...queries: MockQuery[][]): [Connection, () => void] => {
|
|
9
10
|
return createConnectionMockAltWithPool({}, ...queries)
|
|
@@ -12,6 +13,16 @@ export const createConnectionMockAlt = (...queries: MockQuery[][]): [Connection,
|
|
|
12
13
|
export const createConnectionMockAltWithPool = (
|
|
13
14
|
poolConfig: Partial<Omit<PoolConfig, 'logError'>>,
|
|
14
15
|
...queries: MockQuery[][]
|
|
16
|
+
): [Connection, () => void] => {
|
|
17
|
+
return createConnectionMockAltWithOptions({ pool: poolConfig }, ...queries)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const createConnectionMockAltWithOptions = (
|
|
21
|
+
{ pool: poolConfig = {}, connection: connectionOptions = {} }: {
|
|
22
|
+
pool?: Partial<Omit<PoolConfig, 'logError'>>
|
|
23
|
+
connection?: Connection.Options
|
|
24
|
+
},
|
|
25
|
+
...queries: MockQuery[][]
|
|
15
26
|
): [Connection, () => void] => {
|
|
16
27
|
const connectionMocks: (PgClient & { assertEmpty: () => void })[] = []
|
|
17
28
|
for (const queriesSet of queries) {
|
|
@@ -35,6 +46,12 @@ export const createConnectionMockAltWithPool = (
|
|
|
35
46
|
expect(query).toBeDefined()
|
|
36
47
|
expect(sql).toEqual(query?.sql as string)
|
|
37
48
|
await new Promise<void>(resolve => setTimeout(resolve, query?.timeout ?? 1))
|
|
49
|
+
if (query?.lost) {
|
|
50
|
+
this.emit('error', query.error)
|
|
51
|
+
}
|
|
52
|
+
if (query?.error) {
|
|
53
|
+
throw query.error
|
|
54
|
+
}
|
|
38
55
|
|
|
39
56
|
return query?.result
|
|
40
57
|
}
|
|
@@ -52,7 +69,7 @@ export const createConnectionMockAltWithPool = (
|
|
|
52
69
|
})()
|
|
53
70
|
}, { ...poolConfig, logError: () => null })
|
|
54
71
|
return [
|
|
55
|
-
new Connection(pool),
|
|
72
|
+
new Connection(pool, undefined, connectionOptions),
|
|
56
73
|
() => {
|
|
57
74
|
allMocks.forEach(it => it.assertEmpty())
|
|
58
75
|
},
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { expect, it } from 'bun:test'
|
|
2
|
+
import { createConnectionMockAltWithOptions } from './createConnectionMockAlt.js'
|
|
3
|
+
import { Connection } from '../../../src/index.js'
|
|
4
|
+
|
|
5
|
+
const terminated = () => new Error('Connection terminated unexpectedly')
|
|
6
|
+
const adminShutdown = () => Object.assign(new Error('terminating connection due to administrator command'), { code: '57P01' })
|
|
7
|
+
const retrying = { connection: { retryStaleConnection: true } }
|
|
8
|
+
|
|
9
|
+
const warmUp = async (connection: Connection) => {
|
|
10
|
+
await connection.query('SELECT 1')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
it('runs the scope again when a pooled connection was lost before answering', async () => {
|
|
14
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
15
|
+
retrying,
|
|
16
|
+
[{ sql: 'SELECT 1' }, { sql: 'BEGIN', error: terminated(), lost: true }],
|
|
17
|
+
[{ sql: 'BEGIN' }, { sql: 'SELECT 2', result: { rows: [{ ok: true }] } }, { sql: 'COMMIT', result: { command: 'COMMIT' } }],
|
|
18
|
+
)
|
|
19
|
+
await warmUp(connection)
|
|
20
|
+
const rows = await connection.transaction(async trx => (await trx.query('SELECT 2')).rows)
|
|
21
|
+
expect(rows).toStrictEqual([{ ok: true }])
|
|
22
|
+
end()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('runs the scope again when the server shut a pooled connection down', async () => {
|
|
26
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
27
|
+
retrying,
|
|
28
|
+
[{ sql: 'SELECT 1' }, { sql: 'SELECT 2', error: adminShutdown() }],
|
|
29
|
+
[{ sql: 'SELECT 2', result: { rows: [] } }],
|
|
30
|
+
)
|
|
31
|
+
await warmUp(connection)
|
|
32
|
+
await connection.query('SELECT 2')
|
|
33
|
+
end()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('keeps going through stale idle connections until a working one', async () => {
|
|
37
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
38
|
+
retrying,
|
|
39
|
+
[{ sql: 'SELECT 1', timeout: 5 }, { sql: 'SELECT 2', error: terminated(), lost: true }],
|
|
40
|
+
[{ sql: 'SELECT 1', timeout: 5 }, { sql: 'SELECT 2', error: adminShutdown() }],
|
|
41
|
+
[{ sql: 'SELECT 2', result: { rows: [] } }],
|
|
42
|
+
)
|
|
43
|
+
await Promise.all([warmUp(connection), warmUp(connection)])
|
|
44
|
+
await connection.query('SELECT 2')
|
|
45
|
+
end()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('does not retry without the option', async () => {
|
|
49
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
50
|
+
{},
|
|
51
|
+
[{ sql: 'SELECT 1' }, { sql: 'SELECT 2', error: terminated(), lost: true }],
|
|
52
|
+
)
|
|
53
|
+
await warmUp(connection)
|
|
54
|
+
await expect(connection.query('SELECT 2')).rejects.toThrow('Connection terminated unexpectedly')
|
|
55
|
+
end()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('does not retry a new connection', async () => {
|
|
59
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
60
|
+
retrying,
|
|
61
|
+
[{ sql: 'SELECT 1', error: terminated(), lost: true }],
|
|
62
|
+
)
|
|
63
|
+
await expect(connection.query('SELECT 1')).rejects.toThrow('Connection terminated unexpectedly')
|
|
64
|
+
end()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('does not retry once the connection answered a query', async () => {
|
|
68
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
69
|
+
retrying,
|
|
70
|
+
[{ sql: 'SELECT 1' }, { sql: 'BEGIN' }, { sql: 'SELECT 2', error: terminated(), lost: true }, { sql: 'ROLLBACK' }],
|
|
71
|
+
)
|
|
72
|
+
await warmUp(connection)
|
|
73
|
+
await expect(connection.transaction(trx => trx.query('SELECT 2'))).rejects.toThrow('Connection terminated unexpectedly')
|
|
74
|
+
end()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('does not retry a query error on a live connection', async () => {
|
|
78
|
+
const syntaxError = Object.assign(new Error('syntax error'), { code: '42601' })
|
|
79
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
80
|
+
retrying,
|
|
81
|
+
[{ sql: 'SELECT 1' }, { sql: 'SELECT 2', error: syntaxError }],
|
|
82
|
+
)
|
|
83
|
+
await warmUp(connection)
|
|
84
|
+
await expect(connection.query('SELECT 2')).rejects.toThrow('syntax error')
|
|
85
|
+
end()
|
|
86
|
+
})
|