@contember/database 1.1.1 → 1.2.0-alpha.10
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/src/client/execution.ts
DELETED
|
@@ -1,93 +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
|
-
ClientError,
|
|
13
|
-
} from './errors'
|
|
14
|
-
import { ClientErrorCodes } from './errorCodes'
|
|
15
|
-
|
|
16
|
-
function prepareSql(sql: string) {
|
|
17
|
-
let parameterIndex = 0
|
|
18
|
-
return sql.replace(/(\\*)\?/g, ({}, numOfEscapes) => (numOfEscapes.length % 2 ? '?' : `$${++parameterIndex}`))
|
|
19
|
-
}
|
|
20
|
-
type SomeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
|
|
21
|
-
|
|
22
|
-
export async function executeQuery<Row extends Record<string, any>>(
|
|
23
|
-
pgClient: ClientBase,
|
|
24
|
-
eventManager: EventManager,
|
|
25
|
-
{ sql, parameters, meta, timing }: SomeOptional<Connection.Query, 'parameters' | 'meta'> & Connection.QueryConfig,
|
|
26
|
-
context: Connection.QueryContext = {},
|
|
27
|
-
): Promise<Connection.Result<Row>> {
|
|
28
|
-
parameters ??= []
|
|
29
|
-
meta ??= {}
|
|
30
|
-
try {
|
|
31
|
-
eventManager.fire(EventManager.Event.queryStart, { sql, parameters, meta })
|
|
32
|
-
|
|
33
|
-
const exec = async () => await pgClient.query(prepareSql(sql), parameters)
|
|
34
|
-
|
|
35
|
-
let result: Connection.Result<Row>
|
|
36
|
-
if (timing) {
|
|
37
|
-
const startHrTime = process.hrtime()
|
|
38
|
-
const startTimeUs = startHrTime[0] * 1e6 + Math.floor(startHrTime[1] / 1000)
|
|
39
|
-
|
|
40
|
-
result = await exec()
|
|
41
|
-
|
|
42
|
-
const endHrTime = process.hrtime()
|
|
43
|
-
const endTimeUs = endHrTime[0] * 1e6 + Math.floor(endHrTime[1] / 1000)
|
|
44
|
-
const realStart =
|
|
45
|
-
context.previousQueryEnd && context.previousQueryEnd > startTimeUs ? context.previousQueryEnd : startTimeUs
|
|
46
|
-
result = {
|
|
47
|
-
...result,
|
|
48
|
-
timing: {
|
|
49
|
-
selfDuration: endTimeUs - realStart,
|
|
50
|
-
totalDuration: endTimeUs - startTimeUs,
|
|
51
|
-
},
|
|
52
|
-
}
|
|
53
|
-
context.previousQueryEnd = endTimeUs
|
|
54
|
-
} else {
|
|
55
|
-
result = await exec()
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
eventManager.fire(EventManager.Event.queryEnd, { sql, parameters, meta }, result)
|
|
59
|
-
|
|
60
|
-
return result
|
|
61
|
-
} catch (error) {
|
|
62
|
-
if (!(error instanceof Error)) {
|
|
63
|
-
throw error
|
|
64
|
-
}
|
|
65
|
-
eventManager.fire(EventManager.Event.queryError, { sql, parameters, meta }, error)
|
|
66
|
-
|
|
67
|
-
switch ((error as any).code) {
|
|
68
|
-
case ClientErrorCodes.NOT_NULL_VIOLATION:
|
|
69
|
-
throw new NotNullViolationError(sql, parameters, error)
|
|
70
|
-
case ClientErrorCodes.FOREIGN_KEY_VIOLATION:
|
|
71
|
-
throw new ForeignKeyViolationError(sql, parameters, error)
|
|
72
|
-
case ClientErrorCodes.UNIQUE_VIOLATION:
|
|
73
|
-
throw new UniqueViolationError(sql, parameters, error)
|
|
74
|
-
case ClientErrorCodes.T_R_SERIALIZATION_FAILURE:
|
|
75
|
-
throw new SerializationFailureError(sql, parameters, error)
|
|
76
|
-
case ClientErrorCodes.INVALID_TEXT_REPRESENTATION:
|
|
77
|
-
case ClientErrorCodes.DATETIME_FIELD_OVERFLOW:
|
|
78
|
-
throw new InvalidDataError(sql, parameters, error)
|
|
79
|
-
case ClientErrorCodes.IN_FAILED_SQL_TRANSACTION:
|
|
80
|
-
throw new TransactionAbortedError(sql, parameters, error)
|
|
81
|
-
default:
|
|
82
|
-
throw new QueryError(sql, parameters, error)
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export const executeClientOperation = async <T>(cb: () => Promise<T>): Promise<T> => {
|
|
88
|
-
try {
|
|
89
|
-
return await cb()
|
|
90
|
-
} catch (e) {
|
|
91
|
-
throw new ClientError(e)
|
|
92
|
-
}
|
|
93
|
-
}
|