@contember/database 1.2.0-alpha.3 → 1.2.0-alpha.6
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/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 +18 -16
- package/dist/src/client/Connection.d.ts.map +1 -1
- package/dist/src/client/Connection.js +22 -30
- 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 +2 -1
- package/dist/src/client/Pool.d.ts.map +1 -1
- package/dist/src/client/Pool.js +14 -2
- package/dist/src/client/Pool.js.map +1 -1
- 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/index.d.ts +0 -1
- package/dist/src/client/index.d.ts.map +1 -1
- package/dist/src/client/index.js +0 -1
- package/dist/src/client/index.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +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.map +1 -1
- package/dist/src/utils/asyncIterableTransaction.js +21 -9
- package/dist/src/utils/asyncIterableTransaction.js.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 +1 -0
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/index.js.map +1 -1
- package/dist/src/utils/pgClientFactory.d.ts +3 -2
- package/dist/src/utils/pgClientFactory.d.ts.map +1 -1
- package/dist/src/utils/pgClientFactory.js.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.js +6 -10
- package/dist/tests/cases/unit/pool.test.js.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/client/AcquiredConnection.ts +104 -0
- package/src/client/Client.ts +34 -18
- package/src/client/Connection.ts +42 -46
- package/src/client/PgClient.ts +13 -0
- package/src/client/Pool.ts +13 -3
- package/src/client/Transaction.ts +101 -69
- package/src/client/index.ts +0 -1
- package/src/utils/Mutex.ts +49 -0
- package/src/utils/asyncIterableTransaction.ts +21 -9
- package/src/utils/createDatabaseIfNotExists.ts +3 -3
- package/src/utils/index.ts +1 -0
- package/src/utils/pgClientFactory.ts +3 -2
- 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 +6 -12
- package/dist/src/client/SingleConnection.d.ts +0 -21
- package/dist/src/client/SingleConnection.d.ts.map +0 -1
- package/dist/src/client/SingleConnection.js +0 -73
- package/dist/src/client/SingleConnection.js.map +0 -1
- package/dist/src/client/execution.d.ts +0 -7
- package/dist/src/client/execution.d.ts.map +0 -1
- package/dist/src/client/execution.js +0 -65
- package/dist/src/client/execution.js.map +0 -1
- package/src/client/SingleConnection.ts +0 -89
- package/src/client/execution.ts +0 -84
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
import { EventManager } from '../../../src'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const simpleQuery = (sql: string) => ({
|
|
6
|
-
sql,
|
|
7
|
-
parameters: [],
|
|
8
|
-
response: { rows: [] },
|
|
9
|
-
})
|
|
2
|
+
import { assert, test } from 'vitest'
|
|
3
|
+
import { createConnectionMockAlt } from './createConnectionMockAlt'
|
|
10
4
|
|
|
11
5
|
test('event manager: connection and client', async () => {
|
|
12
|
-
const connection =
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
const [connection, end] = createConnectionMockAlt([
|
|
7
|
+
{ sql: 'SELECT 1' },
|
|
8
|
+
{ sql: 'SELECT 2' },
|
|
15
9
|
])
|
|
16
10
|
const events: { source: string; sql: string }[] = []
|
|
17
11
|
const client = connection.createClient('public', {})
|
|
@@ -25,16 +19,17 @@ test('event manager: connection and client', async () => {
|
|
|
25
19
|
{ sql: 'SELECT 1', source: 'connection' },
|
|
26
20
|
{ sql: 'SELECT 2', source: 'connection' },
|
|
27
21
|
])
|
|
22
|
+
end()
|
|
28
23
|
})
|
|
29
24
|
|
|
30
25
|
|
|
31
26
|
test('event manager: connection and client with transaction', async () => {
|
|
32
|
-
const connection =
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
const [connection, end] = createConnectionMockAlt([
|
|
28
|
+
{ sql: 'BEGIN' },
|
|
29
|
+
{ sql: 'SELECT 1' },
|
|
30
|
+
{ sql: 'COMMIT' },
|
|
31
|
+
{ sql: 'SELECT 2' },
|
|
32
|
+
{ sql: 'SELECT 3' },
|
|
38
33
|
])
|
|
39
34
|
const events: { source: string; sql: string }[] = []
|
|
40
35
|
const client = connection.createClient('public', {})
|
|
@@ -49,13 +44,100 @@ test('event manager: connection and client with transaction', async () => {
|
|
|
49
44
|
await client.query('SELECT 2')
|
|
50
45
|
await connection.query('SELECT 3')
|
|
51
46
|
assert.deepStrictEqual(events, [
|
|
52
|
-
{ sql: 'BEGIN
|
|
47
|
+
{ sql: 'BEGIN', source: 'client' },
|
|
48
|
+
{ sql: 'BEGIN', source: 'connection' },
|
|
49
|
+
{ sql: 'SELECT 1', source: 'transaction' },
|
|
50
|
+
{ sql: 'SELECT 1', source: 'client' },
|
|
51
|
+
{ sql: 'SELECT 1', source: 'connection' },
|
|
52
|
+
{ sql: 'COMMIT', source: 'client' },
|
|
53
|
+
{ sql: 'COMMIT', source: 'connection' },
|
|
54
|
+
{ sql: 'SELECT 2', source: 'client' },
|
|
55
|
+
{ sql: 'SELECT 2', source: 'connection' },
|
|
56
|
+
{ sql: 'SELECT 3', source: 'connection' },
|
|
57
|
+
])
|
|
58
|
+
end()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
test('event manager: connection and client with scopes', async () => {
|
|
63
|
+
const [connection, end] = createConnectionMockAlt([
|
|
64
|
+
{ sql: 'SELECT 1' },
|
|
65
|
+
{ sql: 'SELECT 2' },
|
|
66
|
+
{ sql: 'SELECT 3' },
|
|
67
|
+
])
|
|
68
|
+
const events: { source: string; sql: string }[] = []
|
|
69
|
+
const client = connection.createClient('public', {})
|
|
70
|
+
|
|
71
|
+
connection.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'connection' }))
|
|
72
|
+
client.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'client' }))
|
|
73
|
+
await client.scope(async conn => {
|
|
74
|
+
conn.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'scoped' }))
|
|
75
|
+
await conn.query('SELECT 1')
|
|
76
|
+
|
|
77
|
+
})
|
|
78
|
+
await client.query('SELECT 2')
|
|
79
|
+
await connection.query('SELECT 3')
|
|
80
|
+
assert.deepStrictEqual(events, [
|
|
81
|
+
{ sql: 'SELECT 1', source: 'scoped' },
|
|
82
|
+
{ sql: 'SELECT 1', source: 'client' },
|
|
83
|
+
{ sql: 'SELECT 1', source: 'connection' },
|
|
84
|
+
{ sql: 'SELECT 2', source: 'client' },
|
|
85
|
+
{ sql: 'SELECT 2', source: 'connection' },
|
|
86
|
+
{ sql: 'SELECT 3', source: 'connection' },
|
|
87
|
+
])
|
|
88
|
+
end()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
test('event manager: connection and client with transaction and savepoint', async () => {
|
|
93
|
+
const [connection, end] = createConnectionMockAlt([
|
|
94
|
+
{ sql: 'BEGIN' },
|
|
95
|
+
{ sql: 'SELECT 1' },
|
|
96
|
+
{ sql: 'SAVEPOINT "savepoint_1"' },
|
|
97
|
+
{ sql: 'SELECT 2' },
|
|
98
|
+
{ sql: 'RELEASE SAVEPOINT "savepoint_1"' },
|
|
99
|
+
{ sql: 'COMMIT' },
|
|
100
|
+
{ sql: 'SELECT 3' },
|
|
101
|
+
{ sql: 'SELECT 4' },
|
|
102
|
+
])
|
|
103
|
+
const events: { source: string; sql: string }[] = []
|
|
104
|
+
const client = connection.createClient('public', {})
|
|
105
|
+
|
|
106
|
+
connection.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'connection' }))
|
|
107
|
+
client.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'client' }))
|
|
108
|
+
await client.transaction(async trx => {
|
|
109
|
+
trx.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'transaction' }))
|
|
110
|
+
await trx.query('SELECT 1')
|
|
111
|
+
await trx.transaction(async savepoint => {
|
|
112
|
+
savepoint.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'savepoint' }))
|
|
113
|
+
await savepoint.query('SELECT 2')
|
|
114
|
+
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
})
|
|
118
|
+
await client.query('SELECT 3')
|
|
119
|
+
await connection.query('SELECT 4')
|
|
120
|
+
assert.deepStrictEqual(events, [
|
|
121
|
+
{ sql: 'BEGIN', source: 'client' },
|
|
122
|
+
{ sql: 'BEGIN', source: 'connection' },
|
|
53
123
|
{ sql: 'SELECT 1', source: 'transaction' },
|
|
54
124
|
{ sql: 'SELECT 1', source: 'client' },
|
|
55
125
|
{ sql: 'SELECT 1', source: 'connection' },
|
|
56
|
-
{ sql: '
|
|
126
|
+
{ sql: 'SAVEPOINT "savepoint_1"', source: 'transaction' },
|
|
127
|
+
{ sql: 'SAVEPOINT "savepoint_1"', source: 'client' },
|
|
128
|
+
{ sql: 'SAVEPOINT "savepoint_1"', source: 'connection' },
|
|
129
|
+
{ sql: 'SELECT 2', source: 'savepoint' },
|
|
130
|
+
{ sql: 'SELECT 2', source: 'transaction' },
|
|
57
131
|
{ sql: 'SELECT 2', source: 'client' },
|
|
58
132
|
{ sql: 'SELECT 2', source: 'connection' },
|
|
133
|
+
{ sql: 'RELEASE SAVEPOINT "savepoint_1"', source: 'transaction' },
|
|
134
|
+
{ sql: 'RELEASE SAVEPOINT "savepoint_1"', source: 'client' },
|
|
135
|
+
{ sql: 'RELEASE SAVEPOINT "savepoint_1"', source: 'connection' },
|
|
136
|
+
{ sql: 'COMMIT', source: 'client' },
|
|
137
|
+
{ sql: 'COMMIT', source: 'connection' },
|
|
138
|
+
{ sql: 'SELECT 3', source: 'client' },
|
|
59
139
|
{ sql: 'SELECT 3', source: 'connection' },
|
|
140
|
+
{ sql: 'SELECT 4', source: 'connection' },
|
|
60
141
|
])
|
|
142
|
+
end()
|
|
61
143
|
})
|
|
@@ -297,29 +297,23 @@ it('fails to reconnect on recoverable error', async () => {
|
|
|
297
297
|
it('fails to reconnect on unrecoverable error', async () => {
|
|
298
298
|
const logger = createPoolLogger()
|
|
299
299
|
const pgClientMock = new PgClientMock()
|
|
300
|
+
pgClientMock.on('error', () => {
|
|
301
|
+
})
|
|
300
302
|
const pool = new Pool(() => pgClientMock as unknown as PgClient, {
|
|
301
303
|
log: logger,
|
|
302
|
-
acquireTimeoutMs: 3,
|
|
303
304
|
})
|
|
304
|
-
pgClientMock.connections.push(createErrorPromise())
|
|
305
305
|
pool.on('error', () => {
|
|
306
306
|
|
|
307
307
|
})
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
await expect(pool.acquire.bind(pool)).rejects.toThrowError('Failed to acquire a connection')
|
|
313
|
-
await expect(poolError).resolves.toEqual(new Error('my err'))
|
|
314
|
-
|
|
315
|
-
await timeout(4)
|
|
308
|
+
pgClientMock.connections.push(createErrorPromise())
|
|
309
|
+
await expect(async () => await pool.acquire()).rejects.toThrowError('Database client error: my err')
|
|
310
|
+
await timeout()
|
|
316
311
|
expect(logger.messages).toMatchInlineSnapshot(`
|
|
317
312
|
CAI P
|
|
318
313
|
000 1: Item added to a queue.
|
|
319
314
|
000 1: Creating a new connection
|
|
320
315
|
100 1: Connection error occurred: my err
|
|
321
|
-
000
|
|
322
|
-
000 0: Queued item timed out
|
|
316
|
+
000 0: Connecting failed, rejecting pending item
|
|
323
317
|
`)
|
|
324
318
|
})
|
|
325
319
|
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { ClientConfig } from 'pg';
|
|
2
|
-
import { EventManager } from './EventManager';
|
|
3
|
-
import { Client } from './Client';
|
|
4
|
-
import { Connection } from './Connection';
|
|
5
|
-
import { DatabaseConfig } 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 & DatabaseConfig, 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
|
-
private maybeConnect;
|
|
20
|
-
}
|
|
21
|
-
//# 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,cAAc,EAAE,MAAM,UAAU,CAAA;AAIzC,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,cAAc,EACrC,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;IAwBZ,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;YAUpB,YAAY;CAU1B"}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SingleConnection = void 0;
|
|
4
|
-
const EventManager_1 = require("./EventManager");
|
|
5
|
-
const Client_1 = require("./Client");
|
|
6
|
-
const Transaction_1 = require("./Transaction");
|
|
7
|
-
const execution_1 = require("./execution");
|
|
8
|
-
const errors_1 = require("./errors");
|
|
9
|
-
const utils_1 = require("../utils");
|
|
10
|
-
class SingleConnection {
|
|
11
|
-
constructor(config, queryConfig, eventManager = new EventManager_1.EventManager(null)) {
|
|
12
|
-
this.config = config;
|
|
13
|
-
this.queryConfig = queryConfig;
|
|
14
|
-
this.eventManager = eventManager;
|
|
15
|
-
this.isConnected = false;
|
|
16
|
-
this.pgClient = (0, utils_1.createPgClientFactory)(config)();
|
|
17
|
-
this.pgClient.on('error', err => {
|
|
18
|
-
// eslint-disable-next-line no-console
|
|
19
|
-
console.error(err);
|
|
20
|
-
this.eventManager.fire(EventManager_1.EventManager.Event.clientError, err);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
createClient(schema, queryMeta) {
|
|
24
|
-
return new Client_1.Client(this, schema, queryMeta, new EventManager_1.EventManager(this.eventManager));
|
|
25
|
-
}
|
|
26
|
-
async transaction(callback, options = {}) {
|
|
27
|
-
var _a;
|
|
28
|
-
await this.maybeConnect();
|
|
29
|
-
const eventManager = new EventManager_1.EventManager((_a = options.eventManager) !== null && _a !== void 0 ? _a : this.eventManager);
|
|
30
|
-
await (0, execution_1.executeQuery)(this.pgClient, eventManager, {
|
|
31
|
-
sql: 'BEGIN',
|
|
32
|
-
...this.queryConfig,
|
|
33
|
-
});
|
|
34
|
-
const transaction = new Transaction_1.Transaction(this.pgClient, eventManager, this.queryConfig);
|
|
35
|
-
try {
|
|
36
|
-
const result = await callback(transaction);
|
|
37
|
-
await transaction.commitUnclosed();
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
await transaction.rollbackUnclosed();
|
|
42
|
-
throw e;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
async end() {
|
|
46
|
-
this.isConnected = false;
|
|
47
|
-
await this.pgClient.end();
|
|
48
|
-
}
|
|
49
|
-
async query(sql, parameters = [], meta = {}, config = {}) {
|
|
50
|
-
var _a;
|
|
51
|
-
await this.maybeConnect();
|
|
52
|
-
const query = { sql, parameters, meta, ...this.queryConfig, ...config };
|
|
53
|
-
try {
|
|
54
|
-
return await (0, execution_1.executeQuery)(this.pgClient, (_a = config.eventManager) !== null && _a !== void 0 ? _a : this.eventManager, query, {});
|
|
55
|
-
}
|
|
56
|
-
catch (e) {
|
|
57
|
-
throw e;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
async maybeConnect() {
|
|
61
|
-
if (!this.isConnected) {
|
|
62
|
-
try {
|
|
63
|
-
await this.pgClient.connect();
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
throw new errors_1.ClientError(e);
|
|
67
|
-
}
|
|
68
|
-
this.isConnected = true;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
exports.SingleConnection = SingleConnection;
|
|
73
|
-
//# sourceMappingURL=SingleConnection.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SingleConnection.js","sourceRoot":"","sources":["../../../src/client/SingleConnection.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ClientBase } 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 {};
|
|
7
|
-
//# 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;AAgBzC,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"}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
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
|
-
//# sourceMappingURL=execution.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"execution.js","sourceRoot":"","sources":["../../../src/client/execution.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,89 +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 { DatabaseConfig } from '../types'
|
|
8
|
-
import { ClientError } from './errors'
|
|
9
|
-
import { createPgClientFactory } from '../utils'
|
|
10
|
-
|
|
11
|
-
export class SingleConnection implements Connection.ConnectionLike, Connection.ClientFactory {
|
|
12
|
-
private readonly pgClient: PgClient
|
|
13
|
-
private isConnected = false
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
private readonly config: ClientConfig & DatabaseConfig,
|
|
17
|
-
private readonly queryConfig: Connection.QueryConfig,
|
|
18
|
-
public readonly eventManager: EventManager = new EventManager(null),
|
|
19
|
-
) {
|
|
20
|
-
this.pgClient = createPgClientFactory(config)()
|
|
21
|
-
this.pgClient.on('error', err => {
|
|
22
|
-
// eslint-disable-next-line no-console
|
|
23
|
-
console.error(err)
|
|
24
|
-
this.eventManager.fire(EventManager.Event.clientError, err)
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public createClient(schema: string, queryMeta: Record<string, any>): Client {
|
|
29
|
-
return new Client(this, schema, queryMeta, new EventManager(this.eventManager))
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async transaction<Result>(
|
|
33
|
-
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
34
|
-
options: { eventManager?: EventManager } = {},
|
|
35
|
-
): Promise<Result> {
|
|
36
|
-
await this.maybeConnect()
|
|
37
|
-
const eventManager = new EventManager(options.eventManager ?? this.eventManager)
|
|
38
|
-
await executeQuery(
|
|
39
|
-
this.pgClient,
|
|
40
|
-
eventManager,
|
|
41
|
-
{
|
|
42
|
-
sql: 'BEGIN',
|
|
43
|
-
...this.queryConfig,
|
|
44
|
-
},
|
|
45
|
-
)
|
|
46
|
-
const transaction = new Transaction(this.pgClient, eventManager, this.queryConfig)
|
|
47
|
-
try {
|
|
48
|
-
const result = await callback(transaction)
|
|
49
|
-
|
|
50
|
-
await transaction.commitUnclosed()
|
|
51
|
-
|
|
52
|
-
return result
|
|
53
|
-
} catch (e) {
|
|
54
|
-
await transaction.rollbackUnclosed()
|
|
55
|
-
throw e
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async end(): Promise<void> {
|
|
60
|
-
this.isConnected = false
|
|
61
|
-
await this.pgClient.end()
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async query<Row extends Record<string, any>>(
|
|
65
|
-
sql: string,
|
|
66
|
-
parameters: any[] = [],
|
|
67
|
-
meta: Record<string, any> = {},
|
|
68
|
-
config: Connection.QueryConfig = {},
|
|
69
|
-
): Promise<Connection.Result<Row>> {
|
|
70
|
-
await this.maybeConnect()
|
|
71
|
-
const query: Connection.Query = { sql, parameters, meta, ...this.queryConfig, ...config }
|
|
72
|
-
try {
|
|
73
|
-
return await executeQuery<Row>(this.pgClient, config.eventManager ?? this.eventManager, query, {})
|
|
74
|
-
} catch (e) {
|
|
75
|
-
throw e
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
private async maybeConnect() {
|
|
80
|
-
if (!this.isConnected) {
|
|
81
|
-
try {
|
|
82
|
-
await this.pgClient.connect()
|
|
83
|
-
} catch (e) {
|
|
84
|
-
throw new ClientError(e)
|
|
85
|
-
}
|
|
86
|
-
this.isConnected = true
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
package/src/client/execution.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { ClientBase } from 'pg'
|
|
2
|
-
import { EventManager } from './EventManager'
|
|
3
|
-
import { Connection } from './Connection'
|
|
4
|
-
import {
|
|
5
|
-
QueryError,
|
|
6
|
-
ForeignKeyViolationError,
|
|
7
|
-
InvalidDataError,
|
|
8
|
-
NotNullViolationError,
|
|
9
|
-
SerializationFailureError,
|
|
10
|
-
TransactionAbortedError,
|
|
11
|
-
UniqueViolationError,
|
|
12
|
-
} from './errors'
|
|
13
|
-
import { ClientErrorCodes } from './errorCodes'
|
|
14
|
-
|
|
15
|
-
function prepareSql(sql: string) {
|
|
16
|
-
let parameterIndex = 0
|
|
17
|
-
return sql.replace(/(\\*)\?/g, ({}, numOfEscapes) => (numOfEscapes.length % 2 ? '?' : `$${++parameterIndex}`))
|
|
18
|
-
}
|
|
19
|
-
type SomeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
|
|
20
|
-
|
|
21
|
-
export async function executeQuery<Row extends Record<string, any>>(
|
|
22
|
-
pgClient: ClientBase,
|
|
23
|
-
eventManager: EventManager,
|
|
24
|
-
{ sql, parameters, meta, timing }: SomeOptional<Connection.Query, 'parameters' | 'meta'> & Connection.QueryConfig,
|
|
25
|
-
context: Connection.QueryContext = {},
|
|
26
|
-
): Promise<Connection.Result<Row>> {
|
|
27
|
-
parameters ??= []
|
|
28
|
-
meta ??= {}
|
|
29
|
-
try {
|
|
30
|
-
eventManager.fire(EventManager.Event.queryStart, { sql, parameters, meta })
|
|
31
|
-
|
|
32
|
-
const exec = async () => await pgClient.query(prepareSql(sql), parameters)
|
|
33
|
-
|
|
34
|
-
let result: Connection.Result<Row>
|
|
35
|
-
if (timing) {
|
|
36
|
-
const startHrTime = process.hrtime()
|
|
37
|
-
const startTimeUs = startHrTime[0] * 1e6 + Math.floor(startHrTime[1] / 1000)
|
|
38
|
-
|
|
39
|
-
result = await exec()
|
|
40
|
-
|
|
41
|
-
const endHrTime = process.hrtime()
|
|
42
|
-
const endTimeUs = endHrTime[0] * 1e6 + Math.floor(endHrTime[1] / 1000)
|
|
43
|
-
const realStart =
|
|
44
|
-
context.previousQueryEnd && context.previousQueryEnd > startTimeUs ? context.previousQueryEnd : startTimeUs
|
|
45
|
-
result = {
|
|
46
|
-
...result,
|
|
47
|
-
timing: {
|
|
48
|
-
selfDuration: endTimeUs - realStart,
|
|
49
|
-
totalDuration: endTimeUs - startTimeUs,
|
|
50
|
-
},
|
|
51
|
-
}
|
|
52
|
-
context.previousQueryEnd = endTimeUs
|
|
53
|
-
} else {
|
|
54
|
-
result = await exec()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
eventManager.fire(EventManager.Event.queryEnd, { sql, parameters, meta }, result)
|
|
58
|
-
|
|
59
|
-
return result
|
|
60
|
-
} catch (error) {
|
|
61
|
-
if (!(error instanceof Error)) {
|
|
62
|
-
throw error
|
|
63
|
-
}
|
|
64
|
-
eventManager.fire(EventManager.Event.queryError, { sql, parameters, meta }, error)
|
|
65
|
-
|
|
66
|
-
switch ((error as any).code) {
|
|
67
|
-
case ClientErrorCodes.NOT_NULL_VIOLATION:
|
|
68
|
-
throw new NotNullViolationError(sql, parameters, error)
|
|
69
|
-
case ClientErrorCodes.FOREIGN_KEY_VIOLATION:
|
|
70
|
-
throw new ForeignKeyViolationError(sql, parameters, error)
|
|
71
|
-
case ClientErrorCodes.UNIQUE_VIOLATION:
|
|
72
|
-
throw new UniqueViolationError(sql, parameters, error)
|
|
73
|
-
case ClientErrorCodes.T_R_SERIALIZATION_FAILURE:
|
|
74
|
-
throw new SerializationFailureError(sql, parameters, error)
|
|
75
|
-
case ClientErrorCodes.INVALID_TEXT_REPRESENTATION:
|
|
76
|
-
case ClientErrorCodes.DATETIME_FIELD_OVERFLOW:
|
|
77
|
-
throw new InvalidDataError(sql, parameters, error)
|
|
78
|
-
case ClientErrorCodes.IN_FAILED_SQL_TRANSACTION:
|
|
79
|
-
throw new TransactionAbortedError(sql, parameters, error)
|
|
80
|
-
default:
|
|
81
|
-
throw new QueryError(sql, parameters, error)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|