Skip to content
Source
Sign inSign up
TanStack/db

feat(electric-db-collection): support nested property references in SQL queries #1514

Closedmiguelrk(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/1514/headopened

Showing 3 changed files with 130 additions and 8 deletions.

x
0 of 3 viewed, 3 left
11 additions and 0 deletions.changeset/electric-sql-nested-json-refs.mdAdded
View file
@@ -0,0 +1,11 @@
+---
+'@tanstack/electric-db-collection': patch
+---
+​
+Support nested property refs in WHERE/ORDER BY by compiling to PostgreSQL JSON/`jsonb` operators (`->` / `->>`).
+​
+Previously, multi-segment IR refs threw during SQL compilation. They now compile to safe JSON traversal: intermediate keys use `->`, the final key uses `->>` (text). Keys are emitted as SQL string literals with proper quote escaping.
+​
+**Limitation:** Nested paths apply only to JSON/`jsonb` extraction from a single root column—not Postgres composite types or dotted column names. If the physical column is not `json`/`jsonb`, the query may fail at runtime.
+​
+**Consumer impact:** No API changes. Queries that already used nested field refs against Electric subset loading can now generate valid SQL when the backing column is JSON/`jsonb`.
46 additions and 7 deletionspackages/electric-db-collection/src/sql-compiler.tsModified
View file
@@ -92,6 +92,51 @@
​
/**
+ * Escape content for use inside a PostgreSQL single-quoted string literal (`'...'`).
+ */
+const escapeSqlSingleQuotedString = (value: string): string =>
+ value.replace(/'/g, `''`)
+​
+/**
+ * A SQL single-quoted string literal with standard quote doubling (e.g. `O'Brien` → `'O''Brien'`).