@mikro-orm/postgresql 7.2.6-dev.4 → 7.2.6-dev.40

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.
@@ -15,8 +15,9 @@ export declare class PostgreSqlConnection extends AbstractSqlConnection {
15
15
  * Both of the paths that emit one are unguarded by default: `pg-pool` removes its own listener
16
16
  * from a client while that client is checked out, deliberately leaving errors to whoever holds it,
17
17
  * and it re-emits errors of idle clients on the pool itself. The query in flight still rejects on
18
- * its own, so there is nothing to do here beyond logging — the pool has already discarded the
19
- * broken client by the time we see it.
18
+ * its own. An idle client is already discarded by the pool by the time we see it, a checked-out one
19
+ * is released here, as kysely's `ControlledTransaction` only releases after a commit or a rollback,
20
+ * which a transaction left open on a dead connection may never get.
20
21
  */
21
22
  private handlePoolErrors;
22
23
  /** Applies the active EM's session context (or resets it) on every pooled connection acquire. */
@@ -1,15 +1,36 @@
1
- import { Pool, TypeOverrides } from 'pg';
1
+ import { Client, Pool, TypeOverrides } from 'pg';
2
2
  import Cursor from 'pg-cursor';
3
3
  import { CompiledQuery, PostgresDialect } from 'kysely';
4
4
  import array from 'postgres-array';
5
5
  import { AbstractSqlConnection, createPostgreSqlTypeParsers, Utils } from '@mikro-orm/sql';
6
+ /**
7
+ * PostgreSQL answers `COMMIT` of an aborted transaction with the command tag `ROLLBACK` instead of an error,
8
+ * and kysely drops the tag, so the failed commit is rejected here, like any other commit the server refused.
9
+ */
10
+ class PostgreSqlClient extends Client {
11
+ query(...args) {
12
+ const res = super.query.apply(this, args);
13
+ // kysely sends a top-level commit as exactly `commit`, a callback-style call returns nothing
14
+ if (args[0] !== 'commit' || !res) {
15
+ return res;
16
+ }
17
+ return res.then((result) => {
18
+ if (result.command === 'ROLLBACK') {
19
+ throw Object.assign(new Error('current transaction is aborted, COMMIT was answered with ROLLBACK'), {
20
+ code: '25P02',
21
+ });
22
+ }
23
+ return result;
24
+ });
25
+ }
26
+ }
6
27
  /** PostgreSQL database connection using the `pg` driver. */
7
28
  export class PostgreSqlConnection extends AbstractSqlConnection {
8
29
  #pool;
9
30
  createKyselyDialect(overrides) {
10
31
  const { onPoolCreated, ...poolOverrides } = (overrides ?? {});
11
32
  const options = this.mapOptions(poolOverrides);
12
- const pool = new Pool(options);
33
+ const pool = new Pool({ Client: PostgreSqlClient, ...options });
13
34
  this.#pool = pool;
14
35
  this.handlePoolErrors(pool);
15
36
  void onPoolCreated?.(pool);
@@ -35,12 +56,24 @@ export class PostgreSqlConnection extends AbstractSqlConnection {
35
56
  * Both of the paths that emit one are unguarded by default: `pg-pool` removes its own listener
36
57
  * from a client while that client is checked out, deliberately leaving errors to whoever holds it,
37
58
  * and it re-emits errors of idle clients on the pool itself. The query in flight still rejects on
38
- * its own, so there is nothing to do here beyond logging — the pool has already discarded the
39
- * broken client by the time we see it.
59
+ * its own. An idle client is already discarded by the pool by the time we see it, a checked-out one
60
+ * is released here, as kysely's `ControlledTransaction` only releases after a commit or a rollback,
61
+ * which a transaction left open on a dead connection may never get.
40
62
  */
41
63
  handlePoolErrors(pool) {
64
+ const checkedOut = new WeakSet();
65
+ pool.on('acquire', client => checkedOut.add(client));
66
+ pool.on('release', (_err, client) => checkedOut.delete(client));
42
67
  pool.on('connect', client => {
43
- client.on('error', e => this.logger.warn('info', `PostgreSQL connection error: ${e.message}`));
68
+ client.on('error', e => {
69
+ this.logger.warn('info', `PostgreSQL connection error: ${e.message}`);
70
+ // kysely still releases a client whose plain query failed, and `pg-pool` throws on a second release
71
+ if (checkedOut.has(client)) {
72
+ const release = client.release;
73
+ client.release = () => undefined;
74
+ release(e);
75
+ }
76
+ });
44
77
  });
45
78
  pool.on('error', e => this.logger.warn('info', `PostgreSQL pool error: ${e.message}`));
46
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/postgresql",
3
- "version": "7.2.6-dev.4",
3
+ "version": "7.2.6-dev.40",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -47,8 +47,8 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "@mikro-orm/sql": "7.2.6-dev.4",
51
- "kysely": "0.29.6",
50
+ "@mikro-orm/sql": "7.2.6-dev.40",
51
+ "kysely": "0.29.7",
52
52
  "pg": "8.23.1",
53
53
  "pg-cursor": "2.22.1",
54
54
  "postgres-array": "3.0.4",
@@ -59,7 +59,7 @@
59
59
  "@mikro-orm/core": "^7.2.5"
60
60
  },
61
61
  "peerDependencies": {
62
- "@mikro-orm/core": "7.2.6-dev.4"
62
+ "@mikro-orm/core": "7.2.6-dev.40"
63
63
  },
64
64
  "engines": {
65
65
  "node": ">= 22.17.0"