joist-orm 2.4.0 → 2.5.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/build/drivers/PostgreJsDriver.cjs +216 -0
- package/build/drivers/PostgreJsDriver.cjs.map +1 -0
- package/build/drivers/PostgreJsDriver.d.cts +87 -0
- package/build/drivers/PostgreJsDriver.d.cts.map +1 -0
- package/build/drivers/PostgreJsDriver.d.mts +87 -0
- package/build/drivers/PostgreJsDriver.d.mts.map +1 -0
- package/build/drivers/PostgreJsDriver.js +212 -0
- package/build/drivers/PostgreJsDriver.js.map +1 -0
- package/build/drivers/PostgresDriver.cjs +165 -196
- package/build/drivers/PostgresDriver.cjs.map +1 -1
- package/build/drivers/PostgresDriver.d.cts +71 -47
- package/build/drivers/PostgresDriver.d.cts.map +1 -1
- package/build/drivers/PostgresDriver.d.mts +71 -47
- package/build/drivers/PostgresDriver.d.mts.map +1 -1
- package/build/drivers/PostgresDriver.js +164 -197
- package/build/drivers/PostgresDriver.js.map +1 -1
- package/build/drivers/WireRowData.cjs +26 -130
- package/build/drivers/WireRowData.cjs.map +1 -1
- package/build/drivers/WireRowData.d.cts +26 -33
- package/build/drivers/WireRowData.d.cts.map +1 -1
- package/build/drivers/WireRowData.d.mts +26 -33
- package/build/drivers/WireRowData.d.mts.map +1 -1
- package/build/drivers/WireRowData.js +27 -128
- package/build/drivers/WireRowData.js.map +1 -1
- package/build/drivers/binaryParsers.cjs +21 -13
- package/build/drivers/binaryParsers.cjs.map +1 -1
- package/build/drivers/binaryParsers.d.cts +16 -18
- package/build/drivers/binaryParsers.d.cts.map +1 -1
- package/build/drivers/binaryParsers.d.mts +16 -18
- package/build/drivers/binaryParsers.d.mts.map +1 -1
- package/build/drivers/binaryParsers.js +21 -13
- package/build/drivers/binaryParsers.js.map +1 -1
- package/build/drivers/patchPgProtocol.cjs.map +1 -1
- package/build/drivers/patchPgProtocol.d.cts +2 -3
- package/build/drivers/patchPgProtocol.d.cts.map +1 -1
- package/build/drivers/patchPgProtocol.d.mts +2 -3
- package/build/drivers/patchPgProtocol.d.mts.map +1 -1
- package/build/drivers/patchPgProtocol.js.map +1 -1
- package/build/drivers/postgresSql.cjs +145 -0
- package/build/drivers/postgresSql.cjs.map +1 -0
- package/build/drivers/postgresSql.d.cts +19 -0
- package/build/drivers/postgresSql.d.cts.map +1 -0
- package/build/drivers/postgresSql.d.mts +19 -0
- package/build/drivers/postgresSql.d.mts.map +1 -0
- package/build/drivers/postgresSql.js +143 -0
- package/build/drivers/postgresSql.js.map +1 -0
- package/build/index.js +1 -2
- package/build/pg-export.d.cts +1 -1
- package/build/pg-export.d.mts +1 -1
- package/build/postgrejs-export.cjs +3 -0
- package/build/postgrejs-export.d.cts +2 -0
- package/build/postgrejs-export.d.mts +2 -0
- package/build/postgrejs-export.js +1 -0
- package/build/seed.d.cts +1 -2
- package/build/seed.d.cts.map +1 -1
- package/build/seed.d.mts +1 -2
- package/build/seed.d.mts.map +1 -1
- package/package.json +38 -11
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
|
|
3
|
-
const require_drivers_binaryParsers = require("./binaryParsers.cjs");
|
|
4
|
-
require("joist-core");
|
|
5
|
-
let pg = require("pg");
|
|
6
|
-
pg = require_runtime.__toESM(pg, 1);
|
|
7
2
|
//#region src/drivers/WireRowData.ts
|
|
8
|
-
const PgQuery = (0, require("node:module").createRequire)(require("url").pathToFileURL(__filename).href)("pg/lib/query");
|
|
9
3
|
const ROW_STRIDE = 3;
|
|
10
4
|
/** The row-length sentinel for rows dropped by `finalize` compaction. */
|
|
11
5
|
const DROPPED = 4294967295;
|
|
@@ -24,11 +18,11 @@ const SCAN_CURSOR_MIN_ORDINAL = 8;
|
|
|
24
18
|
* or appended across queries, and the payload is read-only after the query completes (entity
|
|
25
19
|
* mutations go into `InstanceData.data`, never back into the row bytes).
|
|
26
20
|
*
|
|
27
|
-
* Rows are kept in their row-major wire format (`int16 fieldCount` + length-prefixed cells) as
|
|
21
|
+
* Rows are kept in their row-major wire format (optional `int16 fieldCount` + length-prefixed cells) as
|
|
28
22
|
* zero-copy views over the parser's own immutable buffers: `addRow` records each DataRow's
|
|
29
23
|
* `(bytes, offset, length)` by reference, deduping the socket chunk that consecutive rows
|
|
30
|
-
* share.
|
|
31
|
-
*
|
|
24
|
+
* share. The selected driver guarantees immutable bytes: node-pg through `patchPgProtocol.ts`,
|
|
25
|
+
* PostgreJS through its public RowDecoder contract. A
|
|
32
26
|
* `row × column` cell is decoded on first field access by scanning the row's length-prefixed
|
|
33
27
|
* cells to the column's ordinal ("row-lazy" decode, see JS-ROW-STORE-DESIGN.md §3/C2). This is
|
|
34
28
|
* deferred decoding of row-major data, not a columnar layout.
|
|
@@ -37,12 +31,11 @@ const SCAN_CURSOR_MIN_ORDINAL = 8;
|
|
|
37
31
|
* protocol frames share it); `finalize`'s compaction copies retained rows out into an
|
|
38
32
|
* exact-size owned buffer when enough of the payload was dropped.
|
|
39
33
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* at all — it round-trips cells through a UTF-8 string, corrupting bytes >= 0x80.)
|
|
34
|
+
* Column decoders come from the selected driver through `setColumnDecoders`. RowDataQuery
|
|
35
|
+
* resolves node-pg text fields through TypeOverrides and binary fields through Joist's binary
|
|
36
|
+
* registry. PostgreJsDriver supplies its native parsers, bypassing pg's parser resolution.
|
|
37
|
+
* (Classic node-postgres cannot safely decode binary cells: it round-trips
|
|
38
|
+
* them through a UTF-8 string, corrupting bytes >= 0x80.)
|
|
46
39
|
*
|
|
47
40
|
* Because decoding is deferred, a custom parser that throws will do so on first field access
|
|
48
41
|
* (or `toRow`/`toRows`), not while awaiting the query; `id` and inheritance-discriminator cells
|
|
@@ -62,6 +55,19 @@ const SCAN_CURSOR_MIN_ORDINAL = 8;
|
|
|
62
55
|
* (benchmark-lazy-parsing.ts), and small finds (n <= 10) are statistically identical.
|
|
63
56
|
*/
|
|
64
57
|
var WireRowData = class {
|
|
58
|
+
rowHeaderBytes;
|
|
59
|
+
/** PostgreJS omits the int16 field count from the immutable row view it exposes. */
|
|
60
|
+
constructor(rowHeaderBytes = 2) {
|
|
61
|
+
this.rowHeaderBytes = rowHeaderBytes;
|
|
62
|
+
}
|
|
63
|
+
/** Installs decoders supplied by a driver with a public raw-row API. */
|
|
64
|
+
setColumnDecoders(fields) {
|
|
65
|
+
this.#fields = fields.map((field, ordinal) => ({
|
|
66
|
+
...field,
|
|
67
|
+
ordinal
|
|
68
|
+
}));
|
|
69
|
+
this.#columns = new Map(this.#fields.map((field) => [field.name, field]));
|
|
70
|
+
}
|
|
65
71
|
#chunks = [];
|
|
66
72
|
/** Where each row's bytes live: `(chunk index, start, length)` slices, `ROW_STRIDE` entries per row. */
|
|
67
73
|
#rowSlices = /* @__PURE__ */ new Uint32Array(48);
|
|
@@ -102,7 +108,7 @@ var WireRowData = class {
|
|
|
102
108
|
const start = slices[base + 1];
|
|
103
109
|
const end = start + slices[base + 2];
|
|
104
110
|
const row = {};
|
|
105
|
-
let pos = start +
|
|
111
|
+
let pos = start + this.rowHeaderBytes;
|
|
106
112
|
for (const field of this.#fields) {
|
|
107
113
|
const len = this.#cellLength(chunk, pos, end, rowIndex);
|
|
108
114
|
pos += 4;
|
|
@@ -121,42 +127,15 @@ var WireRowData = class {
|
|
|
121
127
|
return rows;
|
|
122
128
|
}
|
|
123
129
|
/**
|
|
124
|
-
* Resolves each column's decoder from the query's RowDescription.
|
|
125
|
-
*
|
|
126
|
-
* Binary-format fields decode through Joist's own binary registry only (see
|
|
127
|
-
* `setBinaryTypeParser`) — `pg.types`/`TypeOverrides` are never consulted, and a column whose
|
|
128
|
-
* oid has no registered binary parser fails the query here, before any rows arrive, rather
|
|
129
|
-
* than guessing at a lossy decoding. Text-format fields (the `binary: false` escape hatch)
|
|
130
|
-
* decode by utf8-slicing the cell into the active text parser, resolved via `getTypeParser`
|
|
131
|
-
* (i.e. the pool/client `TypeOverrides` chain) or the global registry.
|
|
132
|
-
*/
|
|
133
|
-
setRowDescription(fields, getTypeParser) {
|
|
134
|
-
for (let i = 0; i < fields.length; i++) {
|
|
135
|
-
const { name, dataTypeID, format } = fields[i];
|
|
136
|
-
let decode;
|
|
137
|
-
if (format === "binary") {
|
|
138
|
-
decode = require_drivers_binaryParsers.getBinaryTypeParser(dataTypeID);
|
|
139
|
-
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)`);
|
|
140
|
-
} else decode = textCellDecoder(getTypeParser?.(dataTypeID) ?? pg.default.types.getTypeParser(dataTypeID, "text"));
|
|
141
|
-
const column = {
|
|
142
|
-
name,
|
|
143
|
-
ordinal: i,
|
|
144
|
-
decode
|
|
145
|
-
};
|
|
146
|
-
this.#columns.set(name, column);
|
|
147
|
-
this.#fields.push(column);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
130
|
* Records one DataRow payload *by reference* (zero-copy); called synchronously from the wire
|
|
152
|
-
* parser, whose
|
|
131
|
+
* parser, whose buffer ownership contract guarantees the bytes are never rewritten.
|
|
153
132
|
*
|
|
154
133
|
* Consecutive rows usually share one socket chunk, so `bytes` is deduped against the last
|
|
155
134
|
* chunk ref; retaining any row of a chunk pins the whole chunk, which `finalize`'s compaction
|
|
156
135
|
* resolves by copying retained rows out when enough of the payload was dropped.
|
|
157
136
|
*/
|
|
158
137
|
addRow(bytes, offset, payloadLength) {
|
|
159
|
-
if (payloadLength <
|
|
138
|
+
if (payloadLength < this.rowHeaderBytes || offset + payloadLength > bytes.length) throw new Error(`Malformed DataRow payload (length ${payloadLength})`);
|
|
160
139
|
let chunkIndex = this.#chunks.length - 1;
|
|
161
140
|
if (chunkIndex === -1 || this.#chunks[chunkIndex] !== bytes) {
|
|
162
141
|
this.#chunks.push(bytes);
|
|
@@ -248,10 +227,10 @@ var WireRowData = class {
|
|
|
248
227
|
* resuming to matter, and — being row-relative — stay valid across compaction.
|
|
249
228
|
*/
|
|
250
229
|
#readCell(chunk, start, end, column, rowIndex) {
|
|
251
|
-
const fieldCount = chunk.readInt16BE(start);
|
|
230
|
+
const fieldCount = this.rowHeaderBytes === 2 ? chunk.readInt16BE(start) : this.#fields.length;
|
|
252
231
|
const { ordinal } = column;
|
|
253
232
|
if (ordinal >= fieldCount) throw new Error(`Row ${rowIndex} has ${fieldCount} cells but column ${column.name} is #${column.ordinal}`);
|
|
254
|
-
let pos = start +
|
|
233
|
+
let pos = start + this.rowHeaderBytes;
|
|
255
234
|
let c = 0;
|
|
256
235
|
let scanOrdinal = this.#scanOrdinal;
|
|
257
236
|
if (scanOrdinal === void 0 && ordinal >= SCAN_CURSOR_MIN_ORDINAL) {
|
|
@@ -285,90 +264,7 @@ var WireRowData = class {
|
|
|
285
264
|
return len;
|
|
286
265
|
}
|
|
287
266
|
};
|
|
288
|
-
/**
|
|
289
|
-
* Executes `sql` on an already-checked-out client, returning a {@link RowData} instead of
|
|
290
|
-
* materialized POJO rows.
|
|
291
|
-
*
|
|
292
|
-
* Uses a `pg` Query subclass that records each DataRow's raw payload bytes into the result
|
|
293
|
-
* (via the lazy DataRow message from `patchPgProtocol`) and never materializes per-cell
|
|
294
|
-
* strings or per-row objects. If the client's connection turns out to use an unpatched
|
|
295
|
-
* pg-protocol copy (i.e. the app's pool was built from a different `pg` install than the one
|
|
296
|
-
* joist-orm patched), the query fails with a descriptive error — a misconfiguration any CI
|
|
297
|
-
* build/smoketest will surface immediately, so we fail loudly rather than silently degrade.
|
|
298
|
-
* The rows already streamed are discarded; the connection itself stays usable.
|
|
299
|
-
*
|
|
300
|
-
* By default the query requests *binary* result format (via the extended protocol), so scalar
|
|
301
|
-
* cells decode wire-bytes -> value with no intermediate strings; see `wireBinaryParser` for the
|
|
302
|
-
* parity strategy. Pass `binary: false` for classic text-format results, or set
|
|
303
|
-
* `JOIST_LAZY_BINARY=0` to flip the default while the binary path is a prototype (i.e. for
|
|
304
|
-
* A/B benchmarking or as an escape hatch).
|
|
305
|
-
*/
|
|
306
|
-
const BINARY_BY_DEFAULT = process.env.JOIST_LAZY_BINARY !== "0";
|
|
307
|
-
function executeRowDataQuery(client, sql, bindings, opts) {
|
|
308
|
-
const binary = opts?.binary ?? BINARY_BY_DEFAULT;
|
|
309
|
-
return new Promise((resolve, reject) => {
|
|
310
|
-
const query = new RowDataQuery(binary ? {
|
|
311
|
-
text: sql,
|
|
312
|
-
values: bindings,
|
|
313
|
-
binary: true,
|
|
314
|
-
queryMode: "extended"
|
|
315
|
-
} : {
|
|
316
|
-
text: sql,
|
|
317
|
-
values: bindings
|
|
318
|
-
}, (err) => {
|
|
319
|
-
if (err) reject(err);
|
|
320
|
-
else resolve(query.rowData);
|
|
321
|
-
});
|
|
322
|
-
client.query(query);
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
/** Returns whether `client` supports the lazy row-data query path, before submitting anything. */
|
|
326
|
-
function isRowDataCapableClient(client) {
|
|
327
|
-
return typeof client === "object" && client !== null && typeof client.query === "function" && client.connection !== void 0 && client.native === void 0;
|
|
328
|
-
}
|
|
329
|
-
/** A pg Query that diverts DataRows into a {@link WireRowData} instead of a `Result`. */
|
|
330
|
-
var RowDataQuery = class extends PgQuery {
|
|
331
|
-
#wire = new WireRowData();
|
|
332
|
-
constructor(config, callback) {
|
|
333
|
-
super(config, void 0, callback);
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* The query's result rows, i.e. once our callback has fired.
|
|
337
|
-
*
|
|
338
|
-
* If a small-result materialization threshold ever seems attractive, this is where it would
|
|
339
|
-
* go — but see the "Small results deliberately stay lazy" note on {@link WireRowData}: lazy
|
|
340
|
-
* won the measured comparison at every row count for sparse access, so no threshold exists.
|
|
341
|
-
*/
|
|
342
|
-
get rowData() {
|
|
343
|
-
return this.#wire;
|
|
344
|
-
}
|
|
345
|
-
handleRowDescription(msg) {
|
|
346
|
-
if (this._canceledDueToError) return;
|
|
347
|
-
try {
|
|
348
|
-
super.handleRowDescription(msg);
|
|
349
|
-
const types = this._result?._types;
|
|
350
|
-
this.#wire.setRowDescription(msg.fields, types && ((oid) => types.getTypeParser(oid, "text")));
|
|
351
|
-
} catch (err) {
|
|
352
|
-
this._canceledDueToError = err;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
handleDataRow(msg) {
|
|
356
|
-
if (this._canceledDueToError) return;
|
|
357
|
-
try {
|
|
358
|
-
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.");
|
|
359
|
-
this.#wire.addRow(msg.bytes, msg.offset, msg.length - 4);
|
|
360
|
-
} catch (err) {
|
|
361
|
-
this._canceledDueToError = err;
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
};
|
|
365
|
-
/** Builds a text-format cell decoder: utf8-slice the cell bytes into the active text parser. */
|
|
366
|
-
function textCellDecoder(parse) {
|
|
367
|
-
return (chunk, start, length) => parse(chunk.toString("utf8", start, start + length));
|
|
368
|
-
}
|
|
369
267
|
//#endregion
|
|
370
268
|
exports.WireRowData = WireRowData;
|
|
371
|
-
exports.executeRowDataQuery = executeRowDataQuery;
|
|
372
|
-
exports.isRowDataCapableClient = isRowDataCapableClient;
|
|
373
269
|
|
|
374
270
|
//# sourceMappingURL=WireRowData.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WireRowData.cjs","names":["createRequire","#rowCount","#payloadBytes","#rowSlices","#chunks","#columns","#rowBase","#readCell","#fields","#cellLength","getBinaryTypeParser","#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,WAAA,wBALiBA,CAAAA,CAAAA,cAAAA,CAAAA,QAAAA,KAAAA,CAAAA,CAAAA,cAAAA,UAAAA,CAAAA,CAAAA,IAKW,CAAC,CAAC,cAAc;AAWlD,MAAM,aAAa;;AAGnB,MAAM,UAAU;;AAGhB,MAAM,oBAAoB;;;;;;AAO1B,MAAM,0BAA0B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8ChC,IAAa,cAAb,MAA4C;CAC1C,UAAoB,CAAC;;CAErB,6BAA2C,IAAI,YAAY,EAAe;CAC1E,YAAY;CACZ,gBAAgB;CAChB,YAAkC,KAAA;CAClC,2BAAoC,IAAI,IAAI;CAC5C,UAAwB,CAAC;;CAEzB,eAAwC,KAAA;CACxC,cAAuC,KAAA;CAEvC,IAAI,WAAmB;EACrB,OAAO,KAAKC;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AAsB;EAC7B;CACF;AACF;;AAGA,SAAS,gBAAgB,OAAmF;CAC1G,QAAQ,OAAO,OAAO,WAAW,MAAM,MAAM,SAAS,QAAQ,OAAO,QAAQ,MAAM,CAAC;AACtF"}
|
|
1
|
+
{"version":3,"file":"WireRowData.cjs","names":[],"sources":["../../src/drivers/WireRowData.ts"],"sourcesContent":["import type { RowData } from \"joist-core\";\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; supplied by the selected driver. */\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 (optional `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. The selected driver guarantees immutable bytes: node-pg through `patchPgProtocol.ts`,\n * PostgreJS through its public RowDecoder contract. 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 * Column decoders come from the selected driver through `setColumnDecoders`. RowDataQuery\n * resolves node-pg text fields through TypeOverrides and binary fields through Joist's binary\n * registry. PostgreJsDriver supplies its native parsers, bypassing pg's parser resolution.\n * (Classic node-postgres cannot safely decode binary cells: it round-trips\n * them 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 /** PostgreJS omits the int16 field count from the immutable row view it exposes. */\n constructor(private readonly rowHeaderBytes: 0 | 2 = 2) {}\n\n /** Installs decoders supplied by a driver with a public raw-row API. */\n setColumnDecoders(fields: Array<{ name: string; decode: WireColumn[\"decode\"] }>): void {\n this.#fields = fields.map((field, ordinal) => ({ ...field, ordinal }));\n this.#columns = new Map(this.#fields.map((field) => [field.name, field]));\n }\n\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 + this.rowHeaderBytes;\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 * Records one DataRow payload *by reference* (zero-copy); called synchronously from the wire\n * parser, whose buffer ownership contract 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 < this.rowHeaderBytes || 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 = this.rowHeaderBytes === 2 ? chunk.readInt16BE(start) : this.#fields.length;\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 + this.rowHeaderBytes;\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 rowHeaderBytes), 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"],"mappings":";;AAWA,MAAM,aAAa;;AAGnB,MAAM,UAAU;;AAGhB,MAAM,oBAAoB;;;;;;AAO1B,MAAM,0BAA0B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6ChC,IAAa,cAAb,MAA4C;CAEb;;CAA7B,YAAY,iBAAyC,GAAG;EAA3B,KAAA,iBAAA;CAA4B;;CAGzD,kBAAkB,QAAqE;EACrF,KAAK,UAAU,OAAO,KAAK,OAAO,aAAa;GAAE,GAAG;GAAO;EAAQ,EAAE;EACrE,KAAK,WAAW,IAAI,IAAI,KAAK,QAAQ,KAAK,UAAU,CAAC,MAAM,MAAM,KAAK,CAAC,CAAC;CAC1E;CAEA,UAAoB,CAAC;;CAErB,6BAA2C,IAAI,YAAY,EAAe;CAC1E,YAAY;CACZ,gBAAgB;CAChB,YAAkC,KAAA;CAClC,2BAAoC,IAAI,IAAI;CAC5C,UAAwB,CAAC;;CAEzB,eAAwC,KAAA;CACxC,cAAuC,KAAA;CAEvC,IAAI,WAAmB;EACrB,OAAO,KAAK;CACd;;CAGA,IAAI,eAAuB;EACzB,OAAO,KAAK;CACd;;CAGA,IAAI,cAAsB;EACxB,IAAI,QAAQ,KAAK,WAAW;EAC5B,KAAK,MAAM,SAAS,KAAK,SAAS,SAAS,MAAM;EACjD,OAAO;CACT;CAEA,IAAI,UAAkB,YAAyB;EAC7C,MAAM,SAAS,KAAK,SAAS,IAAI,UAAU;EAE3C,IAAI,WAAW,KAAA,GAAW,OAAO,KAAA;EACjC,MAAM,OAAO,KAAK,SAAS,QAAQ;EACnC,MAAM,SAAS,KAAK;EACpB,MAAM,QAAQ,OAAO,OAAO;EAC5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AI,QAAQ,IAAI,OAAO;EACvB,OAAO,OAAO,OAAO,OAAO,MAAM,GAAG,GAAG;CAC1C;;CAGA,YAAY,OAAe,KAAa,KAAa,UAA0B;EAC7E,IAAI,MAAM,IAAI,KAAK,MAAM,IAAI,MAAM,oCAAoC,UAAU;EACjF,MAAM,MAAM,MAAM,YAAY,GAAG;EACjC,IAAI,MAAM,MAAO,MAAM,KAAK,MAAM,IAAI,MAAM,KAC1C,MAAM,IAAI,MAAM,yBAAyB,IAAI,UAAU,UAAU;EAEnE,OAAO;CACT;AACF"}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { RowData } from "joist-core";
|
|
2
|
-
import pg from "pg";
|
|
3
2
|
//#region src/drivers/WireRowData.d.ts
|
|
3
|
+
/** One column's RowDescription-derived metadata, resolved once per query. */
|
|
4
|
+
type WireColumn = {
|
|
5
|
+
name: string;
|
|
6
|
+
ordinal: number;
|
|
7
|
+
/** Decodes this column's cell bytes to its value; supplied by the selected driver. */
|
|
8
|
+
decode: (chunk: Buffer, start: number, length: number) => any;
|
|
9
|
+
};
|
|
4
10
|
/**
|
|
5
11
|
* A lazy wire-row {@link RowData} over raw Postgres `DataRow` payload bytes.
|
|
6
12
|
*
|
|
@@ -8,11 +14,11 @@ import pg from "pg";
|
|
|
8
14
|
* or appended across queries, and the payload is read-only after the query completes (entity
|
|
9
15
|
* mutations go into `InstanceData.data`, never back into the row bytes).
|
|
10
16
|
*
|
|
11
|
-
* Rows are kept in their row-major wire format (`int16 fieldCount` + length-prefixed cells) as
|
|
17
|
+
* Rows are kept in their row-major wire format (optional `int16 fieldCount` + length-prefixed cells) as
|
|
12
18
|
* zero-copy views over the parser's own immutable buffers: `addRow` records each DataRow's
|
|
13
19
|
* `(bytes, offset, length)` by reference, deduping the socket chunk that consecutive rows
|
|
14
|
-
* share.
|
|
15
|
-
*
|
|
20
|
+
* share. The selected driver guarantees immutable bytes: node-pg through `patchPgProtocol.ts`,
|
|
21
|
+
* PostgreJS through its public RowDecoder contract. A
|
|
16
22
|
* `row × column` cell is decoded on first field access by scanning the row's length-prefixed
|
|
17
23
|
* cells to the column's ordinal ("row-lazy" decode, see JS-ROW-STORE-DESIGN.md §3/C2). This is
|
|
18
24
|
* deferred decoding of row-major data, not a columnar layout.
|
|
@@ -21,12 +27,11 @@ import pg from "pg";
|
|
|
21
27
|
* protocol frames share it); `finalize`'s compaction copies retained rows out into an
|
|
22
28
|
* exact-size owned buffer when enough of the payload was dropped.
|
|
23
29
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* at all — it round-trips cells through a UTF-8 string, corrupting bytes >= 0x80.)
|
|
30
|
+
* Column decoders come from the selected driver through `setColumnDecoders`. RowDataQuery
|
|
31
|
+
* resolves node-pg text fields through TypeOverrides and binary fields through Joist's binary
|
|
32
|
+
* registry. PostgreJsDriver supplies its native parsers, bypassing pg's parser resolution.
|
|
33
|
+
* (Classic node-postgres cannot safely decode binary cells: it round-trips
|
|
34
|
+
* them through a UTF-8 string, corrupting bytes >= 0x80.)
|
|
30
35
|
*
|
|
31
36
|
* Because decoding is deferred, a custom parser that throws will do so on first field access
|
|
32
37
|
* (or `toRow`/`toRows`), not while awaiting the query; `id` and inheritance-discriminator cells
|
|
@@ -45,8 +50,16 @@ import pg from "pg";
|
|
|
45
50
|
* scan cursor, reading all ~36 columns of every row measures ~parity with classic end-to-end
|
|
46
51
|
* (benchmark-lazy-parsing.ts), and small finds (n <= 10) are statistically identical.
|
|
47
52
|
*/
|
|
48
|
-
declare class WireRowData implements RowData {
|
|
53
|
+
export declare class WireRowData implements RowData {
|
|
49
54
|
#private;
|
|
55
|
+
private readonly rowHeaderBytes;
|
|
56
|
+
/** PostgreJS omits the int16 field count from the immutable row view it exposes. */
|
|
57
|
+
constructor(rowHeaderBytes?: 0 | 2);
|
|
58
|
+
/** Installs decoders supplied by a driver with a public raw-row API. */
|
|
59
|
+
setColumnDecoders(fields: Array<{
|
|
60
|
+
name: string;
|
|
61
|
+
decode: WireColumn["decode"];
|
|
62
|
+
}>): void;
|
|
50
63
|
get rowCount(): number;
|
|
51
64
|
/** The DataRow payload bytes currently indexed; drops when compaction discards rows. */
|
|
52
65
|
get payloadBytes(): number;
|
|
@@ -57,24 +70,9 @@ declare class WireRowData implements RowData {
|
|
|
57
70
|
toRow(rowIndex: number): any;
|
|
58
71
|
/** Materializes classic POJO rows, i.e. for `afterFind` observation or debugging; not cached. */
|
|
59
72
|
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
73
|
/**
|
|
76
74
|
* Records one DataRow payload *by reference* (zero-copy); called synchronously from the wire
|
|
77
|
-
* parser, whose
|
|
75
|
+
* parser, whose buffer ownership contract guarantees the bytes are never rewritten.
|
|
78
76
|
*
|
|
79
77
|
* Consecutive rows usually share one socket chunk, so `bytes` is deduped against the last
|
|
80
78
|
* chunk ref; retaining any row of a chunk pins the whole chunk, which `finalize`'s compaction
|
|
@@ -101,11 +99,6 @@ declare class WireRowData implements RowData {
|
|
|
101
99
|
*/
|
|
102
100
|
finalize(): void;
|
|
103
101
|
}
|
|
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
102
|
//#endregion
|
|
110
|
-
export {
|
|
103
|
+
export {};
|
|
111
104
|
//# sourceMappingURL=WireRowData.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WireRowData.d.cts","names":[],"sources":["../../src/drivers/WireRowData.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"WireRowData.d.cts","names":[],"sources":["../../src/drivers/WireRowData.ts"],"mappings":";;;KAGK;EACH;EACA;;EAEA,SAAS,OAAO,QAAQ,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA8D5B,uBAAuB;;mBAEL;;EAA7B,YAA6B;;EAG7B,kBAAkB,QAAQ;IAAQ;IAAc,QAAQ;;MAiBpD;;MAKA;;MAKA;EAMJ,IAAI,kBAAkB;;EAWtB,MAAM;;EAsBN;;;;;;;;;EAcA,OAAO,OAAO,QAAQ,gBAAgB;;EAwBtC,OAAO;;;;;;;;;;;;;;;;;EAoBP"}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { RowData } from "joist-core";
|
|
2
|
-
import pg from "pg";
|
|
3
2
|
//#region src/drivers/WireRowData.d.ts
|
|
3
|
+
/** One column's RowDescription-derived metadata, resolved once per query. */
|
|
4
|
+
type WireColumn = {
|
|
5
|
+
name: string;
|
|
6
|
+
ordinal: number;
|
|
7
|
+
/** Decodes this column's cell bytes to its value; supplied by the selected driver. */
|
|
8
|
+
decode: (chunk: Buffer, start: number, length: number) => any;
|
|
9
|
+
};
|
|
4
10
|
/**
|
|
5
11
|
* A lazy wire-row {@link RowData} over raw Postgres `DataRow` payload bytes.
|
|
6
12
|
*
|
|
@@ -8,11 +14,11 @@ import pg from "pg";
|
|
|
8
14
|
* or appended across queries, and the payload is read-only after the query completes (entity
|
|
9
15
|
* mutations go into `InstanceData.data`, never back into the row bytes).
|
|
10
16
|
*
|
|
11
|
-
* Rows are kept in their row-major wire format (`int16 fieldCount` + length-prefixed cells) as
|
|
17
|
+
* Rows are kept in their row-major wire format (optional `int16 fieldCount` + length-prefixed cells) as
|
|
12
18
|
* zero-copy views over the parser's own immutable buffers: `addRow` records each DataRow's
|
|
13
19
|
* `(bytes, offset, length)` by reference, deduping the socket chunk that consecutive rows
|
|
14
|
-
* share.
|
|
15
|
-
*
|
|
20
|
+
* share. The selected driver guarantees immutable bytes: node-pg through `patchPgProtocol.ts`,
|
|
21
|
+
* PostgreJS through its public RowDecoder contract. A
|
|
16
22
|
* `row × column` cell is decoded on first field access by scanning the row's length-prefixed
|
|
17
23
|
* cells to the column's ordinal ("row-lazy" decode, see JS-ROW-STORE-DESIGN.md §3/C2). This is
|
|
18
24
|
* deferred decoding of row-major data, not a columnar layout.
|
|
@@ -21,12 +27,11 @@ import pg from "pg";
|
|
|
21
27
|
* protocol frames share it); `finalize`'s compaction copies retained rows out into an
|
|
22
28
|
* exact-size owned buffer when enough of the payload was dropped.
|
|
23
29
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* at all — it round-trips cells through a UTF-8 string, corrupting bytes >= 0x80.)
|
|
30
|
+
* Column decoders come from the selected driver through `setColumnDecoders`. RowDataQuery
|
|
31
|
+
* resolves node-pg text fields through TypeOverrides and binary fields through Joist's binary
|
|
32
|
+
* registry. PostgreJsDriver supplies its native parsers, bypassing pg's parser resolution.
|
|
33
|
+
* (Classic node-postgres cannot safely decode binary cells: it round-trips
|
|
34
|
+
* them through a UTF-8 string, corrupting bytes >= 0x80.)
|
|
30
35
|
*
|
|
31
36
|
* Because decoding is deferred, a custom parser that throws will do so on first field access
|
|
32
37
|
* (or `toRow`/`toRows`), not while awaiting the query; `id` and inheritance-discriminator cells
|
|
@@ -45,8 +50,16 @@ import pg from "pg";
|
|
|
45
50
|
* scan cursor, reading all ~36 columns of every row measures ~parity with classic end-to-end
|
|
46
51
|
* (benchmark-lazy-parsing.ts), and small finds (n <= 10) are statistically identical.
|
|
47
52
|
*/
|
|
48
|
-
declare class WireRowData implements RowData {
|
|
53
|
+
export declare class WireRowData implements RowData {
|
|
49
54
|
#private;
|
|
55
|
+
private readonly rowHeaderBytes;
|
|
56
|
+
/** PostgreJS omits the int16 field count from the immutable row view it exposes. */
|
|
57
|
+
constructor(rowHeaderBytes?: 0 | 2);
|
|
58
|
+
/** Installs decoders supplied by a driver with a public raw-row API. */
|
|
59
|
+
setColumnDecoders(fields: Array<{
|
|
60
|
+
name: string;
|
|
61
|
+
decode: WireColumn["decode"];
|
|
62
|
+
}>): void;
|
|
50
63
|
get rowCount(): number;
|
|
51
64
|
/** The DataRow payload bytes currently indexed; drops when compaction discards rows. */
|
|
52
65
|
get payloadBytes(): number;
|
|
@@ -57,24 +70,9 @@ declare class WireRowData implements RowData {
|
|
|
57
70
|
toRow(rowIndex: number): any;
|
|
58
71
|
/** Materializes classic POJO rows, i.e. for `afterFind` observation or debugging; not cached. */
|
|
59
72
|
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
73
|
/**
|
|
76
74
|
* Records one DataRow payload *by reference* (zero-copy); called synchronously from the wire
|
|
77
|
-
* parser, whose
|
|
75
|
+
* parser, whose buffer ownership contract guarantees the bytes are never rewritten.
|
|
78
76
|
*
|
|
79
77
|
* Consecutive rows usually share one socket chunk, so `bytes` is deduped against the last
|
|
80
78
|
* chunk ref; retaining any row of a chunk pins the whole chunk, which `finalize`'s compaction
|
|
@@ -101,11 +99,6 @@ declare class WireRowData implements RowData {
|
|
|
101
99
|
*/
|
|
102
100
|
finalize(): void;
|
|
103
101
|
}
|
|
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
102
|
//#endregion
|
|
110
|
-
export {
|
|
103
|
+
export {};
|
|
111
104
|
//# sourceMappingURL=WireRowData.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WireRowData.d.mts","names":[],"sources":["../../src/drivers/WireRowData.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"WireRowData.d.mts","names":[],"sources":["../../src/drivers/WireRowData.ts"],"mappings":";;;KAGK;EACH;EACA;;EAEA,SAAS,OAAO,QAAQ,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA8D5B,uBAAuB;;mBAEL;;EAA7B,YAA6B;;EAG7B,kBAAkB,QAAQ;IAAQ;IAAc,QAAQ;;MAiBpD;;MAKA;;MAKA;EAMJ,IAAI,kBAAkB;;EAWtB,MAAM;;EAsBN;;;;;;;;;EAcA,OAAO,OAAO,QAAQ,gBAAgB;;EAwBtC,OAAO;;;;;;;;;;;;;;;;;EAoBP"}
|