@mielk/db 3.1.0 → 3.2.0
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/CHANGELOG.md +14 -0
- package/README.md +19 -12
- package/dist/errors/db-error.d.ts +1 -0
- package/dist/errors/db-error.js +1 -0
- package/dist/locales/en.json +1 -0
- package/dist/locales/pl.json +1 -0
- package/dist/pg/connection/transaction.d.ts +7 -0
- package/dist/pg/connection/transaction.js +20 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,20 @@ Versions up to 2.1.0 were generated by standard-version and are kept unchanged.
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [3.2.0] - 2026-11-23
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Public PostgreSQL error code `DB_ERROR_CODES.PG_TRANSACTION_ENDED_BY_CALLBACK` (TASK-403).
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- PostgreSQL `withTransaction` rejects when the callback resolves after ending the transaction itself with `ROLLBACK` or `COMMIT`. Changes already committed by the callback remain committed (TASK-403).
|
|
19
|
+
|
|
20
|
+
### Documentation
|
|
21
|
+
|
|
22
|
+
- Clarified that PostgreSQL `withTransaction` can commit a replacement transaction and that queries after an explicit callback `COMMIT` run outside a transaction until a new one begins (TASK-408).
|
|
23
|
+
|
|
10
24
|
## [3.1.0] - 2026-11-23
|
|
11
25
|
|
|
12
26
|
### Added
|
package/README.md
CHANGED
|
@@ -89,6 +89,12 @@ await closeDb();
|
|
|
89
89
|
leaves the transaction aborted. If PostgreSQL responds to `COMMIT` with `ROLLBACK`, `withTransaction` rejects
|
|
90
90
|
with `PG_TRANSACTION_ABORTED`. Handle the error outside `withTransaction`, or roll back to a savepoint inside
|
|
91
91
|
the callback to recover from an expected statement error.
|
|
92
|
+
- Let `withTransaction` control the transaction boundary. If `fn` resolves after ending the transaction with
|
|
93
|
+
`ROLLBACK` or `COMMIT`, leaving no active transaction, `withTransaction` rejects with
|
|
94
|
+
`PG_TRANSACTION_ENDED_BY_CALLBACK`. Any changes committed by `fn` remain committed. Queries that `fn` runs after
|
|
95
|
+
an explicit `COMMIT` execute outside a transaction until a new one begins. If `fn` runs `ROLLBACK; BEGIN` or
|
|
96
|
+
`COMMIT; BEGIN` and leaves the replacement transaction active, `withTransaction` cannot detect the replacement
|
|
97
|
+
and commits it when `fn` resolves successfully. Savepoints remain supported.
|
|
92
98
|
- `getQueryExecutor()` returns the transaction connection inside `withTransaction` and the pool outside it. Call it
|
|
93
99
|
for each unit of work rather than keeping the result: an executor taken outside a transaction stays the pool
|
|
94
100
|
also when used inside one. Most code needs only `query`.
|
|
@@ -151,18 +157,19 @@ call `closeDb` first to connect again.
|
|
|
151
157
|
Errors of the library are `DbError` objects: `code` identifies the error (compare it with `DB_ERROR_CODES`, never
|
|
152
158
|
the message), `message` is the English text for logs, and `cause` holds the original error, if there was one.
|
|
153
159
|
|
|
154
|
-
| Code (`DB_ERROR_CODES.*`)
|
|
155
|
-
|
|
|
156
|
-
| `PG_ALREADY_INITIALIZED`
|
|
157
|
-
| `PG_NOT_INITIALIZED`
|
|
158
|
-
| `PG_NESTED_TRANSACTION_FAILED`
|
|
159
|
-
| `PG_TRANSACTION_ABORTED`
|
|
160
|
-
| `
|
|
161
|
-
| `
|
|
162
|
-
| `
|
|
163
|
-
| `
|
|
164
|
-
| `
|
|
165
|
-
| `
|
|
160
|
+
| Code (`DB_ERROR_CODES.*`) | Thrown by | When |
|
|
161
|
+
| ---------------------------------- | ---------------------------------------------- | ------------------------------------------------------------------------- |
|
|
162
|
+
| `PG_ALREADY_INITIALIZED` | `initDb` (pg) | the pool exists; call `closeDb` first |
|
|
163
|
+
| `PG_NOT_INITIALIZED` | `query`, `withTransaction`, `getQueryExecutor` | `initDb` has not been called |
|
|
164
|
+
| `PG_NESTED_TRANSACTION_FAILED` | `withTransaction` | `fn` resolved although a nested `withTransaction` failed; rolled back |
|
|
165
|
+
| `PG_TRANSACTION_ABORTED` | `withTransaction` | PostgreSQL rolled back the aborted transaction in response to `COMMIT` |
|
|
166
|
+
| `PG_TRANSACTION_ENDED_BY_CALLBACK` | `withTransaction` | `fn` resolved after ending the transaction and left no active transaction |
|
|
167
|
+
| `PG_TRANSACTION_ENDED` | `query`, `withTransaction`, `getQueryExecutor` | called by an operation of `fn` after its transaction ended |
|
|
168
|
+
| `PG_UNEXPECTED_ERROR` | passed to `onError` (pg) | an idle connection of the pool failed (e.g. the server closed it) |
|
|
169
|
+
| `MSSQL_ALREADY_INITIALIZED` | `initDb` (mssql) | the settings are stored; call `closeDb` first |
|
|
170
|
+
| `MSSQL_NOT_INITIALIZED` | `query`, `spExecute` (mssql) | `initDb` has not been called |
|
|
171
|
+
| `MSSQL_DRIVER_NOT_INSTALLED` | `query`, `spExecute` (mssql) | `msnodesqlv8` cannot be loaded; the import error is the `cause` |
|
|
172
|
+
| `MSSQL_CLOSE_FAILED` | passed to `onError` (mssql) | closing the connection of `spExecute` failed |
|
|
166
173
|
|
|
167
174
|
Asynchronous functions report these errors as rejections. Errors of the server and the driver (failed connection,
|
|
168
175
|
failed query, `BEGIN`, `COMMIT`) are passed to the caller unchanged, not wrapped in `DbError`. `query` of
|
|
@@ -15,6 +15,7 @@ export declare const DB_ERROR_CODES: {
|
|
|
15
15
|
/** PostgreSQL rolled back the aborted transaction in response to COMMIT. */
|
|
16
16
|
readonly PG_TRANSACTION_ABORTED: "db/pg/transaction:aborted";
|
|
17
17
|
readonly PG_TRANSACTION_ENDED: "db/pg/transaction:ended";
|
|
18
|
+
readonly PG_TRANSACTION_ENDED_BY_CALLBACK: "db/pg/transaction:endedByCallback";
|
|
18
19
|
readonly PG_UNEXPECTED_ERROR: "db/pg/connection:unexpectedError";
|
|
19
20
|
};
|
|
20
21
|
/** Code of an error thrown by the library: one of the values of {@link DB_ERROR_CODES}. */
|
package/dist/errors/db-error.js
CHANGED
|
@@ -16,6 +16,7 @@ export const DB_ERROR_CODES = {
|
|
|
16
16
|
/** PostgreSQL rolled back the aborted transaction in response to COMMIT. */
|
|
17
17
|
PG_TRANSACTION_ABORTED: 'db/pg/transaction:aborted',
|
|
18
18
|
PG_TRANSACTION_ENDED: 'db/pg/transaction:ended',
|
|
19
|
+
PG_TRANSACTION_ENDED_BY_CALLBACK: 'db/pg/transaction:endedByCallback',
|
|
19
20
|
PG_UNEXPECTED_ERROR: 'db/pg/connection:unexpectedError',
|
|
20
21
|
};
|
|
21
22
|
const ENGLISH_TEXTS = en;
|
package/dist/locales/en.json
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"db/pg/transaction": {
|
|
14
14
|
"aborted": "PostgreSQL rolled back the aborted transaction when asked to commit.",
|
|
15
15
|
"ended": "The transaction of this call chain has already ended: a query started inside withTransaction cannot run after it.",
|
|
16
|
+
"endedByCallback": "The withTransaction callback ended the transaction before the library could commit it.",
|
|
16
17
|
"nestedFailed": "A nested withTransaction failed and its error was caught: the whole transaction is rolled back."
|
|
17
18
|
}
|
|
18
19
|
}
|
package/dist/locales/pl.json
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"db/pg/transaction": {
|
|
14
14
|
"aborted": "PostgreSQL wycofał przerwaną transakcję przy próbie zatwierdzenia.",
|
|
15
15
|
"ended": "Transakcja tego łańcucha wywołań już się zakończyła: zapytanie rozpoczęte wewnątrz withTransaction nie może zostać wykonane po niej.",
|
|
16
|
+
"endedByCallback": "Funkcja przekazana do withTransaction zakończyła transakcję, zanim biblioteka mogła ją zatwierdzić.",
|
|
16
17
|
"nestedFailed": "Zagnieżdżone withTransaction nie powiodło się, a jego błąd został przechwycony: cała transakcja zostaje wycofana."
|
|
17
18
|
}
|
|
18
19
|
}
|
|
@@ -27,10 +27,17 @@ export declare function query<T extends QueryResultRow = QueryResultRow>(text: s
|
|
|
27
27
|
* `fn` joins the outer transaction: it opens no transaction of its own, and its error rolls back the whole one, also
|
|
28
28
|
* when `fn` catches it.
|
|
29
29
|
*
|
|
30
|
+
* Let `withTransaction` control the transaction boundary. Queries after an explicit `COMMIT` in `fn` execute outside
|
|
31
|
+
* a transaction until a new one begins. If `fn` runs `ROLLBACK; BEGIN` or `COMMIT; BEGIN` and leaves the replacement
|
|
32
|
+
* transaction active, `withTransaction` cannot detect the replacement and commits it when `fn` resolves successfully.
|
|
33
|
+
*
|
|
34
|
+
* @param fn - The callback to run on the transaction connection.
|
|
35
|
+
* @returns The callback result after the transaction commits.
|
|
30
36
|
* @throws The error of `fn`, of `BEGIN` or of `COMMIT`, unchanged; a failed `ROLLBACK` does not replace it.
|
|
31
37
|
* @throws {DbError} `PG_NESTED_TRANSACTION_FAILED` when `fn` resolves although a nested `withTransaction` failed;
|
|
32
38
|
* the transaction is rolled back and the first error of a nested call is the `cause`.
|
|
33
39
|
* @throws {DbError} `PG_TRANSACTION_ABORTED` when PostgreSQL rolls back in response to `COMMIT`.
|
|
40
|
+
* @throws {DbError} `PG_TRANSACTION_ENDED_BY_CALLBACK` when the callback leaves no active transaction.
|
|
34
41
|
* @throws {DbError} `PG_TRANSACTION_ENDED` when called in the call chain of a transaction that has already ended.
|
|
35
42
|
* @throws The error of the pool when no connection can be opened.
|
|
36
43
|
*/
|
|
@@ -62,10 +62,17 @@ async function runNested(context, fn) {
|
|
|
62
62
|
* `fn` joins the outer transaction: it opens no transaction of its own, and its error rolls back the whole one, also
|
|
63
63
|
* when `fn` catches it.
|
|
64
64
|
*
|
|
65
|
+
* Let `withTransaction` control the transaction boundary. Queries after an explicit `COMMIT` in `fn` execute outside
|
|
66
|
+
* a transaction until a new one begins. If `fn` runs `ROLLBACK; BEGIN` or `COMMIT; BEGIN` and leaves the replacement
|
|
67
|
+
* transaction active, `withTransaction` cannot detect the replacement and commits it when `fn` resolves successfully.
|
|
68
|
+
*
|
|
69
|
+
* @param fn - The callback to run on the transaction connection.
|
|
70
|
+
* @returns The callback result after the transaction commits.
|
|
65
71
|
* @throws The error of `fn`, of `BEGIN` or of `COMMIT`, unchanged; a failed `ROLLBACK` does not replace it.
|
|
66
72
|
* @throws {DbError} `PG_NESTED_TRANSACTION_FAILED` when `fn` resolves although a nested `withTransaction` failed;
|
|
67
73
|
* the transaction is rolled back and the first error of a nested call is the `cause`.
|
|
68
74
|
* @throws {DbError} `PG_TRANSACTION_ABORTED` when PostgreSQL rolls back in response to `COMMIT`.
|
|
75
|
+
* @throws {DbError} `PG_TRANSACTION_ENDED_BY_CALLBACK` when the callback leaves no active transaction.
|
|
69
76
|
* @throws {DbError} `PG_TRANSACTION_ENDED` when called in the call chain of a transaction that has already ended.
|
|
70
77
|
* @throws The error of the pool when no connection can be opened.
|
|
71
78
|
*/
|
|
@@ -90,6 +97,19 @@ export async function withTransaction(fn) {
|
|
|
90
97
|
if (context.nestedFailure) {
|
|
91
98
|
throw new DbError(DB_ERROR_CODES.PG_NESTED_TRANSACTION_FAILED, { cause: context.nestedFailure.error });
|
|
92
99
|
}
|
|
100
|
+
// Outside a transaction, COMMIT only warns and still reports success. SAVEPOINT fails with 25P01.
|
|
101
|
+
try {
|
|
102
|
+
await client.query('SAVEPOINT mielk_db_before_commit');
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
const code = err instanceof Error && 'code' in err ? err.code : undefined;
|
|
106
|
+
if (code === '25P01') {
|
|
107
|
+
throw new DbError(DB_ERROR_CODES.PG_TRANSACTION_ENDED_BY_CALLBACK, { cause: err });
|
|
108
|
+
}
|
|
109
|
+
// Let COMMIT report ROLLBACK when the callback caught a query error (25P02).
|
|
110
|
+
if (code !== '25P02')
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
93
113
|
commit = await client.query('COMMIT');
|
|
94
114
|
}
|
|
95
115
|
catch (err) {
|
package/package.json
CHANGED