@mielk/db 3.0.0 → 3.1.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 +11 -1
- package/README.md +31 -19
- package/dist/errors/db-error.d.ts +2 -0
- package/dist/errors/db-error.js +2 -0
- package/dist/locales/en.json +1 -0
- package/dist/locales/pl.json +1 -0
- package/dist/pg/connection/transaction.d.ts +1 -0
- package/dist/pg/connection/transaction.js +8 -3
- package/package.json +6 -5
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,16 @@ Versions up to 2.1.0 were generated by standard-version and are kept unchanged.
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [3.1.0] - 2026-11-23
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Public PostgreSQL error code `DB_ERROR_CODES.PG_TRANSACTION_ABORTED` (TASK-357).
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- PostgreSQL `withTransaction` rejects with `PG_TRANSACTION_ABORTED` when PostgreSQL responds to `COMMIT` with `ROLLBACK` (TASK-357).
|
|
19
|
+
|
|
10
20
|
## [3.0.0] - 2026-11-15
|
|
11
21
|
|
|
12
22
|
### Added
|
|
@@ -25,7 +35,7 @@ Versions up to 2.1.0 were generated by standard-version and are kept unchanged.
|
|
|
25
35
|
|
|
26
36
|
### Changed
|
|
27
37
|
|
|
28
|
-
- **Breaking:** the package is renamed from `mielk-db` to `@mielk/db` (scope `@mielk` of the npm account `mielk`); `mielk-db` is
|
|
38
|
+
- **Breaking:** the package is renamed from `mielk-db` to `@mielk/db` (scope `@mielk` of the npm account `mielk`); `mielk-db` is deprecated and gets no new versions. Migration: replace `mielk-db` with `@mielk/db` in `package.json` and in every import, including subpaths (e.g. `mielk-db/mssql` becomes `@mielk/db/mssql`); do not keep both packages in one application (TASK-227).
|
|
29
39
|
- **Breaking:** Node.js 24 or later is required (`engines.node: >=24`). Migration: run the application on Node.js 24 (TASK-189).
|
|
30
40
|
- **Breaking:** the main entry point of the package (`mielk-db` without a subpath) is removed (it pointed to a file the build did not create). Migration: import from `@mielk/db/pg` or `@mielk/db/mssql` (TASK-189).
|
|
31
41
|
- **Breaking:** `pg` and `msnodesqlv8` are optional peer dependencies instead of optional dependencies, so they are no longer installed with the library; `@types/pg` is an optional peer dependency too. Migration: install `pg` (and `@types/pg` in TypeScript) or `msnodesqlv8` in the application (TASK-189).
|
package/README.md
CHANGED
|
@@ -5,9 +5,8 @@ asynchronous call chain (`@mielk/db/pg`), and simple queries and stored procedur
|
|
|
5
5
|
over ODBC (`@mielk/db/mssql`). Errors of the library have stable codes, with texts in several languages for
|
|
6
6
|
display. The library is written in TypeScript and published to npm as an ES module with type declarations.
|
|
7
7
|
|
|
8
|
-
Versions up to 2.1.0 were published as `mielk-db
|
|
9
|
-
|
|
10
|
-
when 3.0.0 is published. The migration is described in
|
|
8
|
+
Versions up to 2.1.0 were published as `mielk-db`; that package is deprecated and gets no new versions.
|
|
9
|
+
Version 3.0.0 and later are published as `@mielk/db` (repository `mielk-db`). The migration is described in
|
|
11
10
|
[CHANGELOG.md](CHANGELOG.md).
|
|
12
11
|
|
|
13
12
|
## Requirements
|
|
@@ -87,8 +86,9 @@ await closeDb();
|
|
|
87
86
|
outer `withTransaction` then rolls back and rejects with `PG_NESTED_TRANSACTION_FAILED`, whose `cause` is the
|
|
88
87
|
first error of a nested call.
|
|
89
88
|
- PostgreSQL aborts a transaction after any failed statement. An error caught inside `fn` and not rethrown still
|
|
90
|
-
leaves the transaction aborted
|
|
91
|
-
|
|
89
|
+
leaves the transaction aborted. If PostgreSQL responds to `COMMIT` with `ROLLBACK`, `withTransaction` rejects
|
|
90
|
+
with `PG_TRANSACTION_ABORTED`. Handle the error outside `withTransaction`, or roll back to a savepoint inside
|
|
91
|
+
the callback to recover from an expected statement error.
|
|
92
92
|
- `getQueryExecutor()` returns the transaction connection inside `withTransaction` and the pool outside it. Call it
|
|
93
93
|
for each unit of work rather than keeping the result: an executor taken outside a transaction stays the pool
|
|
94
94
|
also when used inside one. Most code needs only `query`.
|
|
@@ -151,17 +151,18 @@ call `closeDb` first to connect again.
|
|
|
151
151
|
Errors of the library are `DbError` objects: `code` identifies the error (compare it with `DB_ERROR_CODES`, never
|
|
152
152
|
the message), `message` is the English text for logs, and `cause` holds the original error, if there was one.
|
|
153
153
|
|
|
154
|
-
| Code (`DB_ERROR_CODES.*`) | Thrown by | When
|
|
155
|
-
| ------------------------------ | ---------------------------------------------- |
|
|
156
|
-
| `PG_ALREADY_INITIALIZED` | `initDb` (pg) | the pool exists; call `closeDb` first
|
|
157
|
-
| `PG_NOT_INITIALIZED` | `query`, `withTransaction`, `getQueryExecutor` | `initDb` has not been called
|
|
158
|
-
| `PG_NESTED_TRANSACTION_FAILED` | `withTransaction` | `fn` resolved although a nested `withTransaction` failed; rolled back
|
|
159
|
-
| `
|
|
160
|
-
| `
|
|
161
|
-
| `
|
|
162
|
-
| `
|
|
163
|
-
| `
|
|
164
|
-
| `
|
|
154
|
+
| Code (`DB_ERROR_CODES.*`) | Thrown by | When |
|
|
155
|
+
| ------------------------------ | ---------------------------------------------- | ---------------------------------------------------------------------- |
|
|
156
|
+
| `PG_ALREADY_INITIALIZED` | `initDb` (pg) | the pool exists; call `closeDb` first |
|
|
157
|
+
| `PG_NOT_INITIALIZED` | `query`, `withTransaction`, `getQueryExecutor` | `initDb` has not been called |
|
|
158
|
+
| `PG_NESTED_TRANSACTION_FAILED` | `withTransaction` | `fn` resolved although a nested `withTransaction` failed; rolled back |
|
|
159
|
+
| `PG_TRANSACTION_ABORTED` | `withTransaction` | PostgreSQL rolled back the aborted transaction in response to `COMMIT` |
|
|
160
|
+
| `PG_TRANSACTION_ENDED` | `query`, `withTransaction`, `getQueryExecutor` | called by an operation of `fn` after its transaction ended |
|
|
161
|
+
| `PG_UNEXPECTED_ERROR` | passed to `onError` (pg) | an idle connection of the pool failed (e.g. the server closed it) |
|
|
162
|
+
| `MSSQL_ALREADY_INITIALIZED` | `initDb` (mssql) | the settings are stored; call `closeDb` first |
|
|
163
|
+
| `MSSQL_NOT_INITIALIZED` | `query`, `spExecute` (mssql) | `initDb` has not been called |
|
|
164
|
+
| `MSSQL_DRIVER_NOT_INSTALLED` | `query`, `spExecute` (mssql) | `msnodesqlv8` cannot be loaded; the import error is the `cause` |
|
|
165
|
+
| `MSSQL_CLOSE_FAILED` | passed to `onError` (mssql) | closing the connection of `spExecute` failed |
|
|
165
166
|
|
|
166
167
|
Asynchronous functions report these errors as rejections. Errors of the server and the driver (failed connection,
|
|
167
168
|
failed query, `BEGIN`, `COMMIT`) are passed to the caller unchanged, not wrapped in `DbError`. `query` of
|
|
@@ -209,22 +210,33 @@ message text, which depends on the server language, so `@mielk/db/mssql` reports
|
|
|
209
210
|
| ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
|
|
210
211
|
| `npm ci` | install dependencies from `package-lock.json` |
|
|
211
212
|
| `npm test` | all unit tests (Vitest) with coverage; threshold 80% of lines and branches; the report is written to the system temp folder |
|
|
213
|
+
| `npm run test:pg` | PostgreSQL integration test against a temporary local database |
|
|
212
214
|
| `npm run test:watch` | tests in watch mode |
|
|
213
215
|
| `npm run typecheck` | TypeScript check without output |
|
|
214
216
|
| `npm run lint` | ESLint with type information, no warnings allowed |
|
|
215
217
|
| `npm run format` / `npm run format:check` | Prettier |
|
|
216
218
|
| `npm run build` | compiles `src` to `dist` (clears the content of `dist`, keeps the folder) |
|
|
217
219
|
| `npm run dev` | build in watch mode |
|
|
218
|
-
| `npm run check:package` | `publint --strict
|
|
220
|
+
| `npm run check:package` | `mielk-check-package` from `@mielk/lib-config`: `publint --strict`, `attw` and the package file list |
|
|
219
221
|
|
|
220
222
|
`prepublishOnly` runs typecheck, lint, tests, build and the package check.
|
|
221
|
-
|
|
223
|
+
The TypeScript, ESLint, Prettier and Vitest configuration comes from the shared package `@mielk/lib-config`
|
|
224
|
+
(development dependency); the files in this repository only extend it.
|
|
225
|
+
Unit tests replace `pg` and `msnodesqlv8` with test doubles. Run the PostgreSQL integration test separately with
|
|
226
|
+
`npm run test:pg`.
|
|
227
|
+
|
|
228
|
+
Start a dedicated local PostgreSQL 16 test cluster on `127.0.0.1`. Set `MIELK_DB_TEST_PG_PORT` to its port and
|
|
229
|
+
`MIELK_DB_TEST_PG_USER` to a bootstrap login with `CREATEDB` and `CREATEROLE` privileges and access to the
|
|
230
|
+
`postgres` maintenance database. Set `MIELK_DB_TEST_PG_PASSWORD` if the login requires a password. The cluster
|
|
231
|
+
must allow the generated test login to connect over loopback. The suite creates a uniquely named database and
|
|
232
|
+
login for each run and drops both during teardown. If the port or bootstrap login is missing, the suite prints
|
|
233
|
+
the missing variables and skips the test.
|
|
222
234
|
|
|
223
235
|
## Releases and changes
|
|
224
236
|
|
|
225
237
|
Changes are listed by hand in [CHANGELOG.md](CHANGELOG.md) under `Unreleased`; it also holds the migration notes
|
|
226
238
|
between major versions. `npm version <major|minor|patch>` moves the entries to the new version
|
|
227
|
-
(`
|
|
239
|
+
(`mielk-release-changelog` from `@mielk/lib-config`) and creates the release commit and tag.
|
|
228
240
|
|
|
229
241
|
## Documentation scope
|
|
230
242
|
|
|
@@ -12,6 +12,8 @@ export declare const DB_ERROR_CODES: {
|
|
|
12
12
|
readonly PG_ALREADY_INITIALIZED: "db/pg/connection:alreadyInitialized";
|
|
13
13
|
readonly PG_NESTED_TRANSACTION_FAILED: "db/pg/transaction:nestedFailed";
|
|
14
14
|
readonly PG_NOT_INITIALIZED: "db/pg/connection:notInitialized";
|
|
15
|
+
/** PostgreSQL rolled back the aborted transaction in response to COMMIT. */
|
|
16
|
+
readonly PG_TRANSACTION_ABORTED: "db/pg/transaction:aborted";
|
|
15
17
|
readonly PG_TRANSACTION_ENDED: "db/pg/transaction:ended";
|
|
16
18
|
readonly PG_UNEXPECTED_ERROR: "db/pg/connection:unexpectedError";
|
|
17
19
|
};
|
package/dist/errors/db-error.js
CHANGED
|
@@ -13,6 +13,8 @@ export const DB_ERROR_CODES = {
|
|
|
13
13
|
PG_ALREADY_INITIALIZED: 'db/pg/connection:alreadyInitialized',
|
|
14
14
|
PG_NESTED_TRANSACTION_FAILED: 'db/pg/transaction:nestedFailed',
|
|
15
15
|
PG_NOT_INITIALIZED: 'db/pg/connection:notInitialized',
|
|
16
|
+
/** PostgreSQL rolled back the aborted transaction in response to COMMIT. */
|
|
17
|
+
PG_TRANSACTION_ABORTED: 'db/pg/transaction:aborted',
|
|
16
18
|
PG_TRANSACTION_ENDED: 'db/pg/transaction:ended',
|
|
17
19
|
PG_UNEXPECTED_ERROR: 'db/pg/connection:unexpectedError',
|
|
18
20
|
};
|
package/dist/locales/en.json
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"unexpectedError": "Unexpected error of an idle PostgreSQL connection."
|
|
12
12
|
},
|
|
13
13
|
"db/pg/transaction": {
|
|
14
|
+
"aborted": "PostgreSQL rolled back the aborted transaction when asked to commit.",
|
|
14
15
|
"ended": "The transaction of this call chain has already ended: a query started inside withTransaction cannot run after it.",
|
|
15
16
|
"nestedFailed": "A nested withTransaction failed and its error was caught: the whole transaction is rolled back."
|
|
16
17
|
}
|
package/dist/locales/pl.json
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"unexpectedError": "Nieoczekiwany błąd bezczynnego połączenia z PostgreSQL."
|
|
12
12
|
},
|
|
13
13
|
"db/pg/transaction": {
|
|
14
|
+
"aborted": "PostgreSQL wycofał przerwaną transakcję przy próbie zatwierdzenia.",
|
|
14
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.",
|
|
15
16
|
"nestedFailed": "Zagnieżdżone withTransaction nie powiodło się, a jego błąd został przechwycony: cała transakcja zostaje wycofana."
|
|
16
17
|
}
|
|
@@ -30,6 +30,7 @@ export declare function query<T extends QueryResultRow = QueryResultRow>(text: s
|
|
|
30
30
|
* @throws The error of `fn`, of `BEGIN` or of `COMMIT`, unchanged; a failed `ROLLBACK` does not replace it.
|
|
31
31
|
* @throws {DbError} `PG_NESTED_TRANSACTION_FAILED` when `fn` resolves although a nested `withTransaction` failed;
|
|
32
32
|
* the transaction is rolled back and the first error of a nested call is the `cause`.
|
|
33
|
+
* @throws {DbError} `PG_TRANSACTION_ABORTED` when PostgreSQL rolls back in response to `COMMIT`.
|
|
33
34
|
* @throws {DbError} `PG_TRANSACTION_ENDED` when called in the call chain of a transaction that has already ended.
|
|
34
35
|
* @throws The error of the pool when no connection can be opened.
|
|
35
36
|
*/
|
|
@@ -65,6 +65,7 @@ async function runNested(context, fn) {
|
|
|
65
65
|
* @throws The error of `fn`, of `BEGIN` or of `COMMIT`, unchanged; a failed `ROLLBACK` does not replace it.
|
|
66
66
|
* @throws {DbError} `PG_NESTED_TRANSACTION_FAILED` when `fn` resolves although a nested `withTransaction` failed;
|
|
67
67
|
* the transaction is rolled back and the first error of a nested call is the `cause`.
|
|
68
|
+
* @throws {DbError} `PG_TRANSACTION_ABORTED` when PostgreSQL rolls back in response to `COMMIT`.
|
|
68
69
|
* @throws {DbError} `PG_TRANSACTION_ENDED` when called in the call chain of a transaction that has already ended.
|
|
69
70
|
* @throws The error of the pool when no connection can be opened.
|
|
70
71
|
*/
|
|
@@ -75,9 +76,10 @@ export async function withTransaction(fn) {
|
|
|
75
76
|
const client = await getPool().connect();
|
|
76
77
|
const context = { client, active: true };
|
|
77
78
|
let brokenConnection;
|
|
79
|
+
let result;
|
|
80
|
+
let commit;
|
|
78
81
|
try {
|
|
79
82
|
await client.query('BEGIN');
|
|
80
|
-
let result;
|
|
81
83
|
try {
|
|
82
84
|
result = await transactionContext.run(context, fn);
|
|
83
85
|
}
|
|
@@ -88,8 +90,7 @@ export async function withTransaction(fn) {
|
|
|
88
90
|
if (context.nestedFailure) {
|
|
89
91
|
throw new DbError(DB_ERROR_CODES.PG_NESTED_TRANSACTION_FAILED, { cause: context.nestedFailure.error });
|
|
90
92
|
}
|
|
91
|
-
await client.query('COMMIT');
|
|
92
|
-
return result;
|
|
93
|
+
commit = await client.query('COMMIT');
|
|
93
94
|
}
|
|
94
95
|
catch (err) {
|
|
95
96
|
try {
|
|
@@ -106,4 +107,8 @@ export async function withTransaction(fn) {
|
|
|
106
107
|
finally {
|
|
107
108
|
client.release(brokenConnection);
|
|
108
109
|
}
|
|
110
|
+
// PostgreSQL has already rolled back, so no further command is needed on the connection.
|
|
111
|
+
if (commit.command === 'ROLLBACK')
|
|
112
|
+
throw new DbError(DB_ERROR_CODES.PG_TRANSACTION_ABORTED);
|
|
113
|
+
return result;
|
|
109
114
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mielk/db",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "PostgreSQL connection pool with call-chain transactions and MS SQL queries over ODBC, with coded errors, for mielk Node.js applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "vitest run --coverage",
|
|
11
|
+
"test:pg": "vitest run --config vitest.pg.config.ts",
|
|
11
12
|
"build": "npm run clean && tsc -p tsconfig.build.json",
|
|
12
13
|
"prepublishOnly": "npm run typecheck && npm run lint && npm test && npm run build && npm run check:package",
|
|
13
14
|
"dev": "tsc -p tsconfig.build.json --watch",
|
|
@@ -16,8 +17,8 @@
|
|
|
16
17
|
"lint": "eslint . --max-warnings 0",
|
|
17
18
|
"format": "prettier --write .",
|
|
18
19
|
"format:check": "prettier --check .",
|
|
19
|
-
"check:package": "
|
|
20
|
-
"version": "
|
|
20
|
+
"check:package": "mielk-check-package",
|
|
21
|
+
"version": "mielk-release-changelog && git add CHANGELOG.md",
|
|
21
22
|
"clean": "node -e \"const fs = require('node:fs'); for (const e of fs.existsSync('dist') ? fs.readdirSync('dist') : []) fs.rmSync('dist/' + e, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 })\""
|
|
22
23
|
},
|
|
23
24
|
"exports": {
|
|
@@ -33,6 +34,7 @@
|
|
|
33
34
|
},
|
|
34
35
|
"./locales/*": "./dist/locales/*.json"
|
|
35
36
|
},
|
|
37
|
+
"prettier": "@mielk/lib-config/prettier",
|
|
36
38
|
"repository": {
|
|
37
39
|
"type": "git",
|
|
38
40
|
"url": "git+https://github.com/mielk/mielk-db.git"
|
|
@@ -52,7 +54,7 @@
|
|
|
52
54
|
],
|
|
53
55
|
"devDependencies": {
|
|
54
56
|
"@arethetypeswrong/cli": "0.18.5",
|
|
55
|
-
"@
|
|
57
|
+
"@mielk/lib-config": "1.0.0",
|
|
56
58
|
"@types/node": "24.19.2",
|
|
57
59
|
"@types/pg": "8.20.0",
|
|
58
60
|
"@vitest/coverage-v8": "5.1.0",
|
|
@@ -62,7 +64,6 @@
|
|
|
62
64
|
"prettier": "3.10.1",
|
|
63
65
|
"publint": "0.3.25",
|
|
64
66
|
"typescript": "5.9.2",
|
|
65
|
-
"typescript-eslint": "8.72.2",
|
|
66
67
|
"vitest": "5.1.0"
|
|
67
68
|
},
|
|
68
69
|
"peerDependencies": {
|