joist-orm 2.3.0-next.7 → 2.3.0-next.71

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.
Files changed (104) hide show
  1. package/build/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/build/_virtual/_rolldown/runtime.js +5 -0
  3. package/build/codegen.cjs +19 -0
  4. package/build/codegen.cjs.map +1 -0
  5. package/build/codegen.d.cts +1 -0
  6. package/build/codegen.d.mts +1 -0
  7. package/build/codegen.js +12 -28
  8. package/build/codegen.js.map +1 -1
  9. package/build/drivers/PostgresDriver.cjs +299 -0
  10. package/build/drivers/PostgresDriver.cjs.map +1 -0
  11. package/build/drivers/PostgresDriver.d.cts +81 -0
  12. package/build/drivers/PostgresDriver.d.cts.map +1 -0
  13. package/build/drivers/PostgresDriver.d.mts +81 -0
  14. package/build/drivers/PostgresDriver.d.mts.map +1 -0
  15. package/build/drivers/PostgresDriver.js +262 -274
  16. package/build/drivers/PostgresDriver.js.map +1 -1
  17. package/build/drivers/WireRowData.cjs +374 -0
  18. package/build/drivers/WireRowData.cjs.map +1 -0
  19. package/build/drivers/WireRowData.d.cts +111 -0
  20. package/build/drivers/WireRowData.d.cts.map +1 -0
  21. package/build/drivers/WireRowData.d.mts +111 -0
  22. package/build/drivers/WireRowData.d.mts.map +1 -0
  23. package/build/drivers/WireRowData.js +370 -0
  24. package/build/drivers/WireRowData.js.map +1 -0
  25. package/build/drivers/binaryParsers.cjs +422 -0
  26. package/build/drivers/binaryParsers.cjs.map +1 -0
  27. package/build/drivers/binaryParsers.d.cts +80 -0
  28. package/build/drivers/binaryParsers.d.cts.map +1 -0
  29. package/build/drivers/binaryParsers.d.mts +80 -0
  30. package/build/drivers/binaryParsers.d.mts.map +1 -0
  31. package/build/drivers/binaryParsers.js +412 -0
  32. package/build/drivers/binaryParsers.js.map +1 -0
  33. package/build/drivers/patchPgProtocol.cjs +255 -0
  34. package/build/drivers/patchPgProtocol.cjs.map +1 -0
  35. package/build/drivers/patchPgProtocol.d.cts +47 -0
  36. package/build/drivers/patchPgProtocol.d.cts.map +1 -0
  37. package/build/drivers/patchPgProtocol.d.mts +47 -0
  38. package/build/drivers/patchPgProtocol.d.mts.map +1 -0
  39. package/build/drivers/patchPgProtocol.js +253 -0
  40. package/build/drivers/patchPgProtocol.js.map +1 -0
  41. package/build/graphql-codegen-export.cjs +10 -0
  42. package/build/graphql-codegen-export.d.cts +1 -0
  43. package/build/graphql-codegen-export.d.mts +1 -0
  44. package/build/graphql-codegen-export.js +2 -19
  45. package/build/graphql-export.cjs +10 -0
  46. package/build/graphql-export.d.cts +1 -0
  47. package/build/graphql-export.d.mts +1 -0
  48. package/build/graphql-export.js +2 -19
  49. package/build/index.cjs +41 -0
  50. package/build/index.d.cts +3 -0
  51. package/build/index.d.mts +3 -0
  52. package/build/index.js +3 -27
  53. package/build/knex-export.cjs +10 -0
  54. package/build/knex-export.d.cts +1 -0
  55. package/build/knex-export.d.mts +1 -0
  56. package/build/knex-export.js +2 -19
  57. package/build/pg-export.cjs +13 -0
  58. package/build/pg-export.d.cts +4 -0
  59. package/build/pg-export.d.mts +4 -0
  60. package/build/pg-export.js +4 -9
  61. package/build/pg-migrate.cjs +22 -0
  62. package/build/pg-migrate.cjs.map +1 -0
  63. package/build/pg-migrate.d.cts +1 -0
  64. package/build/pg-migrate.d.mts +1 -0
  65. package/build/pg-migrate.js +14 -28
  66. package/build/pg-migrate.js.map +1 -1
  67. package/build/seed.cjs +49 -0
  68. package/build/seed.cjs.map +1 -0
  69. package/build/{seed.d.ts → seed.d.cts} +5 -2
  70. package/build/seed.d.cts.map +1 -0
  71. package/build/seed.d.mts +24 -0
  72. package/build/seed.d.mts.map +1 -0
  73. package/build/seed.js +39 -45
  74. package/build/seed.js.map +1 -1
  75. package/build/tests-export.cjs +10 -0
  76. package/build/tests-export.d.cts +1 -0
  77. package/build/tests-export.d.mts +1 -0
  78. package/build/tests-export.js +2 -19
  79. package/package.json +154 -38
  80. package/build/codegen.d.ts +0 -2
  81. package/build/codegen.d.ts.map +0 -1
  82. package/build/drivers/PostgresDriver.d.ts +0 -49
  83. package/build/drivers/PostgresDriver.d.ts.map +0 -1
  84. package/build/graphql-codegen-export.d.ts +0 -2
  85. package/build/graphql-codegen-export.d.ts.map +0 -1
  86. package/build/graphql-codegen-export.js.map +0 -1
  87. package/build/graphql-export.d.ts +0 -2
  88. package/build/graphql-export.d.ts.map +0 -1
  89. package/build/graphql-export.js.map +0 -1
  90. package/build/index.d.ts +0 -4
  91. package/build/index.d.ts.map +0 -1
  92. package/build/index.js.map +0 -1
  93. package/build/knex-export.d.ts +0 -2
  94. package/build/knex-export.d.ts.map +0 -1
  95. package/build/knex-export.js.map +0 -1
  96. package/build/pg-export.d.ts +0 -3
  97. package/build/pg-export.d.ts.map +0 -1
  98. package/build/pg-export.js.map +0 -1
  99. package/build/pg-migrate.d.ts +0 -2
  100. package/build/pg-migrate.d.ts.map +0 -1
  101. package/build/seed.d.ts.map +0 -1
  102. package/build/tests-export.d.ts +0 -2
  103. package/build/tests-export.d.ts.map +0 -1
  104. package/build/tests-export.js.map +0 -1
@@ -0,0 +1,111 @@
1
+ import { RowData } from "joist-core";
2
+ import pg from "pg";
3
+ //#region src/drivers/WireRowData.d.ts
4
+ /**
5
+ * A lazy wire-row {@link RowData} over raw Postgres `DataRow` payload bytes.
6
+ *
7
+ * Each query produces its own `WireRowData` — one query, one result; results are never combined
8
+ * or appended across queries, and the payload is read-only after the query completes (entity
9
+ * mutations go into `InstanceData.data`, never back into the row bytes).
10
+ *
11
+ * Rows are kept in their row-major wire format (`int16 fieldCount` + length-prefixed cells) as
12
+ * zero-copy views over the parser's own immutable buffers: `addRow` records each DataRow's
13
+ * `(bytes, offset, length)` by reference, deduping the socket chunk that consecutive rows
14
+ * share. (Our pg-protocol patch guarantees message bytes are never rewritten — chunks parse in
15
+ * place and straddling messages get a buffer of their own; see `patchPgProtocol.ts`.) A
16
+ * `row × column` cell is decoded on first field access by scanning the row's length-prefixed
17
+ * cells to the column's ordinal ("row-lazy" decode, see JS-ROW-STORE-DESIGN.md §3/C2). This is
18
+ * deferred decoding of row-major data, not a columnar layout.
19
+ *
20
+ * Because chunks are whole socket reads, retaining any row pins its ~64KiB chunk (plus whatever
21
+ * protocol frames share it); `finalize`'s compaction copies retained rows out into an
22
+ * exact-size owned buffer when enough of the payload was dropped.
23
+ *
24
+ * Text-format cells go through the same active text parsers node-postgres would resolve for
25
+ * the query (i.e. honoring pool/client `TypeOverrides`), so they parse identically to classic
26
+ * rows. Binary-format cells decode via `wireBinaryParser`: wire bytes -> value directly for
27
+ * default-parsed scalar types (no intermediate string), else rendered to pg's canonical text
28
+ * and fed through the active text parser for parity. (Classic node-postgres cannot do binary
29
+ * at all — it round-trips cells through a UTF-8 string, corrupting bytes >= 0x80.)
30
+ *
31
+ * Because decoding is deferred, a custom parser that throws will do so on first field access
32
+ * (or `toRow`/`toRows`), not while awaiting the query; `id` and inheritance-discriminator cells
33
+ * still decode during hydration.
34
+ *
35
+ * After hydration, `finalize` trims unused capacity and, when some rows were not retained (i.e.
36
+ * their entities were already in the identity map), compacts the payload down to only the
37
+ * retained rows, so retained memory tracks live entities rather than query history.
38
+ *
39
+ * Small results deliberately stay lazy — there is NO row-count threshold below which we
40
+ * materialize to a `PojoRowData` instead. Measured (benchmark-rowdata-small.ts, 40-col rows):
41
+ * for the typical sparse access pattern (~6 of 40 columns read), keeping the lazy result wins at
42
+ * every size including a single row (n=1: 4.3µs vs 7.6µs; n=1000: 0.76ms vs 3.0ms), because
43
+ * materialization eagerly decodes every column while lazy faults only what is read. Even full
44
+ * column coverage no longer flips the winner: with binary decode and `#readCell`'s adaptive
45
+ * scan cursor, reading all ~36 columns of every row measures ~parity with classic end-to-end
46
+ * (benchmark-lazy-parsing.ts), and small finds (n <= 10) are statistically identical.
47
+ */
48
+ declare class WireRowData implements RowData {
49
+ #private;
50
+ get rowCount(): number;
51
+ /** The DataRow payload bytes currently indexed; drops when compaction discards rows. */
52
+ get payloadBytes(): number;
53
+ /** The bytes currently held by payload chunks + the row-slice table, i.e. for benchmarks. */
54
+ get memoryBytes(): number;
55
+ get(rowIndex: number, columnName: string): any;
56
+ /** Materializes one row as a POJO, i.e. for debugging and differential tests; values are not cached. */
57
+ toRow(rowIndex: number): any;
58
+ /** Materializes classic POJO rows, i.e. for `afterFind` observation or debugging; not cached. */
59
+ toRows(): any[];
60
+ /**
61
+ * Resolves each column's decoder from the query's RowDescription.
62
+ *
63
+ * Binary-format fields decode through Joist's own binary registry only (see
64
+ * `setBinaryTypeParser`) — `pg.types`/`TypeOverrides` are never consulted, and a column whose
65
+ * oid has no registered binary parser fails the query here, before any rows arrive, rather
66
+ * than guessing at a lossy decoding. Text-format fields (the `binary: false` escape hatch)
67
+ * decode by utf8-slicing the cell into the active text parser, resolved via `getTypeParser`
68
+ * (i.e. the pool/client `TypeOverrides` chain) or the global registry.
69
+ */
70
+ setRowDescription(fields: Array<{
71
+ name: string;
72
+ dataTypeID: number;
73
+ format?: string;
74
+ }>, getTypeParser?: (dataTypeID: number) => (value: any) => any): void;
75
+ /**
76
+ * Records one DataRow payload *by reference* (zero-copy); called synchronously from the wire
77
+ * parser, whose patched buffer management guarantees the bytes are never rewritten.
78
+ *
79
+ * Consecutive rows usually share one socket chunk, so `bytes` is deduped against the last
80
+ * chunk ref; retaining any row of a chunk pins the whole chunk, which `finalize`'s compaction
81
+ * resolves by copying retained rows out when enough of the payload was dropped.
82
+ */
83
+ addRow(bytes: Buffer, offset: number, payloadLength: number): void;
84
+ /** Marks `rowIndex` as retained by a hydrated entity; unmarked rows can be compacted away. */
85
+ retain(rowIndex: number): void;
86
+ /**
87
+ * Trims unused capacity, and compacts down to only `retain`-ed rows when enough rows were not
88
+ * retained to be worth the copy.
89
+ *
90
+ * Compaction exists to release pinned socket buffers to the GC: rows are zero-copy views into
91
+ * whole ~64KiB chunks, so keeping any row alive pins its entire chunk — and unretained rows
92
+ * are typically duplicates whose entities were *already in memory* (identity-map hits that
93
+ * keep their original `rowData`), making the newly-arrived bytes dead weight. Copying the
94
+ * retained rows into one exact-size owned buffer lets every chunk reference drop.
95
+ *
96
+ * The copy re-copies every retained byte, so it only pays off when it buys back a meaningful
97
+ * fraction of the payload: we compact when the dropped rows hold more than 20% of the payload
98
+ * bytes, and otherwise just trim, accepting the (bounded) leftover bytes. Called once after
99
+ * hydration + sidecar reads (`_tags`, preload aggregates) are complete; retained entities keep
100
+ * their original `rowIndex`, and un-compacted unretained rows simply remain readable-but-unused.
101
+ */
102
+ finalize(): void;
103
+ }
104
+ declare function executeRowDataQuery(client: pg.PoolClient, sql: string, bindings: readonly any[], opts?: {
105
+ binary?: boolean;
106
+ }): Promise<RowData>;
107
+ /** Returns whether `client` supports the lazy row-data query path, before submitting anything. */
108
+ declare function isRowDataCapableClient(client: unknown): client is pg.PoolClient;
109
+ //#endregion
110
+ export { WireRowData, executeRowDataQuery, isRowDataCapableClient };
111
+ //# sourceMappingURL=WireRowData.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WireRowData.d.mts","names":[],"sources":["../../src/drivers/WireRowData.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAkFa,uBAAuB;;MAa9B;;MAKA;;MAKA;EAMJ,IAAI,kBAAkB;;EAWtB,MAAM;;EAsBN;;;;;;;;;;;EAgBA,kBACE,QAAQ;IAAQ;IAAc;IAAoB;MAClD,iBAAiB,wBAAwB;;;;;;;;;EA+B3C,OAAO,OAAO,QAAQ,gBAAgB;;EAwBtC,OAAO;;;;;;;;;;;;;;;;;EAoBP;;iBAoIc,oBACd,QAAQ,GAAG,YACX,aACA,0BACA;EAAS;IACR,QAAQ;;iBAkBK,uBAAuB,kBAAkB,UAAU,GAAG"}
@@ -0,0 +1,370 @@
1
+ import { getBinaryTypeParser } from "./binaryParsers.js";
2
+ import { createRequire } from "node:module";
3
+ import "joist-core";
4
+ import pg from "pg";
5
+ //#region src/drivers/WireRowData.ts
6
+ const PgQuery = createRequire(import.meta.url)("pg/lib/query");
7
+ const ROW_STRIDE = 3;
8
+ /** The row-length sentinel for rows dropped by `finalize` compaction. */
9
+ const DROPPED = 4294967295;
10
+ /** Only compact when dropped rows hold more than this fraction of the payload bytes. */
11
+ const COMPACT_THRESHOLD = .2;
12
+ /**
13
+ * Only allocate the adaptive scan cursor once a fault targets at least this ordinal: shallower
14
+ * scans cost less than the cursor's bookkeeping, i.e. hydrate-only results (id is ordinal ~0)
15
+ * and narrow reads never pay the 6 bytes/row.
16
+ */
17
+ const SCAN_CURSOR_MIN_ORDINAL = 8;
18
+ /**
19
+ * A lazy wire-row {@link RowData} over raw Postgres `DataRow` payload bytes.
20
+ *
21
+ * Each query produces its own `WireRowData` — one query, one result; results are never combined
22
+ * or appended across queries, and the payload is read-only after the query completes (entity
23
+ * mutations go into `InstanceData.data`, never back into the row bytes).
24
+ *
25
+ * Rows are kept in their row-major wire format (`int16 fieldCount` + length-prefixed cells) as
26
+ * zero-copy views over the parser's own immutable buffers: `addRow` records each DataRow's
27
+ * `(bytes, offset, length)` by reference, deduping the socket chunk that consecutive rows
28
+ * share. (Our pg-protocol patch guarantees message bytes are never rewritten — chunks parse in
29
+ * place and straddling messages get a buffer of their own; see `patchPgProtocol.ts`.) A
30
+ * `row × column` cell is decoded on first field access by scanning the row's length-prefixed
31
+ * cells to the column's ordinal ("row-lazy" decode, see JS-ROW-STORE-DESIGN.md §3/C2). This is
32
+ * deferred decoding of row-major data, not a columnar layout.
33
+ *
34
+ * Because chunks are whole socket reads, retaining any row pins its ~64KiB chunk (plus whatever
35
+ * protocol frames share it); `finalize`'s compaction copies retained rows out into an
36
+ * exact-size owned buffer when enough of the payload was dropped.
37
+ *
38
+ * Text-format cells go through the same active text parsers node-postgres would resolve for
39
+ * the query (i.e. honoring pool/client `TypeOverrides`), so they parse identically to classic
40
+ * rows. Binary-format cells decode via `wireBinaryParser`: wire bytes -> value directly for
41
+ * default-parsed scalar types (no intermediate string), else rendered to pg's canonical text
42
+ * and fed through the active text parser for parity. (Classic node-postgres cannot do binary
43
+ * at all — it round-trips cells through a UTF-8 string, corrupting bytes >= 0x80.)
44
+ *
45
+ * Because decoding is deferred, a custom parser that throws will do so on first field access
46
+ * (or `toRow`/`toRows`), not while awaiting the query; `id` and inheritance-discriminator cells
47
+ * still decode during hydration.
48
+ *
49
+ * After hydration, `finalize` trims unused capacity and, when some rows were not retained (i.e.
50
+ * their entities were already in the identity map), compacts the payload down to only the
51
+ * retained rows, so retained memory tracks live entities rather than query history.
52
+ *
53
+ * Small results deliberately stay lazy — there is NO row-count threshold below which we
54
+ * materialize to a `PojoRowData` instead. Measured (benchmark-rowdata-small.ts, 40-col rows):
55
+ * for the typical sparse access pattern (~6 of 40 columns read), keeping the lazy result wins at
56
+ * every size including a single row (n=1: 4.3µs vs 7.6µs; n=1000: 0.76ms vs 3.0ms), because
57
+ * materialization eagerly decodes every column while lazy faults only what is read. Even full
58
+ * column coverage no longer flips the winner: with binary decode and `#readCell`'s adaptive
59
+ * scan cursor, reading all ~36 columns of every row measures ~parity with classic end-to-end
60
+ * (benchmark-lazy-parsing.ts), and small finds (n <= 10) are statistically identical.
61
+ */
62
+ var WireRowData = class {
63
+ #chunks = [];
64
+ /** Where each row's bytes live: `(chunk index, start, length)` slices, `ROW_STRIDE` entries per row. */
65
+ #rowSlices = /* @__PURE__ */ new Uint32Array(48);
66
+ #rowCount = 0;
67
+ #payloadBytes = 0;
68
+ #retained = void 0;
69
+ #columns = /* @__PURE__ */ new Map();
70
+ #fields = [];
71
+ /** The adaptive scan cursor: per row, the furthest scanned ordinal + its offset (see #readCell). */
72
+ #scanOrdinal = void 0;
73
+ #scanOffset = void 0;
74
+ get rowCount() {
75
+ return this.#rowCount;
76
+ }
77
+ /** The DataRow payload bytes currently indexed; drops when compaction discards rows. */
78
+ get payloadBytes() {
79
+ return this.#payloadBytes;
80
+ }
81
+ /** The bytes currently held by payload chunks + the row-slice table, i.e. for benchmarks. */
82
+ get memoryBytes() {
83
+ let bytes = this.#rowSlices.byteLength;
84
+ for (const chunk of this.#chunks) bytes += chunk.length;
85
+ return bytes;
86
+ }
87
+ get(rowIndex, columnName) {
88
+ const column = this.#columns.get(columnName);
89
+ if (column === void 0) return void 0;
90
+ const base = this.#rowBase(rowIndex);
91
+ const slices = this.#rowSlices;
92
+ const start = slices[base + 1];
93
+ return this.#readCell(this.#chunks[slices[base]], start, start + slices[base + 2], column, rowIndex);
94
+ }
95
+ /** Materializes one row as a POJO, i.e. for debugging and differential tests; values are not cached. */
96
+ toRow(rowIndex) {
97
+ const base = this.#rowBase(rowIndex);
98
+ const slices = this.#rowSlices;
99
+ const chunk = this.#chunks[slices[base]];
100
+ const start = slices[base + 1];
101
+ const end = start + slices[base + 2];
102
+ const row = {};
103
+ let pos = start + 2;
104
+ for (const field of this.#fields) {
105
+ const len = this.#cellLength(chunk, pos, end, rowIndex);
106
+ pos += 4;
107
+ if (len === -1) row[field.name] = null;
108
+ else {
109
+ row[field.name] = field.decode(chunk, pos, len);
110
+ pos += len;
111
+ }
112
+ }
113
+ return row;
114
+ }
115
+ /** Materializes classic POJO rows, i.e. for `afterFind` observation or debugging; not cached. */
116
+ toRows() {
117
+ const rows = new Array(this.#rowCount);
118
+ for (let i = 0; i < this.#rowCount; i++) rows[i] = this.toRow(i);
119
+ return rows;
120
+ }
121
+ /**
122
+ * Resolves each column's decoder from the query's RowDescription.
123
+ *
124
+ * Binary-format fields decode through Joist's own binary registry only (see
125
+ * `setBinaryTypeParser`) — `pg.types`/`TypeOverrides` are never consulted, and a column whose
126
+ * oid has no registered binary parser fails the query here, before any rows arrive, rather
127
+ * than guessing at a lossy decoding. Text-format fields (the `binary: false` escape hatch)
128
+ * decode by utf8-slicing the cell into the active text parser, resolved via `getTypeParser`
129
+ * (i.e. the pool/client `TypeOverrides` chain) or the global registry.
130
+ */
131
+ setRowDescription(fields, getTypeParser) {
132
+ for (let i = 0; i < fields.length; i++) {
133
+ const { name, dataTypeID, format } = fields[i];
134
+ let decode;
135
+ if (format === "binary") {
136
+ decode = getBinaryTypeParser(dataTypeID);
137
+ if (decode === void 0) throw new Error(`joist-orm: no binary type parser registered for oid ${dataTypeID} (column "${name}"); register one with setBinaryTypeParser (i.e. binaryTextParser for text-like types)`);
138
+ } else decode = textCellDecoder(getTypeParser?.(dataTypeID) ?? pg.types.getTypeParser(dataTypeID, "text"));
139
+ const column = {
140
+ name,
141
+ ordinal: i,
142
+ decode
143
+ };
144
+ this.#columns.set(name, column);
145
+ this.#fields.push(column);
146
+ }
147
+ }
148
+ /**
149
+ * Records one DataRow payload *by reference* (zero-copy); called synchronously from the wire
150
+ * parser, whose patched buffer management guarantees the bytes are never rewritten.
151
+ *
152
+ * Consecutive rows usually share one socket chunk, so `bytes` is deduped against the last
153
+ * chunk ref; retaining any row of a chunk pins the whole chunk, which `finalize`'s compaction
154
+ * resolves by copying retained rows out when enough of the payload was dropped.
155
+ */
156
+ addRow(bytes, offset, payloadLength) {
157
+ if (payloadLength < 2 || offset + payloadLength > bytes.length) throw new Error(`Malformed DataRow payload (length ${payloadLength})`);
158
+ let chunkIndex = this.#chunks.length - 1;
159
+ if (chunkIndex === -1 || this.#chunks[chunkIndex] !== bytes) {
160
+ this.#chunks.push(bytes);
161
+ chunkIndex++;
162
+ }
163
+ let slices = this.#rowSlices;
164
+ const base = this.#rowCount * ROW_STRIDE;
165
+ if (base === slices.length) {
166
+ const grown = new Uint32Array(slices.length * 2);
167
+ grown.set(slices);
168
+ slices = this.#rowSlices = grown;
169
+ }
170
+ slices[base] = chunkIndex;
171
+ slices[base + 1] = offset;
172
+ slices[base + 2] = payloadLength;
173
+ this.#rowCount++;
174
+ this.#payloadBytes += payloadLength;
175
+ }
176
+ /** Marks `rowIndex` as retained by a hydrated entity; unmarked rows can be compacted away. */
177
+ retain(rowIndex) {
178
+ (this.#retained ??= []).push(rowIndex);
179
+ }
180
+ /**
181
+ * Trims unused capacity, and compacts down to only `retain`-ed rows when enough rows were not
182
+ * retained to be worth the copy.
183
+ *
184
+ * Compaction exists to release pinned socket buffers to the GC: rows are zero-copy views into
185
+ * whole ~64KiB chunks, so keeping any row alive pins its entire chunk — and unretained rows
186
+ * are typically duplicates whose entities were *already in memory* (identity-map hits that
187
+ * keep their original `rowData`), making the newly-arrived bytes dead weight. Copying the
188
+ * retained rows into one exact-size owned buffer lets every chunk reference drop.
189
+ *
190
+ * The copy re-copies every retained byte, so it only pays off when it buys back a meaningful
191
+ * fraction of the payload: we compact when the dropped rows hold more than 20% of the payload
192
+ * bytes, and otherwise just trim, accepting the (bounded) leftover bytes. Called once after
193
+ * hydration + sidecar reads (`_tags`, preload aggregates) are complete; retained entities keep
194
+ * their original `rowIndex`, and un-compacted unretained rows simply remain readable-but-unused.
195
+ */
196
+ finalize() {
197
+ const retained = this.#retained ?? [];
198
+ this.#retained = void 0;
199
+ if (retained.length < this.#rowCount) {
200
+ let retainedBytes = 0;
201
+ for (const i of retained) retainedBytes += this.#rowSlices[i * ROW_STRIDE + 2];
202
+ if (this.#payloadBytes - retainedBytes > this.#payloadBytes * COMPACT_THRESHOLD) {
203
+ this.#compact(retained, retainedBytes);
204
+ return;
205
+ }
206
+ }
207
+ if (this.#rowCount * ROW_STRIDE < this.#rowSlices.length) this.#rowSlices = this.#rowSlices.slice(0, this.#rowCount * ROW_STRIDE);
208
+ }
209
+ /** Copies retained rows into one owned buffer, releasing the pinned socket chunks to the GC. */
210
+ #compact(retained, bytes) {
211
+ const chunks = bytes > 0 ? [Buffer.allocUnsafe(bytes)] : [];
212
+ const slices = new Uint32Array(this.#rowCount * ROW_STRIDE);
213
+ for (let i = 0; i < this.#rowCount; i++) slices[i * ROW_STRIDE + 2] = DROPPED;
214
+ let used = 0;
215
+ for (const i of retained) {
216
+ const base = i * ROW_STRIDE;
217
+ const source = this.#chunks[this.#rowSlices[base]];
218
+ const start = this.#rowSlices[base + 1];
219
+ const len = this.#rowSlices[base + 2];
220
+ source.copy(chunks[0], used, start, start + len);
221
+ slices[base] = 0;
222
+ slices[base + 1] = used;
223
+ slices[base + 2] = len;
224
+ used += len;
225
+ }
226
+ this.#chunks = chunks;
227
+ this.#rowSlices = slices;
228
+ this.#payloadBytes = used;
229
+ }
230
+ /** Validates `rowIndex` and returns its base index into the `#rowSlices` table. */
231
+ #rowBase(rowIndex) {
232
+ if (!(rowIndex >= 0 && rowIndex < this.#rowCount)) throw new Error(`Invalid rowIndex ${rowIndex} (rowCount ${this.#rowCount})`);
233
+ const base = rowIndex * ROW_STRIDE;
234
+ if (this.#rowSlices[base + 2] === DROPPED) throw new Error(`Row ${rowIndex} was compacted away (its entity was already loaded)`);
235
+ return base;
236
+ }
237
+ /**
238
+ * Scans a row's cells to `column`'s ordinal and decodes it.
239
+ *
240
+ * An adaptive per-row scan cursor caches the furthest cell boundary already scanned — the
241
+ * ordinal whose row-relative offset is known (`#scanOrdinal`/`#scanOffset`) — so ascending
242
+ * reads resume instead of re-scanning from the row start: a dense in-order read of all C
243
+ * columns costs one linear pass rather than O(C^2) length-prefix skips. Out-of-order
244
+ * (descending) faults simply scan from the start, i.e. never worse than without the cursor.
245
+ * The arrays are a fixed 6 bytes/row, allocated lazily on the first fault deep enough for
246
+ * resuming to matter, and — being row-relative — stay valid across compaction.
247
+ */
248
+ #readCell(chunk, start, end, column, rowIndex) {
249
+ const fieldCount = chunk.readInt16BE(start);
250
+ const { ordinal } = column;
251
+ if (ordinal >= fieldCount) throw new Error(`Row ${rowIndex} has ${fieldCount} cells but column ${column.name} is #${column.ordinal}`);
252
+ let pos = start + 2;
253
+ let c = 0;
254
+ let scanOrdinal = this.#scanOrdinal;
255
+ if (scanOrdinal === void 0 && ordinal >= SCAN_CURSOR_MIN_ORDINAL) {
256
+ scanOrdinal = this.#scanOrdinal = new Uint16Array(this.#rowCount);
257
+ this.#scanOffset = new Uint32Array(this.#rowCount);
258
+ }
259
+ if (scanOrdinal !== void 0 && rowIndex < scanOrdinal.length) {
260
+ const known = scanOrdinal[rowIndex];
261
+ if (known !== 0 && known <= ordinal) {
262
+ c = known;
263
+ pos = start + this.#scanOffset[rowIndex];
264
+ }
265
+ }
266
+ for (; c < ordinal; c++) {
267
+ const len = this.#cellLength(chunk, pos, end, rowIndex);
268
+ pos += len > 0 ? len + 4 : 4;
269
+ }
270
+ const len = this.#cellLength(chunk, pos, end, rowIndex);
271
+ if (scanOrdinal !== void 0 && rowIndex < scanOrdinal.length && ordinal + 1 > scanOrdinal[rowIndex]) {
272
+ scanOrdinal[rowIndex] = ordinal + 1;
273
+ this.#scanOffset[rowIndex] = pos + 4 + (len > 0 ? len : 0) - start;
274
+ }
275
+ if (len === -1) return null;
276
+ return column.decode(chunk, pos + 4, len);
277
+ }
278
+ /** Reads + validates one cell's length prefix. */
279
+ #cellLength(chunk, pos, end, rowIndex) {
280
+ if (pos + 4 > end) throw new Error(`Truncated DataRow payload in row ${rowIndex}`);
281
+ const len = chunk.readInt32BE(pos);
282
+ if (len < -1 || len > 0 && pos + 4 + len > end) throw new Error(`Malformed cell length ${len} in row ${rowIndex}`);
283
+ return len;
284
+ }
285
+ };
286
+ /**
287
+ * Executes `sql` on an already-checked-out client, returning a {@link RowData} instead of
288
+ * materialized POJO rows.
289
+ *
290
+ * Uses a `pg` Query subclass that records each DataRow's raw payload bytes into the result
291
+ * (via the lazy DataRow message from `patchPgProtocol`) and never materializes per-cell
292
+ * strings or per-row objects. If the client's connection turns out to use an unpatched
293
+ * pg-protocol copy (i.e. the app's pool was built from a different `pg` install than the one
294
+ * joist-orm patched), the query fails with a descriptive error — a misconfiguration any CI
295
+ * build/smoketest will surface immediately, so we fail loudly rather than silently degrade.
296
+ * The rows already streamed are discarded; the connection itself stays usable.
297
+ *
298
+ * By default the query requests *binary* result format (via the extended protocol), so scalar
299
+ * cells decode wire-bytes -> value with no intermediate strings; see `wireBinaryParser` for the
300
+ * parity strategy. Pass `binary: false` for classic text-format results, or set
301
+ * `JOIST_LAZY_BINARY=0` to flip the default while the binary path is a prototype (i.e. for
302
+ * A/B benchmarking or as an escape hatch).
303
+ */
304
+ const BINARY_BY_DEFAULT = process.env.JOIST_LAZY_BINARY !== "0";
305
+ function executeRowDataQuery(client, sql, bindings, opts) {
306
+ const binary = opts?.binary ?? BINARY_BY_DEFAULT;
307
+ return new Promise((resolve, reject) => {
308
+ const query = new RowDataQuery(binary ? {
309
+ text: sql,
310
+ values: bindings,
311
+ binary: true,
312
+ queryMode: "extended"
313
+ } : {
314
+ text: sql,
315
+ values: bindings
316
+ }, (err) => {
317
+ if (err) reject(err);
318
+ else resolve(query.rowData);
319
+ });
320
+ client.query(query);
321
+ });
322
+ }
323
+ /** Returns whether `client` supports the lazy row-data query path, before submitting anything. */
324
+ function isRowDataCapableClient(client) {
325
+ return typeof client === "object" && client !== null && typeof client.query === "function" && client.connection !== void 0 && client.native === void 0;
326
+ }
327
+ /** A pg Query that diverts DataRows into a {@link WireRowData} instead of a `Result`. */
328
+ var RowDataQuery = class extends PgQuery {
329
+ #wire = new WireRowData();
330
+ constructor(config, callback) {
331
+ super(config, void 0, callback);
332
+ }
333
+ /**
334
+ * The query's result rows, i.e. once our callback has fired.
335
+ *
336
+ * If a small-result materialization threshold ever seems attractive, this is where it would
337
+ * go — but see the "Small results deliberately stay lazy" note on {@link WireRowData}: lazy
338
+ * won the measured comparison at every row count for sparse access, so no threshold exists.
339
+ */
340
+ get rowData() {
341
+ return this.#wire;
342
+ }
343
+ handleRowDescription(msg) {
344
+ if (this._canceledDueToError) return;
345
+ try {
346
+ super.handleRowDescription(msg);
347
+ const types = this._result?._types;
348
+ this.#wire.setRowDescription(msg.fields, types && ((oid) => types.getTypeParser(oid, "text")));
349
+ } catch (err) {
350
+ this._canceledDueToError = err;
351
+ }
352
+ }
353
+ handleDataRow(msg) {
354
+ if (this._canceledDueToError) return;
355
+ try {
356
+ if (msg.bytes === void 0) throw new Error("joist-orm: lazyRows is enabled, but this connection's pg-protocol emits classic DataRows (likely a duplicate pg install); fix the install or disable lazyRows.");
357
+ this.#wire.addRow(msg.bytes, msg.offset, msg.length - 4);
358
+ } catch (err) {
359
+ this._canceledDueToError = err;
360
+ }
361
+ }
362
+ };
363
+ /** Builds a text-format cell decoder: utf8-slice the cell bytes into the active text parser. */
364
+ function textCellDecoder(parse) {
365
+ return (chunk, start, length) => parse(chunk.toString("utf8", start, start + length));
366
+ }
367
+ //#endregion
368
+ export { WireRowData, executeRowDataQuery, isRowDataCapableClient };
369
+
370
+ //# sourceMappingURL=WireRowData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WireRowData.js","names":["#rowCount","#payloadBytes","#rowSlices","#chunks","#columns","#rowBase","#readCell","#fields","#cellLength","#retained","#compact","#scanOrdinal","#scanOffset","#wire"],"sources":["../../src/drivers/WireRowData.ts"],"sourcesContent":["import { createRequire } from \"node:module\";\n\nimport { type RowData } from \"joist-core\";\nimport pg from \"pg\";\n\nimport { getBinaryTypeParser } from \"./binaryParsers.ts\";\n\nconst runtimeRequire = createRequire(import.meta.url);\n\n// pg's internal-but-exported Query class; subclassing it reuses its extended-protocol\n// submit/bind logic while letting us intercept row handling (the same seam pg-cursor uses).\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst PgQuery: any = runtimeRequire(\"pg/lib/query\");\n\n/** One column's RowDescription-derived metadata, resolved once per query. */\ntype WireColumn = {\n name: string;\n ordinal: number;\n /** Decodes this column's cell bytes to its value; see `setRowDescription`. */\n decode: (chunk: Buffer, start: number, length: number) => any;\n};\n\n// Each row is three consecutive entries in the `#rowSlices` table: chunk index, start, length\nconst ROW_STRIDE = 3;\n\n/** The row-length sentinel for rows dropped by `finalize` compaction. */\nconst DROPPED = 0xffffffff;\n\n/** Only compact when dropped rows hold more than this fraction of the payload bytes. */\nconst COMPACT_THRESHOLD = 0.2;\n\n/**\n * Only allocate the adaptive scan cursor once a fault targets at least this ordinal: shallower\n * scans cost less than the cursor's bookkeeping, i.e. hydrate-only results (id is ordinal ~0)\n * and narrow reads never pay the 6 bytes/row.\n */\nconst SCAN_CURSOR_MIN_ORDINAL = 8;\n\n/**\n * A lazy wire-row {@link RowData} over raw Postgres `DataRow` payload bytes.\n *\n * Each query produces its own `WireRowData` — one query, one result; results are never combined\n * or appended across queries, and the payload is read-only after the query completes (entity\n * mutations go into `InstanceData.data`, never back into the row bytes).\n *\n * Rows are kept in their row-major wire format (`int16 fieldCount` + length-prefixed cells) as\n * zero-copy views over the parser's own immutable buffers: `addRow` records each DataRow's\n * `(bytes, offset, length)` by reference, deduping the socket chunk that consecutive rows\n * share. (Our pg-protocol patch guarantees message bytes are never rewritten — chunks parse in\n * place and straddling messages get a buffer of their own; see `patchPgProtocol.ts`.) A\n * `row × column` cell is decoded on first field access by scanning the row's length-prefixed\n * cells to the column's ordinal (\"row-lazy\" decode, see JS-ROW-STORE-DESIGN.md §3/C2). This is\n * deferred decoding of row-major data, not a columnar layout.\n *\n * Because chunks are whole socket reads, retaining any row pins its ~64KiB chunk (plus whatever\n * protocol frames share it); `finalize`'s compaction copies retained rows out into an\n * exact-size owned buffer when enough of the payload was dropped.\n *\n * Text-format cells go through the same active text parsers node-postgres would resolve for\n * the query (i.e. honoring pool/client `TypeOverrides`), so they parse identically to classic\n * rows. Binary-format cells decode via `wireBinaryParser`: wire bytes -> value directly for\n * default-parsed scalar types (no intermediate string), else rendered to pg's canonical text\n * and fed through the active text parser for parity. (Classic node-postgres cannot do binary\n * at all — it round-trips cells through a UTF-8 string, corrupting bytes >= 0x80.)\n *\n * Because decoding is deferred, a custom parser that throws will do so on first field access\n * (or `toRow`/`toRows`), not while awaiting the query; `id` and inheritance-discriminator cells\n * still decode during hydration.\n *\n * After hydration, `finalize` trims unused capacity and, when some rows were not retained (i.e.\n * their entities were already in the identity map), compacts the payload down to only the\n * retained rows, so retained memory tracks live entities rather than query history.\n *\n * Small results deliberately stay lazy — there is NO row-count threshold below which we\n * materialize to a `PojoRowData` instead. Measured (benchmark-rowdata-small.ts, 40-col rows):\n * for the typical sparse access pattern (~6 of 40 columns read), keeping the lazy result wins at\n * every size including a single row (n=1: 4.3µs vs 7.6µs; n=1000: 0.76ms vs 3.0ms), because\n * materialization eagerly decodes every column while lazy faults only what is read. Even full\n * column coverage no longer flips the winner: with binary decode and `#readCell`'s adaptive\n * scan cursor, reading all ~36 columns of every row measures ~parity with classic end-to-end\n * (benchmark-lazy-parsing.ts), and small finds (n <= 10) are statistically identical.\n */\nexport class WireRowData implements RowData {\n #chunks: Buffer[] = [];\n /** Where each row's bytes live: `(chunk index, start, length)` slices, `ROW_STRIDE` entries per row. */\n #rowSlices: Uint32Array<ArrayBufferLike> = new Uint32Array(16 * ROW_STRIDE);\n #rowCount = 0;\n #payloadBytes = 0;\n #retained: number[] | undefined = undefined;\n #columns: Map<string, WireColumn> = new Map();\n #fields: WireColumn[] = [];\n /** The adaptive scan cursor: per row, the furthest scanned ordinal + its offset (see #readCell). */\n #scanOrdinal: Uint16Array | undefined = undefined;\n #scanOffset: Uint32Array | undefined = undefined;\n\n get rowCount(): number {\n return this.#rowCount;\n }\n\n /** The DataRow payload bytes currently indexed; drops when compaction discards rows. */\n get payloadBytes(): number {\n return this.#payloadBytes;\n }\n\n /** The bytes currently held by payload chunks + the row-slice table, i.e. for benchmarks. */\n get memoryBytes(): number {\n let bytes = this.#rowSlices.byteLength;\n for (const chunk of this.#chunks) bytes += chunk.length;\n return bytes;\n }\n\n get(rowIndex: number, columnName: string): any {\n const column = this.#columns.get(columnName);\n // Tolerate probes for columns the query didn't select, i.e. `__class` on non-CTI queries\n if (column === undefined) return undefined;\n const base = this.#rowBase(rowIndex);\n const slices = this.#rowSlices;\n const start = slices[base + 1];\n return this.#readCell(this.#chunks[slices[base]], start, start + slices[base + 2], column, rowIndex);\n }\n\n /** Materializes one row as a POJO, i.e. for debugging and differential tests; values are not cached. */\n toRow(rowIndex: number): any {\n const base = this.#rowBase(rowIndex);\n const slices = this.#rowSlices;\n const chunk = this.#chunks[slices[base]];\n const start = slices[base + 1];\n const end = start + slices[base + 2];\n const row: Record<string, any> = {};\n let pos = start + 2;\n for (const field of this.#fields) {\n const len = this.#cellLength(chunk, pos, end, rowIndex);\n pos += 4;\n if (len === -1) {\n row[field.name] = null;\n } else {\n row[field.name] = field.decode(chunk, pos, len);\n pos += len;\n }\n }\n return row;\n }\n\n /** Materializes classic POJO rows, i.e. for `afterFind` observation or debugging; not cached. */\n toRows(): any[] {\n const rows = new Array(this.#rowCount);\n for (let i = 0; i < this.#rowCount; i++) rows[i] = this.toRow(i);\n return rows;\n }\n\n /**\n * Resolves each column's decoder from the query's RowDescription.\n *\n * Binary-format fields decode through Joist's own binary registry only (see\n * `setBinaryTypeParser`) — `pg.types`/`TypeOverrides` are never consulted, and a column whose\n * oid has no registered binary parser fails the query here, before any rows arrive, rather\n * than guessing at a lossy decoding. Text-format fields (the `binary: false` escape hatch)\n * decode by utf8-slicing the cell into the active text parser, resolved via `getTypeParser`\n * (i.e. the pool/client `TypeOverrides` chain) or the global registry.\n */\n setRowDescription(\n fields: Array<{ name: string; dataTypeID: number; format?: string }>,\n getTypeParser?: (dataTypeID: number) => (value: any) => any,\n ): void {\n for (let i = 0; i < fields.length; i++) {\n const { name, dataTypeID, format } = fields[i];\n let decode;\n if (format === \"binary\") {\n decode = getBinaryTypeParser(dataTypeID);\n if (decode === undefined) {\n throw new Error(\n `joist-orm: no binary type parser registered for oid ${dataTypeID} (column \"${name}\");` +\n ` register one with setBinaryTypeParser (i.e. binaryTextParser for text-like types)`,\n );\n }\n } else {\n const parse = getTypeParser?.(dataTypeID) ?? pg.types.getTypeParser(dataTypeID, \"text\");\n decode = textCellDecoder(parse);\n }\n const column = { name, ordinal: i, decode };\n this.#columns.set(name, column);\n this.#fields.push(column);\n }\n }\n\n /**\n * Records one DataRow payload *by reference* (zero-copy); called synchronously from the wire\n * parser, whose patched buffer management guarantees the bytes are never rewritten.\n *\n * Consecutive rows usually share one socket chunk, so `bytes` is deduped against the last\n * chunk ref; retaining any row of a chunk pins the whole chunk, which `finalize`'s compaction\n * resolves by copying retained rows out when enough of the payload was dropped.\n */\n addRow(bytes: Buffer, offset: number, payloadLength: number): void {\n if (payloadLength < 2 || offset + payloadLength > bytes.length) {\n throw new Error(`Malformed DataRow payload (length ${payloadLength})`);\n }\n let chunkIndex = this.#chunks.length - 1;\n if (chunkIndex === -1 || this.#chunks[chunkIndex] !== bytes) {\n this.#chunks.push(bytes);\n chunkIndex++;\n }\n let slices = this.#rowSlices;\n const base = this.#rowCount * ROW_STRIDE;\n if (base === slices.length) {\n const grown = new Uint32Array(slices.length * 2);\n grown.set(slices);\n slices = this.#rowSlices = grown;\n }\n slices[base] = chunkIndex;\n slices[base + 1] = offset;\n slices[base + 2] = payloadLength;\n this.#rowCount++;\n this.#payloadBytes += payloadLength;\n }\n\n /** Marks `rowIndex` as retained by a hydrated entity; unmarked rows can be compacted away. */\n retain(rowIndex: number): void {\n (this.#retained ??= []).push(rowIndex);\n }\n\n /**\n * Trims unused capacity, and compacts down to only `retain`-ed rows when enough rows were not\n * retained to be worth the copy.\n *\n * Compaction exists to release pinned socket buffers to the GC: rows are zero-copy views into\n * whole ~64KiB chunks, so keeping any row alive pins its entire chunk — and unretained rows\n * are typically duplicates whose entities were *already in memory* (identity-map hits that\n * keep their original `rowData`), making the newly-arrived bytes dead weight. Copying the\n * retained rows into one exact-size owned buffer lets every chunk reference drop.\n *\n * The copy re-copies every retained byte, so it only pays off when it buys back a meaningful\n * fraction of the payload: we compact when the dropped rows hold more than 20% of the payload\n * bytes, and otherwise just trim, accepting the (bounded) leftover bytes. Called once after\n * hydration + sidecar reads (`_tags`, preload aggregates) are complete; retained entities keep\n * their original `rowIndex`, and un-compacted unretained rows simply remain readable-but-unused.\n */\n finalize(): void {\n const retained = this.#retained ?? [];\n this.#retained = undefined;\n if (retained.length < this.#rowCount) {\n let retainedBytes = 0;\n for (const i of retained) retainedBytes += this.#rowSlices[i * ROW_STRIDE + 2];\n const droppedBytes = this.#payloadBytes - retainedBytes;\n if (droppedBytes > this.#payloadBytes * COMPACT_THRESHOLD) {\n this.#compact(retained, retainedBytes);\n return;\n }\n }\n // Just shrink the row-slice table to its used size\n if (this.#rowCount * ROW_STRIDE < this.#rowSlices.length) {\n this.#rowSlices = this.#rowSlices.slice(0, this.#rowCount * ROW_STRIDE);\n }\n }\n\n /** Copies retained rows into one owned buffer, releasing the pinned socket chunks to the GC. */\n #compact(retained: readonly number[], bytes: number): void {\n const chunks: Buffer[] = bytes > 0 ? [Buffer.allocUnsafe(bytes)] : [];\n const slices = new Uint32Array(this.#rowCount * ROW_STRIDE);\n for (let i = 0; i < this.#rowCount; i++) slices[i * ROW_STRIDE + 2] = DROPPED;\n let used = 0;\n for (const i of retained) {\n const base = i * ROW_STRIDE;\n const source = this.#chunks[this.#rowSlices[base]];\n const start = this.#rowSlices[base + 1];\n const len = this.#rowSlices[base + 2];\n source.copy(chunks[0], used, start, start + len);\n slices[base] = 0;\n slices[base + 1] = used;\n slices[base + 2] = len;\n used += len;\n }\n this.#chunks = chunks;\n this.#rowSlices = slices;\n this.#payloadBytes = used;\n // The scan cursor survives: its cached offsets are relative to each row's (copied) payload\n }\n\n /** Validates `rowIndex` and returns its base index into the `#rowSlices` table. */\n #rowBase(rowIndex: number): number {\n if (!(rowIndex >= 0 && rowIndex < this.#rowCount)) {\n throw new Error(`Invalid rowIndex ${rowIndex} (rowCount ${this.#rowCount})`);\n }\n const base = rowIndex * ROW_STRIDE;\n if (this.#rowSlices[base + 2] === DROPPED) {\n throw new Error(`Row ${rowIndex} was compacted away (its entity was already loaded)`);\n }\n return base;\n }\n\n /**\n * Scans a row's cells to `column`'s ordinal and decodes it.\n *\n * An adaptive per-row scan cursor caches the furthest cell boundary already scanned — the\n * ordinal whose row-relative offset is known (`#scanOrdinal`/`#scanOffset`) — so ascending\n * reads resume instead of re-scanning from the row start: a dense in-order read of all C\n * columns costs one linear pass rather than O(C^2) length-prefix skips. Out-of-order\n * (descending) faults simply scan from the start, i.e. never worse than without the cursor.\n * The arrays are a fixed 6 bytes/row, allocated lazily on the first fault deep enough for\n * resuming to matter, and — being row-relative — stay valid across compaction.\n */\n #readCell(chunk: Buffer, start: number, end: number, column: WireColumn, rowIndex: number): any {\n const fieldCount = chunk.readInt16BE(start);\n const { ordinal } = column;\n if (ordinal >= fieldCount) {\n throw new Error(`Row ${rowIndex} has ${fieldCount} cells but column ${column.name} is #${column.ordinal}`);\n }\n let pos = start + 2;\n let c = 0;\n let scanOrdinal = this.#scanOrdinal;\n if (scanOrdinal === undefined && ordinal >= SCAN_CURSOR_MIN_ORDINAL) {\n scanOrdinal = this.#scanOrdinal = new Uint16Array(this.#rowCount);\n this.#scanOffset = new Uint32Array(this.#rowCount);\n }\n if (scanOrdinal !== undefined && rowIndex < scanOrdinal.length) {\n // 0 = unset: caching cell #0 would be pointless (it is always at offset 2), so any real\n // entry is the ordinal, >= 1, whose row-relative offset is in #scanOffset\n const known = scanOrdinal[rowIndex];\n if (known !== 0 && known <= ordinal) {\n c = known;\n pos = start + this.#scanOffset![rowIndex];\n }\n }\n for (; c < ordinal; c++) {\n const len = this.#cellLength(chunk, pos, end, rowIndex);\n pos += len > 0 ? len + 4 : 4;\n }\n const len = this.#cellLength(chunk, pos, end, rowIndex);\n if (scanOrdinal !== undefined && rowIndex < scanOrdinal.length && ordinal + 1 > scanOrdinal[rowIndex]) {\n // advance-only: after reading cell #ordinal we know where cell #ordinal+1 starts\n scanOrdinal[rowIndex] = ordinal + 1;\n this.#scanOffset![rowIndex] = pos + 4 + (len > 0 ? len : 0) - start;\n }\n if (len === -1) return null;\n return column.decode(chunk, pos + 4, len);\n }\n\n /** Reads + validates one cell's length prefix. */\n #cellLength(chunk: Buffer, pos: number, end: number, rowIndex: number): number {\n if (pos + 4 > end) throw new Error(`Truncated DataRow payload in row ${rowIndex}`);\n const len = chunk.readInt32BE(pos);\n if (len < -1 || (len > 0 && pos + 4 + len > end)) {\n throw new Error(`Malformed cell length ${len} in row ${rowIndex}`);\n }\n return len;\n }\n}\n\n/**\n * Executes `sql` on an already-checked-out client, returning a {@link RowData} instead of\n * materialized POJO rows.\n *\n * Uses a `pg` Query subclass that records each DataRow's raw payload bytes into the result\n * (via the lazy DataRow message from `patchPgProtocol`) and never materializes per-cell\n * strings or per-row objects. If the client's connection turns out to use an unpatched\n * pg-protocol copy (i.e. the app's pool was built from a different `pg` install than the one\n * joist-orm patched), the query fails with a descriptive error — a misconfiguration any CI\n * build/smoketest will surface immediately, so we fail loudly rather than silently degrade.\n * The rows already streamed are discarded; the connection itself stays usable.\n *\n * By default the query requests *binary* result format (via the extended protocol), so scalar\n * cells decode wire-bytes -> value with no intermediate strings; see `wireBinaryParser` for the\n * parity strategy. Pass `binary: false` for classic text-format results, or set\n * `JOIST_LAZY_BINARY=0` to flip the default while the binary path is a prototype (i.e. for\n * A/B benchmarking or as an escape hatch).\n */\n// The prototype escape hatch for binary results, i.e. for A/B benchmarking; read once at load\nconst BINARY_BY_DEFAULT = process.env.JOIST_LAZY_BINARY !== \"0\";\n\nexport function executeRowDataQuery(\n client: pg.PoolClient,\n sql: string,\n bindings: readonly any[],\n opts?: { binary?: boolean },\n): Promise<RowData> {\n const binary = opts?.binary ?? BINARY_BY_DEFAULT;\n return new Promise((resolve, reject) => {\n const query = new RowDataQuery(\n // Binary results require the extended protocol; `queryMode` forces it for bindings-less queries\n binary\n ? { text: sql, values: bindings as any[], binary: true, queryMode: \"extended\" }\n : { text: sql, values: bindings as any[] },\n (err: unknown) => {\n if (err) reject(err);\n else resolve(query.rowData);\n },\n );\n client.query(query as any);\n });\n}\n\n/** Returns whether `client` supports the lazy row-data query path, before submitting anything. */\nexport function isRowDataCapableClient(client: unknown): client is pg.PoolClient {\n // Require the pure-JS pg client (pg-native has no `connection` and different query internals)\n return (\n typeof client === \"object\" &&\n client !== null &&\n typeof (client as any).query === \"function\" &&\n (client as any).connection !== undefined &&\n (client as any).native === undefined\n );\n}\n\n/** A pg Query that diverts DataRows into a {@link WireRowData} instead of a `Result`. */\nclass RowDataQuery extends PgQuery {\n #wire = new WireRowData();\n\n constructor(\n config: { text: string; values: any[]; binary?: boolean; queryMode?: string },\n callback: (err: unknown) => void,\n ) {\n super(config, undefined, callback);\n }\n\n /**\n * The query's result rows, i.e. once our callback has fired.\n *\n * If a small-result materialization threshold ever seems attractive, this is where it would\n * go — but see the \"Small results deliberately stay lazy\" note on {@link WireRowData}: lazy\n * won the measured comparison at every row count for sparse access, so no threshold exists.\n */\n get rowData(): RowData {\n return this.#wire;\n }\n\n handleRowDescription(msg: any): void {\n if (this._canceledDueToError) return;\n try {\n super.handleRowDescription(msg);\n // Resolve active *text* parsers through the client's TypeOverrides chain (client.js\n // injects `_types` at submit time); we can't reuse `Result._parsers` because in binary\n // mode those resolve to pg's (broken) binary registry, while our binary decode path is\n // built on text-parser parity — see `wireBinaryParser`\n const types = this._result?._types;\n this.#wire.setRowDescription(msg.fields, types && ((oid: number) => types.getTypeParser(oid, \"text\")));\n } catch (err) {\n // Mirror pg's Query error containment: record + reject at ReadyForQuery, keeping the\n // connection's protocol state intact\n this._canceledDueToError = err;\n }\n }\n\n handleDataRow(msg: any): void {\n if (this._canceledDueToError) return;\n try {\n if (msg.bytes === undefined) {\n // This connection's pg-protocol copy is unpatched (i.e. the pool came from a different\n // `pg` install than the one joist-orm patched), so lazyRows cannot work — fail loudly\n // rather than silently degrade; any CI build/smoketest will surface this immediately\n throw new Error(\n \"joist-orm: lazyRows is enabled, but this connection's pg-protocol emits classic\" +\n \" DataRows (likely a duplicate pg install); fix the install or disable lazyRows.\",\n );\n }\n // `msg.length` includes the int32 length field itself, so the payload is `length - 4`;\n // our patched message carries the real length from `handlePacket`'s argument\n this.#wire.addRow(msg.bytes, msg.offset, msg.length - 4);\n } catch (err) {\n this._canceledDueToError = err;\n }\n }\n}\n\n/** Builds a text-format cell decoder: utf8-slice the cell bytes into the active text parser. */\nfunction textCellDecoder(parse: (value: any) => any): (chunk: Buffer, start: number, length: number) => any {\n return (chunk, start, length) => parse(chunk.toString(\"utf8\", start, start + length));\n}\n"],"mappings":";;;;;AAYA,MAAM,U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