@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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/database",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-alpha.11",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
"test": "vitest"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@contember/queryable": "^1.
|
|
11
|
+
"@contember/queryable": "^1.2.0-alpha.11"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@contember/database-tester": "^1.
|
|
15
|
-
"@types/node": "^
|
|
14
|
+
"@contember/database-tester": "^1.2.0-alpha.11",
|
|
15
|
+
"@types/node": "^18",
|
|
16
16
|
"pg": "^8.5.0"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
@@ -37,7 +37,7 @@ class DeleteBuilder<Result extends DeleteBuilder.DeleteResult> implements Return
|
|
|
37
37
|
return this.withOption('using', { ...this.options.using, [alias || tableName]: tableName })
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
public returning<R = Returning.Result>(...columns: Returning.ReturningColumn[]): DeleteBuilder<R[]> {
|
|
40
|
+
public returning<R extends Returning.Result = Returning.Result>(...columns: Returning.ReturningColumn[]): DeleteBuilder<R[]> {
|
|
41
41
|
return this.withOption('returning', new Returning(columns)) as DeleteBuilder<any>
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -89,7 +89,7 @@ class InsertBuilder<Result extends InsertBuilder.InsertResult> implements With.A
|
|
|
89
89
|
return this.withOption('onConflict', conflictAction)
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
public returning<R = Returning.Result>(...columns: Returning.ReturningColumn[]): InsertBuilder<R[]> {
|
|
92
|
+
public returning<R extends Returning.Result = Returning.Result>(...columns: Returning.ReturningColumn[]): InsertBuilder<R[]> {
|
|
93
93
|
return this.withOption('returning', new Returning(columns)) as InsertBuilder<any>
|
|
94
94
|
}
|
|
95
95
|
|
|
@@ -35,7 +35,7 @@ class UpdateBuilder<Result extends UpdateBuilder.UpdateResult> implements With.A
|
|
|
35
35
|
return this.withOption('values', resolveValues(columns))
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
public returning<R = Returning.Result>(...columns: Returning.ReturningColumn[]): UpdateBuilder<R[]> {
|
|
38
|
+
public returning<R extends Returning.Result = Returning.Result>(...columns: Returning.ReturningColumn[]): UpdateBuilder<R[]> {
|
|
39
39
|
return this.withOption('returning', new Returning(columns)) as UpdateBuilder<any>
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Connection } from './Connection'
|
|
2
|
+
import { EventManager } from './EventManager'
|
|
3
|
+
import { Mutex } from '../utils'
|
|
4
|
+
import { executeTransaction, Transaction } from './Transaction'
|
|
5
|
+
import { ClientErrorCodes } from './errorCodes'
|
|
6
|
+
import {
|
|
7
|
+
ForeignKeyViolationError,
|
|
8
|
+
InvalidDataError,
|
|
9
|
+
NotNullViolationError,
|
|
10
|
+
QueryError,
|
|
11
|
+
SerializationFailureError,
|
|
12
|
+
TransactionAbortedError,
|
|
13
|
+
UniqueViolationError,
|
|
14
|
+
} from './errors'
|
|
15
|
+
import { PgClient } from './PgClient'
|
|
16
|
+
|
|
17
|
+
export class AcquiredConnection implements Connection.ConnectionLike {
|
|
18
|
+
private mutex = new Mutex()
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly pgClient: PgClient,
|
|
22
|
+
public readonly eventManager: EventManager,
|
|
23
|
+
) {
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async scope<Result>(
|
|
27
|
+
callback: (connection: Connection.ConnectionLike) => Promise<Result> | Result,
|
|
28
|
+
options: { eventManager?: EventManager } = {},
|
|
29
|
+
): Promise<Result> {
|
|
30
|
+
return await this.mutex.execute(async () => {
|
|
31
|
+
return await callback(new AcquiredConnection(this.pgClient, options.eventManager ?? this.eventManager))
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async transaction<Result>(
|
|
36
|
+
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
37
|
+
options: { eventManager?: EventManager } = {},
|
|
38
|
+
): Promise<Result> {
|
|
39
|
+
return await this.scope(async connection => {
|
|
40
|
+
await connection.query('BEGIN', [])
|
|
41
|
+
const transaction = new Transaction(connection)
|
|
42
|
+
return await executeTransaction(transaction, callback)
|
|
43
|
+
}, options)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async query<Row extends Record<string, any>>(
|
|
47
|
+
sql: string,
|
|
48
|
+
parameters: any[] = [],
|
|
49
|
+
meta: Record<string, any> = {},
|
|
50
|
+
): Promise<Connection.Result<Row>> {
|
|
51
|
+
return await this.mutex.execute(async () => {
|
|
52
|
+
try {
|
|
53
|
+
this.eventManager.fire(EventManager.Event.queryStart, { sql, parameters, meta })
|
|
54
|
+
|
|
55
|
+
let result: Connection.Result<Row>
|
|
56
|
+
const startHrTime = process.hrtime.bigint()
|
|
57
|
+
|
|
58
|
+
result = await this.pgClient.query(prepareSql(sql), parameters)
|
|
59
|
+
|
|
60
|
+
const endHrTime = process.hrtime.bigint()
|
|
61
|
+
const durationUs = Math.floor(Number(endHrTime - startHrTime) / 1000)
|
|
62
|
+
result = {
|
|
63
|
+
...result,
|
|
64
|
+
timing: {
|
|
65
|
+
selfDuration: durationUs,
|
|
66
|
+
totalDuration: durationUs,
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.eventManager.fire(EventManager.Event.queryEnd, { sql, parameters, meta }, result)
|
|
71
|
+
|
|
72
|
+
return result
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (!(error instanceof Error)) {
|
|
75
|
+
throw error
|
|
76
|
+
}
|
|
77
|
+
this.eventManager.fire(EventManager.Event.queryError, { sql, parameters, meta }, error)
|
|
78
|
+
|
|
79
|
+
switch ((error as any).code) {
|
|
80
|
+
case ClientErrorCodes.NOT_NULL_VIOLATION:
|
|
81
|
+
throw new NotNullViolationError(sql, parameters, error)
|
|
82
|
+
case ClientErrorCodes.FOREIGN_KEY_VIOLATION:
|
|
83
|
+
throw new ForeignKeyViolationError(sql, parameters, error)
|
|
84
|
+
case ClientErrorCodes.UNIQUE_VIOLATION:
|
|
85
|
+
throw new UniqueViolationError(sql, parameters, error)
|
|
86
|
+
case ClientErrorCodes.T_R_SERIALIZATION_FAILURE:
|
|
87
|
+
throw new SerializationFailureError(sql, parameters, error)
|
|
88
|
+
case ClientErrorCodes.INVALID_TEXT_REPRESENTATION:
|
|
89
|
+
case ClientErrorCodes.DATETIME_FIELD_OVERFLOW:
|
|
90
|
+
throw new InvalidDataError(sql, parameters, error)
|
|
91
|
+
case ClientErrorCodes.IN_FAILED_SQL_TRANSACTION:
|
|
92
|
+
throw new TransactionAbortedError(sql, parameters, error)
|
|
93
|
+
default:
|
|
94
|
+
throw new QueryError(sql, parameters, error)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function prepareSql(sql: string) {
|
|
102
|
+
let parameterIndex = 0
|
|
103
|
+
return sql.replace(/(\\*)\?/g, ({}, numOfEscapes) => (numOfEscapes.length % 2 ? '?' : `$${++parameterIndex}`))
|
|
104
|
+
}
|
package/src/client/Client.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { DatabaseQueryable } from '../queryable'
|
|
|
3
3
|
import { Connection } from './Connection'
|
|
4
4
|
import { EventManager } from './EventManager'
|
|
5
5
|
import { QueryHandler } from '@contember/queryable'
|
|
6
|
+
import { withDatabaseAdvisoryLock } from '../utils'
|
|
6
7
|
|
|
7
8
|
class Client<ConnectionType extends Connection.ConnectionLike = Connection.ConnectionLike> implements Connection.Queryable {
|
|
8
9
|
constructor(
|
|
@@ -10,27 +11,48 @@ class Client<ConnectionType extends Connection.ConnectionLike = Connection.Conne
|
|
|
10
11
|
public readonly schema: string,
|
|
11
12
|
public readonly queryMeta: Record<string, any>,
|
|
12
13
|
public readonly eventManager: EventManager = new EventManager(connection.eventManager),
|
|
13
|
-
) {
|
|
14
|
+
) {
|
|
15
|
+
}
|
|
14
16
|
|
|
15
17
|
public forSchema(schema: string): Client<ConnectionType> {
|
|
16
18
|
const eventManager = new EventManager(this.eventManager.parent)
|
|
17
19
|
return new Client<ConnectionType>(this.connection, schema, this.queryMeta, eventManager)
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
async scope<T>(callback: (wrapper: Client<Connection.ConnectionLike>) => Promise<T> | T): Promise<T> {
|
|
23
|
+
return await this.connection.scope(
|
|
24
|
+
connection => callback(
|
|
25
|
+
new Client(
|
|
26
|
+
connection,
|
|
27
|
+
this.schema,
|
|
28
|
+
this.queryMeta,
|
|
29
|
+
new EventManager(connection.eventManager),
|
|
30
|
+
),
|
|
31
|
+
),
|
|
32
|
+
{ eventManager: this.eventManager },
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
20
36
|
async transaction<T>(transactionScope: (wrapper: Client<Connection.TransactionLike>) => Promise<T> | T): Promise<T> {
|
|
21
37
|
return await this.connection.transaction(
|
|
22
|
-
transaction =>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
transaction => transactionScope(
|
|
39
|
+
new Client<Connection.TransactionLike>(
|
|
40
|
+
transaction,
|
|
41
|
+
this.schema,
|
|
42
|
+
this.queryMeta,
|
|
43
|
+
new EventManager(transaction.eventManager),
|
|
44
|
+
),
|
|
45
|
+
),
|
|
30
46
|
{ eventManager: this.eventManager },
|
|
31
47
|
)
|
|
32
48
|
}
|
|
33
49
|
|
|
50
|
+
async locked<T>(lock: number, callback: (wrapper: Client<Connection.ConnectionLike>) => Promise<T> | T): Promise<T> {
|
|
51
|
+
return await this.scope(client =>
|
|
52
|
+
withDatabaseAdvisoryLock(client.connection, lock, () => callback(client)),
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
34
56
|
selectBuilder<Result = SelectBuilder.Result>(): SelectBuilder<Result> {
|
|
35
57
|
return SelectBuilder.create<Result>()
|
|
36
58
|
}
|
|
@@ -51,16 +73,10 @@ class Client<ConnectionType extends Connection.ConnectionLike = Connection.Conne
|
|
|
51
73
|
sql: string,
|
|
52
74
|
parameters: readonly any[] = [],
|
|
53
75
|
meta: Record<string, any> = {},
|
|
54
|
-
config: Connection.QueryConfig = {},
|
|
55
76
|
): Promise<Connection.Result<Row>> {
|
|
56
|
-
return this.connection.
|
|
57
|
-
sql,
|
|
58
|
-
|
|
59
|
-
{ ...this.queryMeta, ...meta },
|
|
60
|
-
{
|
|
61
|
-
eventManager: this.eventManager,
|
|
62
|
-
...config,
|
|
63
|
-
},
|
|
77
|
+
return this.connection.scope(
|
|
78
|
+
connection => connection.query(sql, parameters, { ...this.queryMeta, ...meta }),
|
|
79
|
+
{ eventManager: this.eventManager },
|
|
64
80
|
)
|
|
65
81
|
}
|
|
66
82
|
|
package/src/client/Connection.ts
CHANGED
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
import { Pool, PoolConfig } from 'pg'
|
|
2
1
|
import { EventManager } from './EventManager'
|
|
3
2
|
import { Client } from './Client'
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
3
|
+
import { Pool, PoolConfig, PoolStatus } from './Pool'
|
|
4
|
+
import { createPgClientFactory } from '../utils'
|
|
5
|
+
import { DatabaseConfig } from '../types'
|
|
6
|
+
import { AcquiredConnection } from './AcquiredConnection'
|
|
7
7
|
|
|
8
8
|
class Connection implements Connection.ConnectionLike, Connection.ClientFactory, Connection.PoolStatusProvider {
|
|
9
|
-
private readonly pool: Pool
|
|
10
|
-
|
|
11
9
|
constructor(
|
|
12
|
-
|
|
13
|
-
private readonly queryConfig: Connection.QueryConfig,
|
|
10
|
+
private readonly pool: Pool,
|
|
14
11
|
public readonly eventManager: EventManager = new EventManager(null),
|
|
15
12
|
) {
|
|
16
|
-
this.pool = new Pool(config)
|
|
17
13
|
this.pool.on('error', err => {
|
|
18
14
|
// eslint-disable-next-line no-console
|
|
19
15
|
console.error(err)
|
|
@@ -21,73 +17,73 @@ class Connection implements Connection.ConnectionLike, Connection.ClientFactory,
|
|
|
21
17
|
})
|
|
22
18
|
}
|
|
23
19
|
|
|
20
|
+
public static create(
|
|
21
|
+
{ pool = {}, ...config }: DatabaseConfig & { pool?: PoolConfig },
|
|
22
|
+
): Connection {
|
|
23
|
+
return new Connection(new Pool(createPgClientFactory(config), pool))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public static createSingle(
|
|
27
|
+
config: DatabaseConfig,
|
|
28
|
+
): Connection {
|
|
29
|
+
return new Connection(new Pool(createPgClientFactory(config), { maxConnections: 1, maxIdle: 0 }))
|
|
30
|
+
}
|
|
31
|
+
|
|
24
32
|
public createClient(schema: string, queryMeta: Record<string, any>): Client {
|
|
25
33
|
return new Client(this, schema, queryMeta, new EventManager(this.eventManager))
|
|
26
34
|
}
|
|
27
35
|
|
|
28
|
-
async
|
|
29
|
-
callback: (connection: Connection.
|
|
36
|
+
async scope<Result>(
|
|
37
|
+
callback: (connection: Connection.ConnectionLike) => Promise<Result> | Result,
|
|
30
38
|
options: { eventManager?: EventManager } = {},
|
|
31
39
|
): Promise<Result> {
|
|
32
|
-
const
|
|
40
|
+
const acquired = await this.pool.acquire()
|
|
33
41
|
const eventManager = new EventManager(options.eventManager ?? this.eventManager)
|
|
34
|
-
await executeQuery(client, eventManager, {
|
|
35
|
-
sql: 'BEGIN',
|
|
36
|
-
...this.queryConfig,
|
|
37
|
-
})
|
|
38
|
-
const transaction = new Transaction(client, eventManager, this.queryConfig)
|
|
39
42
|
try {
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
client.release()
|
|
43
|
+
const connection = new AcquiredConnection(acquired.client, eventManager)
|
|
44
|
+
const result = await callback(connection)
|
|
45
|
+
this.pool.release(acquired)
|
|
44
46
|
|
|
45
47
|
return result
|
|
46
48
|
} catch (e) {
|
|
47
|
-
|
|
48
|
-
client.release(e as Error)
|
|
49
|
+
this.pool.dispose(acquired)
|
|
49
50
|
throw e
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
async
|
|
54
|
-
|
|
54
|
+
async transaction<Result>(
|
|
55
|
+
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
56
|
+
options: { eventManager?: EventManager } = {},
|
|
57
|
+
): Promise<Result> {
|
|
58
|
+
return await this.scope(async connection => {
|
|
59
|
+
return await connection.transaction(callback)
|
|
60
|
+
}, options)
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
async query<Row extends Record<string, any>>(
|
|
58
64
|
sql: string,
|
|
59
65
|
parameters: any[] = [],
|
|
60
66
|
meta: Record<string, any> = {},
|
|
61
|
-
{ eventManager, ...config }: Connection.QueryConfig = {},
|
|
62
67
|
): Promise<Connection.Result<Row>> {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const result = await executeQuery<Row>(client, eventManager ?? this.eventManager, query, {})
|
|
67
|
-
client.release()
|
|
68
|
-
return result
|
|
69
|
-
} catch (e) {
|
|
70
|
-
client.release(e as Error)
|
|
71
|
-
throw e
|
|
72
|
-
}
|
|
68
|
+
return await this.scope(async connection => {
|
|
69
|
+
return await connection.query(sql, parameters, meta)
|
|
70
|
+
})
|
|
73
71
|
}
|
|
74
72
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
idleCount: this.pool.idleCount,
|
|
78
|
-
totalCount: this.pool.totalCount,
|
|
79
|
-
waitingCount: this.pool.waitingCount,
|
|
80
|
-
maxCount: this.config.max || 10,
|
|
81
|
-
}
|
|
73
|
+
async end(): Promise<void> {
|
|
74
|
+
await this.pool.end()
|
|
82
75
|
}
|
|
83
|
-
}
|
|
84
76
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
77
|
+
public async clearPool(): Promise<void> {
|
|
78
|
+
await this.pool.closeIdle()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getPoolStatus(): PoolStatus {
|
|
82
|
+
return this.pool.getPoolStatus()
|
|
89
83
|
}
|
|
84
|
+
}
|
|
90
85
|
|
|
86
|
+
namespace Connection {
|
|
91
87
|
export interface Queryable {
|
|
92
88
|
readonly eventManager: EventManager
|
|
93
89
|
|
|
@@ -95,7 +91,6 @@ namespace Connection {
|
|
|
95
91
|
sql: string,
|
|
96
92
|
parameters?: readonly any[],
|
|
97
93
|
meta?: Record<string, any>,
|
|
98
|
-
config?: Connection.QueryConfig,
|
|
99
94
|
): Promise<Connection.Result<Row>>
|
|
100
95
|
}
|
|
101
96
|
|
|
@@ -110,21 +105,26 @@ namespace Connection {
|
|
|
110
105
|
& Connection.ConnectionLike
|
|
111
106
|
& Connection.ClientFactory
|
|
112
107
|
& Connection.PoolStatusProvider
|
|
113
|
-
& {
|
|
114
|
-
config: DatabaseCredentials
|
|
115
|
-
}
|
|
116
108
|
|
|
117
|
-
export interface
|
|
109
|
+
export interface Scopable<Sub> {
|
|
110
|
+
scope<Result>(
|
|
111
|
+
callback: (connection: Sub) => Promise<Result> | Result,
|
|
112
|
+
options?: { eventManager?: EventManager },
|
|
113
|
+
): Promise<Result>
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface ConnectionLike extends Transactional, Queryable, Scopable<ConnectionLike> {
|
|
117
|
+
}
|
|
118
118
|
|
|
119
119
|
export interface ClientFactory {
|
|
120
120
|
createClient(schema: string, queryMeta: Record<string, any>): Client
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
export interface PoolStatusProvider {
|
|
124
|
-
getPoolStatus(): PoolStatus
|
|
124
|
+
getPoolStatus(): PoolStatus | undefined
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
export interface TransactionLike extends
|
|
127
|
+
export interface TransactionLike extends Transactional, Queryable, Scopable<TransactionLike> {
|
|
128
128
|
readonly isClosed: boolean
|
|
129
129
|
|
|
130
130
|
rollback(): Promise<void>
|
|
@@ -132,30 +132,24 @@ namespace Connection {
|
|
|
132
132
|
commit(): Promise<void>
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
export interface QueryContext {
|
|
136
|
-
previousQueryEnd?: number
|
|
137
|
-
}
|
|
138
|
-
|
|
139
135
|
export interface Query {
|
|
140
136
|
readonly sql: string
|
|
141
137
|
readonly parameters: any[]
|
|
142
138
|
readonly meta: Record<string, any>
|
|
143
139
|
}
|
|
144
140
|
|
|
145
|
-
export type Credentials = Pick<PoolConfig, 'host' | 'port' | 'user' | 'password' | 'database'>
|
|
146
|
-
|
|
147
141
|
export interface Result<Row extends Record<string, any> = Record<string, any>> {
|
|
148
142
|
readonly rowCount: number
|
|
149
143
|
readonly rows: Row[]
|
|
150
144
|
readonly timing?: {
|
|
151
|
-
totalDuration: number
|
|
152
145
|
selfDuration: number
|
|
146
|
+
/** @deprecated both selfDuration and totalDuration now contains same number */
|
|
147
|
+
totalDuration: number
|
|
153
148
|
}
|
|
154
149
|
}
|
|
155
150
|
|
|
156
151
|
export const REPEATABLE_READ = 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ'
|
|
157
152
|
|
|
158
|
-
export type PoolStatus = { totalCount: number; idleCount: number; waitingCount: number; maxCount: number }
|
|
159
153
|
}
|
|
160
154
|
|
|
161
155
|
export { Connection }
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { QueryConfig, QueryResult, QueryResultRow } from 'pg'
|
|
2
|
+
import EventEmitter from 'events'
|
|
3
|
+
|
|
4
|
+
export interface PgClient extends EventEmitter {
|
|
5
|
+
connect(): Promise<void>
|
|
6
|
+
|
|
7
|
+
query<R extends QueryResultRow = any, I extends any[] = any[]>(
|
|
8
|
+
queryTextOrConfig: string | QueryConfig<I>,
|
|
9
|
+
values?: I,
|
|
10
|
+
): Promise<QueryResult<R>>
|
|
11
|
+
|
|
12
|
+
end(): Promise<void>
|
|
13
|
+
}
|