postgrejs 3.14.0 → 3.14.2

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/README.md CHANGED
@@ -62,35 +62,40 @@ PostgreJS is a driver, not a framework, so what you build on top of it stays you
62
62
  <a href="https://www.sqbjs.org"><img src="https://www.postgrejs.com/img/sqb-logo.svg" alt="SQB" height="60"></a><br><br>
63
63
  <b><a href="https://www.sqbjs.org">SQB</a></b><br>
64
64
  <a href="https://www.npmjs.com/package/@sqb/postgres"><code>@sqb/postgres</code></a><br>
65
- <sub>first-party adapter</sub>
65
+ <sub>built-in adapter</sub>
66
+ </td>
67
+ <td align="center" width="230" valign="top">
68
+ <a href="https://joist-orm.io"><img src="https://www.postgrejs.com/img/joist-logo.svg" alt="Joist" height="60"></a><br><br>
69
+ <b><a href="https://joist-orm.io">Joist</a></b><br>
70
+ <a href="https://www.npmjs.com/package/joist-orm"><code>joist-orm/postgrejs</code></a><br>
71
+ <sub>built-in driver</sub>
66
72
  </td>
67
73
  <td align="center" width="230" valign="top">
68
74
  <a href="https://kysely.dev"><img src="https://www.postgrejs.com/img/kysely-logo.svg" alt="Kysely" height="60"></a><br><br>
69
75
  <b><a href="https://kysely.dev">Kysely</a></b><br>
70
76
  <a href="https://github.com/panates/postgrejs-kysely"><code>kysely-postgrejs</code></a><br>
71
- <sub>dialect</sub>
77
+ <sub>third-party dialect</sub>
72
78
  </td>
79
+ </tr>
80
+ <tr>
73
81
  <td align="center" width="230" valign="top">
74
82
  <a href="https://orm.drizzle.team"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.postgrejs.com/img/drizzle-logo-dark.svg"><img src="https://www.postgrejs.com/img/drizzle-logo-light.svg" alt="Drizzle ORM" height="34"></picture></a><br><br>
75
83
  <b><a href="https://orm.drizzle.team">Drizzle ORM</a></b><br>
76
84
  <a href="https://github.com/panates/postgrejs-drizzle"><code>drizzle-postgrejs</code></a><br>
77
- <sub>driver</sub>
85
+ <sub>third-party driver</sub>
78
86
  </td>
79
- </tr>
80
- <tr>
81
87
  <td align="center" width="230" valign="top">
82
88
  <a href="https://typeorm.io"><img src="https://www.postgrejs.com/img/typeorm-icon.png" alt="TypeORM" height="60"></a><br><br>
83
89
  <b><a href="https://typeorm.io">TypeORM</a></b><br>
84
90
  <a href="https://github.com/panates/postgrejs-typeorm"><code>typeorm-postgrejs</code></a><br>
85
- <sub>driver</sub>
91
+ <sub>third-party driver</sub>
86
92
  </td>
87
93
  <td align="center" width="230" valign="top">
88
94
  <a href="https://www.prisma.io"><img src="https://www.postgrejs.com/img/prisma-icon.svg" alt="Prisma" height="60"></a><br><br>
89
95
  <b><a href="https://www.prisma.io">Prisma</a></b><br>
90
96
  <a href="https://github.com/panates/postgrejs-prisma"><code>prisma-postgrejs</code></a><br>
91
- <sub>driver adapter</sub>
97
+ <sub>third-party adapter</sub>
92
98
  </td>
93
- <td width="230"></td>
94
99
  </tr>
95
100
  </table>
96
101
 
@@ -98,6 +103,9 @@ Each adapter hands the compiled SQL straight to this client, with no `pg` left a
98
103
  compared query by query against the driver it replaces - and, where the project ships a suite of its own, run
99
104
  against that too. So adopting one is a change to how the connection is created and nothing else.
100
105
 
106
+ Joist's is the Joist project's own, and uses `RowDecoder` to leave each cell undecoded until an entity reads it.
107
+ Codegen and migrations there still run on `pg`, though the runtime does not.
108
+
101
109
  ### 🌍 Runs Where You Run
102
110
 
103
111
  <!-- Same card shape as the adapters above, for the same reason: GitHub strips CSS, and a table
@@ -141,7 +149,7 @@ Upgrading from 3.5? See [doc/MIGRATION-v3.5-to-v3.6.md](doc/MIGRATION-v3.5-to-v3
141
149
 
142
150
  - **Language:** Pure JavaScript, with no native/binary dependencies to compile or ship.
143
151
  - **Strictly typed:** Written entirely in TypeScript, with types shipped alongside the package.
144
- - **Modern module format:** Ships as ESM; Node 20.19+/22.12+ can `require()` it from CommonJS code as well.
152
+ - **Modern module format:** Ships as ESM; Node 22.12+ can `require()` it from CommonJS code as well.
145
153
  - **Promise-based API:** Every asynchronous operation returns a promise - no callbacks to wrangle.
146
154
  - **Rigorously tested:** A test suite covering the wire protocol, every data type, and connection-handling edge case,
147
155
  run on every push against PostgreSQL 12 through 18.
@@ -324,10 +332,10 @@ PostgreJS implements the full PostgreSQL wire protocol from scratch, with no dep
324
332
  [`doc/BENCHMARKS.md`](doc/BENCHMARKS.md) compares it against `pg` (node-postgres) and `postgres` (postgres.js)
325
333
  across connection setup, simple/prepared queries, mixed-type decoding, bulk fetches, cursor streaming and pool
326
334
  concurrency, each library run through its own idiomatic fast path. The numbers there are reproducible on your own
327
- machine via `npm run bench` against the repo's own
335
+ machine via `pnpm run bench` against the repo's own
328
336
  `docker/docker-compose.yml` Postgres instance; see [`benchmark/README.md`](./benchmark/README.md) for details.
329
337
 
330
- The same suite runs under Bun via `npm run bench:bun`, where it also measures Bun's own built-in `Bun.sql` client as
338
+ The same suite runs under Bun via `pnpm run bench:bun`, where it also measures Bun's own built-in `Bun.sql` client as
331
339
  a fourth library. Those results live in [`doc/BENCHMARKS-bun.md`](doc/BENCHMARKS-bun.md) and are deliberately kept
332
340
  in a separate report rather than merged into the Node one: GC instrumentation, cursor streaming and binary-format
333
341
  control all differ enough between the two runtimes that a single table would conflate a library difference with a
@@ -344,7 +352,7 @@ you open an issue please provide version of NodeJS and PostgreSQL server.
344
352
 
345
353
  ## Runtime Compatibility
346
354
 
347
- - Node.js >= 20.x
355
+ - Node.js >= 22.x
348
356
  - Bun - the same test suite runs under Bun in CI against PostgreSQL 18 on every push, so Bun is a supported target
349
357
  rather than an untested coincidence. (Coverage is collected on the Node matrix only: `c8` instruments V8's
350
358
  coverage APIs, which JavaScriptCore doesn't have.)
@@ -64,6 +64,12 @@ export declare class IntlConnection extends SafeEventEmitter {
64
64
  * never close again.
65
65
  */
66
66
  private _preparing;
67
+ /**
68
+ * Resolves when the statement submitted before this one has written its
69
+ * own bytes - see _holdWireOrder(). Undefined whenever nothing is on its
70
+ * way to the wire, which is every statement that writes without waiting.
71
+ */
72
+ private _wireOrder?;
67
73
  /** SQL seen once but not yet prepared - see PREPARE_AFTER_USES. */
68
74
  private _preparedCandidates;
69
75
  private _preparedCounter;
@@ -313,6 +319,18 @@ export declare class IntlConnection extends SafeEventEmitter {
313
319
  protected _queryCached(sql: string, paramTypes: Maybe<Maybe<OID>[]>, params: Maybe<Maybe<any>[]>, options: QueryOptions, savepoint: Maybe<string>,
314
320
  /** Whether the CALL asked for text columns, not the connection. */
315
321
  callWantsFields: boolean): Promise<QueryResult>;
322
+ /**
323
+ * Keeps this statement's place in the wire order while it waits for
324
+ * something - today, the Parse round trip that names it.
325
+ *
326
+ * @returns `after`, the place held by the statement submitted before this
327
+ * one, to wait on before writing; and `wrote`, to call the moment this
328
+ * statement's own bytes are on the wire.
329
+ */
330
+ protected _holdWireOrder(): {
331
+ after?: Promise<void>;
332
+ wrote: () => void;
333
+ };
316
334
  /**
317
335
  * Gives this SQL a name and caches it, once, however many callers are
318
336
  * waiting. A Parse that fails caches nothing and reports itself; the
@@ -195,6 +195,12 @@ export class IntlConnection extends SafeEventEmitter {
195
195
  * never close again.
196
196
  */
197
197
  _preparing = new Map();
198
+ /**
199
+ * Resolves when the statement submitted before this one has written its
200
+ * own bytes - see _holdWireOrder(). Undefined whenever nothing is on its
201
+ * way to the wire, which is every statement that writes without waiting.
202
+ */
203
+ _wireOrder;
198
204
  /** SQL seen once but not yet prepared - see PREPARE_AFTER_USES. */
199
205
  _preparedCandidates = new Map();
200
206
  _preparedCounter = 0;
@@ -975,8 +981,11 @@ export class IntlConnection extends SafeEventEmitter {
975
981
  /** Whether the CALL asked for text columns, not the connection. */
976
982
  callWantsFields) {
977
983
  const enabled = options.prepare ?? this.config.prepare ?? true;
978
- if (!enabled)
984
+ if (!enabled) {
985
+ if (this._wireOrder)
986
+ await this._wireOrder;
979
987
  return this.queryOnce(sql, paramTypes, params, options, savepoint);
988
+ }
980
989
  const key = preparedCacheKey(sql, paramTypes);
981
990
  const cached = this._preparedCache.get(key);
982
991
  if (cached) {
@@ -984,6 +993,8 @@ export class IntlConnection extends SafeEventEmitter {
984
993
  this._preparedCache.delete(key);
985
994
  this._preparedCache.set(key, cached);
986
995
  try {
996
+ if (this._wireOrder)
997
+ await this._wireOrder;
987
998
  return await this.executeReused(cached.name, cached.fields, resolvedBindTypes(paramTypes, params, cached.resolvedParamTypes), params, options, savepoint);
988
999
  }
989
1000
  catch (e) {
@@ -1024,8 +1035,11 @@ export class IntlConnection extends SafeEventEmitter {
1024
1035
  // on a statement's first run instead, and per-column codes from its
1025
1036
  // second, which is where the name arrives anyway.
1026
1037
  const needsFields = callWantsFields && options.columnFormat !== DataFormat.text;
1027
- if (!needsFields && !this._earnsAName(key))
1038
+ if (!needsFields && !this._earnsAName(key)) {
1039
+ if (this._wireOrder)
1040
+ await this._wireOrder;
1028
1041
  return this.queryOnce(sql, paramTypes, params, options, savepoint);
1042
+ }
1029
1043
  let preparing = this._preparing.get(key);
1030
1044
  if (!preparing) {
1031
1045
  // Registered before the first await inside, so everything that
@@ -1038,10 +1052,71 @@ export class IntlConnection extends SafeEventEmitter {
1038
1052
  // Someone else is already naming it. Waiting would cost a round
1039
1053
  // trip the one-shot path does not - the name is for the calls
1040
1054
  // after this burst, not for this one.
1055
+ if (this._wireOrder)
1056
+ await this._wireOrder;
1041
1057
  return this.queryOnce(sql, paramTypes, params, options, savepoint);
1042
1058
  }
1043
- const entry = await preparing;
1044
- return this.executeReused(entry.name, entry.fields, paramTypes, params, options, savepoint);
1059
+ const place = this._holdWireOrder();
1060
+ let running;
1061
+ try {
1062
+ const entry = await preparing;
1063
+ if (place.after)
1064
+ await place.after;
1065
+ /* Called, not awaited: executeReused() runs as far as the socket
1066
+ write before its first await, so by the time it hands back a
1067
+ promise the bytes are away and the next statement may follow
1068
+ them. Awaiting it here instead would hold the line until this
1069
+ statement had its answer, which is pipelining switched off. */
1070
+ running = this.executeReused(entry.name, entry.fields, paramTypes, params, options, savepoint);
1071
+ }
1072
+ finally {
1073
+ // Also on the way out of a Parse that failed: this statement will
1074
+ // never write, and whatever queued behind it must not wait forever.
1075
+ place.wrote();
1076
+ }
1077
+ return running;
1078
+ }
1079
+ /**
1080
+ * Keeps this statement's place in the wire order while it waits for
1081
+ * something - today, the Parse round trip that names it.
1082
+ *
1083
+ * @returns `after`, the place held by the statement submitted before this
1084
+ * one, to wait on before writing; and `wrote`, to call the moment this
1085
+ * statement's own bytes are on the wire.
1086
+ */
1087
+ /* PostgreSQL executes what reaches it in the order it reaches it, and
1088
+ cannot know what order the query() calls were made in. A statement
1089
+ that has to await a Parse before it can Bind therefore lets every
1090
+ later statement overtake it - invisibly, because the overtaking only
1091
+ happens when one statement is cached and the other is not, which is a
1092
+ property of the connection's history rather than of the code. Two
1093
+ updates to one row then land in the opposite order on a connection
1094
+ that had been running them correctly all along.
1095
+
1096
+ Holding a place rather than the wire: the Parse still overlaps with
1097
+ everything else, which is the point of pipelining. Only the Bind is
1098
+ made to queue, and only behind calls that really were submitted
1099
+ first.
1100
+
1101
+ Nothing pending leaves `_wireOrder` undefined, so a statement that
1102
+ writes without waiting - a cache hit, the common case - reads one
1103
+ field and writes, paying no promise and no microtask for the
1104
+ ordering. */
1105
+ _holdWireOrder() {
1106
+ const after = this._wireOrder;
1107
+ let resolve;
1108
+ const mine = new Promise(r => (resolve = r));
1109
+ this._wireOrder = mine;
1110
+ return {
1111
+ after,
1112
+ wrote: () => {
1113
+ // Only if nobody queued behind this one in the meantime - their
1114
+ // place is the one that must be waited on now.
1115
+ if (this._wireOrder === mine)
1116
+ this._wireOrder = undefined;
1117
+ resolve();
1118
+ },
1119
+ };
1045
1120
  }
1046
1121
  /**
1047
1122
  * Gives this SQL a name and caches it, once, however many callers are
package/constants.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Protocol } from './protocol/protocol.js';
2
2
  import DataFormat = Protocol.DataFormat;
3
- export declare const version = "3.14.0";
3
+ export declare const version = "3.14.2";
4
4
  export { DataFormat };
5
5
  /**
6
6
  * What columns come back in unless something asks otherwise.
package/constants.js CHANGED
@@ -2,7 +2,7 @@ import { Protocol } from './protocol/protocol.js';
2
2
  var DataFormat = Protocol.DataFormat;
3
3
  // @ts-ignore
4
4
  Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');
5
- export const version = '3.14.0';
5
+ export const version = '3.14.2';
6
6
  export { DataFormat };
7
7
  /**
8
8
  * What columns come back in unless something asks otherwise.
@@ -1,5 +1,12 @@
1
1
  import type { DataType } from '../interfaces/data-type.js';
2
- /** `bytea`, as a `Buffer`. The binary format is the bytes themselves; the text one is `\\x`-prefixed hex, at two characters a byte. */
2
+ /**
3
+ * `bytea`. A value read back is a `Buffer`; a value sent may be any
4
+ * ArrayBuffer view - a `Buffer`, a `Uint8Array` off `fetch` or `subarray`,
5
+ * a `DataView` - and only the bytes that view covers are sent.
6
+ *
7
+ * The binary format is the bytes themselves; the text one is `\\x`-prefixed
8
+ * hex, at two characters a byte.
9
+ */
3
10
  export declare const ByteaType: DataType;
4
11
  /** The `_bytea` array of {@link ByteaType}. */
5
12
  export declare const ArrayByteaType: DataType;
@@ -1,16 +1,42 @@
1
1
  import { DataTypeOIDs } from '../constants.js';
2
2
  import { parseBytea, parseByteaBuffer } from '../util/parse-bytea.js';
3
- /** `bytea`, as a `Buffer`. The binary format is the bytes themselves; the text one is `\\x`-prefixed hex, at two characters a byte. */
3
+ /**
4
+ * Reads any ArrayBuffer view as a `Buffer` over exactly the bytes it covers.
5
+ *
6
+ * @param v The view to read.
7
+ * @returns It unchanged when it is already a `Buffer`, or a window onto the
8
+ * same memory - never a copy.
9
+ */
10
+ /* `byteOffset`/`byteLength` are the whole point: a `subarray()` of a larger
11
+ buffer describes a window, and `Buffer.from(view.buffer)` would send
12
+ everything on both sides of it. */
13
+ function asBuffer(v) {
14
+ return Buffer.isBuffer(v)
15
+ ? v
16
+ : Buffer.from(v.buffer, v.byteOffset, v.byteLength);
17
+ }
18
+ /**
19
+ * `bytea`. A value read back is a `Buffer`; a value sent may be any
20
+ * ArrayBuffer view - a `Buffer`, a `Uint8Array` off `fetch` or `subarray`,
21
+ * a `DataView` - and only the bytes that view covers are sent.
22
+ *
23
+ * The binary format is the bytes themselves; the text one is `\\x`-prefixed
24
+ * hex, at two characters a byte.
25
+ */
4
26
  export const ByteaType = {
5
27
  name: 'bytea',
6
28
  oid: DataTypeOIDs.bytea,
7
29
  jsType: 'Buffer',
8
30
  // The hex form PostgreSQL has emitted by default since 9.0.
9
31
  encodeText(v) {
10
- return '\\x' + v.toString('hex');
32
+ return '\\x' + asBuffer(v).toString('hex');
11
33
  },
34
+ /* Through `asBuffer` rather than straight to `writeBytes`, which reaches
35
+ for `.copy()` - a `Buffer` method that a plain `Uint8Array` does not
36
+ have, and the `buffer.copy is not a function` an explicit
37
+ `BindParam(17, bytes)` used to fail with. */
12
38
  encodeBinary(buf, v) {
13
- buf.writeBytes(v);
39
+ buf.writeBytes(asBuffer(v));
14
40
  },
15
41
  decodeBinary(v, offset = 0, len) {
16
42
  // A view, not a copy - the value handed back is the caller's own
@@ -20,7 +46,7 @@ export const ByteaType = {
20
46
  decodeText: parseBytea,
21
47
  decodeTextBuffer: parseByteaBuffer,
22
48
  isType(v) {
23
- return v instanceof Buffer;
49
+ return ArrayBuffer.isView(v);
24
50
  },
25
51
  };
26
52
  /** The `_bytea` array of {@link ByteaType}. */
@@ -37,9 +37,14 @@ export interface QueryOptions extends DataMappingOptions {
37
37
  *
38
38
  * `false` asks for the wire to itself: the statement waits until
39
39
  * nothing else is running on the connection, and nothing else starts
40
- * until it finishes, whichever mode those asked for. Nothing orders
41
- * two pipelined statements against each other, so this is how a
42
- * statement that depends on what another one left behind says so.
40
+ * until it finishes, whichever mode those asked for. It is not how a
41
+ * statement says it depends on an earlier one: pipelined statements on
42
+ * one connection execute in the order they were submitted. What this
43
+ * is for is a statement that needs the connection to itself for longer
44
+ * than the wire - session state another caller would see, or a result
45
+ * the caller has to read before it can decide what to send next.
46
+ * Statements on different pooled connections have no order between
47
+ * them at all.
43
48
  *
44
49
  * Some statements never share a *pooled* connection however this is
45
50
  * set, because they need one to themselves for longer than the call:
@@ -32,9 +32,14 @@ export interface ScriptExecuteOptions extends DataMappingOptions {
32
32
  *
33
33
  * `false` asks for the wire to itself: the statement waits until
34
34
  * nothing else is running on the connection, and nothing else starts
35
- * until it finishes, whichever mode those asked for. Nothing orders
36
- * two pipelined statements against each other, so this is how a
37
- * statement that depends on what another one left behind says so.
35
+ * until it finishes, whichever mode those asked for. It is not how a
36
+ * statement says it depends on an earlier one: pipelined statements on
37
+ * one connection execute in the order they were submitted. What this
38
+ * is for is a statement that needs the connection to itself for longer
39
+ * than the wire - session state another caller would see, or a result
40
+ * the caller has to read before it can decide what to send next.
41
+ * Statements on different pooled connections have no order between
42
+ * them at all.
38
43
  *
39
44
  * Some statements never share a *pooled* connection however this is
40
45
  * set, because they need one to themselves for longer than the call:
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "postgrejs",
3
3
  "description": "Professional PostgreSQL client NodeJS",
4
- "version": "3.14.0",
4
+ "version": "3.14.2",
5
5
  "author": "Panates",
6
6
  "license": "BSD-3-Clause",
7
7
  "dependencies": {
@@ -12,7 +12,7 @@
12
12
  "power-tasks": "^1.14.2",
13
13
  "putil-promisify": "^1.10.1",
14
14
  "putil-varhelpers": "^1.7.0",
15
- "tslib": "^2.8.1"
15
+ "tslib": "^2.9.1"
16
16
  },
17
17
  "type": "module",
18
18
  "module": "./index.js",
@@ -25,7 +25,7 @@
25
25
  "./package.json": "./package.json"
26
26
  },
27
27
  "engines": {
28
- "node": ">=20.0"
28
+ "node": ">=22.0"
29
29
  },
30
30
  "homepage": "https://www.postgrejs.com/",
31
31
  "contributors": [
@@ -36,7 +36,7 @@
36
36
  ],
37
37
  "repository": {
38
38
  "type": "git",
39
- "url": "https://github.com/panates/postgrejs.git"
39
+ "url": "git+https://github.com/panates/postgrejs.git"
40
40
  },
41
41
  "keywords": [
42
42
  "postgresql",
@@ -65,4 +65,4 @@
65
65
  "publishConfig": {
66
66
  "access": "public"
67
67
  }
68
- }
68
+ }