@pgxsinkit/react 0.1.36 → 0.1.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,4 +15,21 @@ export type SelectedFields = Record<string, unknown>;
15
15
  * - with no field map (a raw query) the row is returned unchanged.
16
16
  */
17
17
  export declare function remapLiveRow(selectedFields: SelectedFields | undefined, row: Record<string, unknown>): Record<string, unknown>;
18
+ /**
19
+ * Flatten a Drizzle select's `selectedFields` into the ordered list of UNIQUE output aliases needed to
20
+ * make its live query safe to materialise. The walk order (depth-first over the field keys, recursing
21
+ * into nested selection objects) matches Drizzle's column-emission order in the compiled SQL, so the
22
+ * `index`-th leaf here is the `index`-th output column there — the invariant the positional
23
+ * column-alias-list depends on. Returns `undefined` for a raw query (no field map): the seam then leaves
24
+ * the SQL unwrapped and rows stay keyed by the underlying column names ({@link remapLiveRow}).
25
+ */
26
+ export declare function liveFieldAliases(selectedFields: SelectedFields | undefined): string[] | undefined;
27
+ /**
28
+ * Map a live row that came back from the ALIAS-WRAPPED query (keyed by the positional `__pgx_cN` aliases
29
+ * {@link liveFieldAliases} produced) onto the Drizzle select's field keys. Walks `selectedFields` in the
30
+ * same order, so the Nth leaf reads `row[__pgx_cN]`; nested selections recurse. The alias-space
31
+ * counterpart to {@link remapLiveRow} — which keys by the underlying column name and cannot survive a
32
+ * same-named-column collision. Returns the row unchanged when there is no field map (a raw query).
33
+ */
34
+ export declare function remapAliasedLiveRow(selectedFields: SelectedFields | undefined, row: Record<string, unknown>): Record<string, unknown>;
18
35
  //# sourceMappingURL=remap-live-row.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"remap-live-row.d.ts","sourceRoot":"","sources":["../src/remap-live-row.ts"],"names":[],"mappings":"AAEA,6GAA6G;AAC7G,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAErD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAC1B,cAAc,EAAE,cAAc,GAAG,SAAS,EAC1C,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC3B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAezB"}
1
+ {"version":3,"file":"remap-live-row.d.ts","sourceRoot":"","sources":["../src/remap-live-row.ts"],"names":[],"mappings":"AAEA,6GAA6G;AAC7G,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAErD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAC1B,cAAc,EAAE,cAAc,GAAG,SAAS,EAC1C,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC3B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAezB;AAqBD;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,cAAc,EAAE,cAAc,GAAG,SAAS,GAAG,MAAM,EAAE,GAAG,SAAS,CAgBjG;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,cAAc,EAAE,cAAc,GAAG,SAAS,EAC1C,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC3B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAmBzB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgxsinkit/react",
3
- "version": "0.1.36",
3
+ "version": "0.1.41",
4
4
  "keywords": [
5
5
  "tanstack-intent"
6
6
  ],
@@ -27,8 +27,8 @@
27
27
  "build": "bun --cwd ../.. run build:public-packages"
28
28
  },
29
29
  "dependencies": {
30
- "@pgxsinkit/client": "0.1.36",
31
- "@pgxsinkit/contracts": "0.1.36"
30
+ "@pgxsinkit/client": "0.1.41",
31
+ "@pgxsinkit/contracts": "0.1.41"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "@electric-sql/pglite": "0.5.4",
@@ -12,7 +12,7 @@ metadata:
12
12
  type: framework
13
13
  framework: react
14
14
  library: "@pgxsinkit/react"
15
- library_version: "0.1.32"
15
+ library_version: "0.1.41"
16
16
  source: https://pgxsinkit.github.io/
17
17
  requires:
18
18
  - react
@@ -4,7 +4,7 @@ import { createContext, type DependencyList, type ReactNode, useContext, useEffe
4
4
  import { type ClientPGlite, type LiveRowsSubscription, type SyncClient, syncDebug } from "@pgxsinkit/client";
5
5
  import type { SyncTableName, SyncTableRegistry } from "@pgxsinkit/contracts";
6
6
 
7
- import { remapLiveRow, type SelectedFields } from "./remap-live-row";
7
+ import { liveFieldAliases, remapAliasedLiveRow, remapLiveRow, type SelectedFields } from "./remap-live-row";
8
8
 
9
9
  /**
10
10
  * Minimal interface satisfied by every Drizzle select/query builder.
@@ -210,10 +210,18 @@ export function createSyncClientHooks<TRegistry extends SyncTableRegistry>() {
210
210
  }
211
211
 
212
212
  const { sql, selectedFields } = queryInfo;
213
- // PGlite's live query returns rows keyed by the underlying (snake_case) column names; remap them
214
- // back to the Drizzle select's field keys so the rows match the builder's inferred (camelCase) type.
213
+ // Render the query safe to MATERIALISE: hand the seam the select's unique field aliases so it wraps a
214
+ // JOIN with same-named columns (two `title`) under a positional column-alias-list — otherwise PGlite's
215
+ // live query refuses it (`column "title" specified more than once`) and same-named columns collapse.
216
+ // With `fields` the rows come back keyed by those aliases, so map by alias; without a field map (a raw
217
+ // query) the seam leaves the SQL unwrapped and rows stay name-keyed (`remapLiveRow`).
218
+ const fields = liveFieldAliases(selectedFields);
215
219
  const mapRows = (rows: readonly unknown[]): TRows =>
216
- rows.map((row) => remapLiveRow(selectedFields, row as Record<string, unknown>)) as unknown as TRows;
220
+ rows.map((row) =>
221
+ fields
222
+ ? remapAliasedLiveRow(selectedFields, row as Record<string, unknown>)
223
+ : remapLiveRow(selectedFields, row as Record<string, unknown>),
224
+ ) as unknown as TRows;
217
225
 
218
226
  let active = true;
219
227
  let subscription: LiveRowsSubscription<Record<string, unknown>> | undefined;
@@ -231,7 +239,7 @@ export function createSyncClientHooks<TRegistry extends SyncTableRegistry>() {
231
239
  setState((prev) => ({ rows: prev.rows, loading: true, hydrating: false, error: null }));
232
240
  return contextClient
233
241
  .subscribeLiveRows<Record<string, unknown>>(
234
- { sql: sql.sql, params: sql.params, ...(useList ? { use: useList } : {}) },
242
+ { sql: sql.sql, params: sql.params, ...(fields ? { fields } : {}), ...(useList ? { use: useList } : {}) },
235
243
  (rows) => {
236
244
  if (active) {
237
245
  syncDebug("live query updated → re-render", { rows: rows.length });
@@ -36,3 +36,79 @@ export function remapLiveRow(
36
36
  }
37
37
  return out;
38
38
  }
39
+
40
+ // ─── Materialization-safe live reads (JOINs with same-named columns) ─────────────────────────────────
41
+ //
42
+ // Reading by the underlying column NAME (as `remapLiveRow` does) is unsound when a JOIN's tables share a
43
+ // column name — `select({ courseTitle: course.title, moduleTitle: module.title })` compiles to a SELECT
44
+ // with two `title` output columns. PGlite's `live` extension MATERIALISES the query and fails hard
45
+ // (`column "title" specified more than once`); even a plain query would silently collapse both `title`s
46
+ // into one value. The seam (`client.subscribeLiveRows`) fixes this by wrapping the query with a POSITIONAL
47
+ // column-alias-list when it is handed `fields` — the unique aliases below — so every output column is
48
+ // renamed to a distinct name. Rows then come back keyed by THOSE aliases, and `remapAliasedLiveRow` maps
49
+ // them onto the select's field keys positionally.
50
+
51
+ /** The prefix for the positional output aliases the live-rows seam renames a materialised query's columns to. */
52
+ const LIVE_FIELD_ALIAS_PREFIX = "__pgx_c";
53
+
54
+ /** Assign a positional alias to the `index`-th output column: `__pgx_c0`, `__pgx_c1`, … (unique by index). */
55
+ function liveFieldAliasAt(index: number): string {
56
+ return `${LIVE_FIELD_ALIAS_PREFIX}${index}`;
57
+ }
58
+
59
+ /**
60
+ * Flatten a Drizzle select's `selectedFields` into the ordered list of UNIQUE output aliases needed to
61
+ * make its live query safe to materialise. The walk order (depth-first over the field keys, recursing
62
+ * into nested selection objects) matches Drizzle's column-emission order in the compiled SQL, so the
63
+ * `index`-th leaf here is the `index`-th output column there — the invariant the positional
64
+ * column-alias-list depends on. Returns `undefined` for a raw query (no field map): the seam then leaves
65
+ * the SQL unwrapped and rows stay keyed by the underlying column names ({@link remapLiveRow}).
66
+ */
67
+ export function liveFieldAliases(selectedFields: SelectedFields | undefined): string[] | undefined {
68
+ if (selectedFields == null) return undefined;
69
+ const aliases: string[] = [];
70
+ const walk = (fields: SelectedFields): void => {
71
+ for (const field of Object.values(fields)) {
72
+ if (is(field, Column) || is(field, SQL) || is(field, SQL.Aliased)) {
73
+ aliases.push(liveFieldAliasAt(aliases.length));
74
+ } else if (field != null && typeof field === "object") {
75
+ walk(field as SelectedFields);
76
+ } else {
77
+ aliases.push(liveFieldAliasAt(aliases.length));
78
+ }
79
+ }
80
+ };
81
+ walk(selectedFields);
82
+ return aliases.length > 0 ? aliases : undefined;
83
+ }
84
+
85
+ /**
86
+ * Map a live row that came back from the ALIAS-WRAPPED query (keyed by the positional `__pgx_cN` aliases
87
+ * {@link liveFieldAliases} produced) onto the Drizzle select's field keys. Walks `selectedFields` in the
88
+ * same order, so the Nth leaf reads `row[__pgx_cN]`; nested selections recurse. The alias-space
89
+ * counterpart to {@link remapLiveRow} — which keys by the underlying column name and cannot survive a
90
+ * same-named-column collision. Returns the row unchanged when there is no field map (a raw query).
91
+ */
92
+ export function remapAliasedLiveRow(
93
+ selectedFields: SelectedFields | undefined,
94
+ row: Record<string, unknown>,
95
+ ): Record<string, unknown> {
96
+ if (selectedFields == null) return row;
97
+ const cursor = { index: 0 };
98
+ const walk = (fields: SelectedFields): Record<string, unknown> => {
99
+ const out: Record<string, unknown> = {};
100
+ for (const [key, field] of Object.entries(fields)) {
101
+ if (is(field, Column) || is(field, SQL) || is(field, SQL.Aliased)) {
102
+ out[key] = row[liveFieldAliasAt(cursor.index)];
103
+ cursor.index += 1;
104
+ } else if (field != null && typeof field === "object") {
105
+ out[key] = walk(field as SelectedFields);
106
+ } else {
107
+ out[key] = row[liveFieldAliasAt(cursor.index)];
108
+ cursor.index += 1;
109
+ }
110
+ }
111
+ return out;
112
+ };
113
+ return walk(selectedFields);
114
+ }