Skip to content
Source
Sign inSign up
TanStack/db

fix: resolve materialize(findOne()) to undefined when the correlation key is null #1707

ClosedMohith26(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/1707/headopened

Showing 4 changed files with 48 additions and 5 deletions (loading 1).

x
0 of 4 viewed, 4 left
5 additions and 0 deletions.changeset/shiny-donkeys-repeat.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/db': patch
+---
+​
+Fix: `materialize(q…findOne())` resolved to `null` instead of `undefined` when the correlation key itself was null. A null correlation key never reached the includes materialization step, so the compiler's `null` select placeholder leaked into the result, violating the typed `T | undefined` contract. Null correlation keys now resolve to the empty materialized value (`undefined` for `findOne()`, `[]` for arrays, `` for `concat`).
Thread on line 5

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Use a valid-looking query expression.

materialize(q…findOne()) is unclear and reads like invalid API syntax. Use materialize(q.from(...).findOne()) or prose instead.

Proposed fix
-Fix: `materialize(q…findOne())` resolved to `null` instead of `undefined`
+Fix: `materialize(q.from(...).findOne())` resolved to `null` instead of `undefined`
📝 Committable suggestion

‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Fix: `materialize(q.from(...).findOne())` resolved to `null` instead of `undefined` when the correlation key itself was null. A null correlation key never reached the includes materialization step, so the compiler's `null` select placeholder leaked into the result, violating the typed `T | undefined` contract. Null correlation keys now resolve to the empty materialized value (`undefined` for `findOne()`, `[]` for arrays, `` for `concat`).
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.changeset/shiny-donkeys-repeat.md at line 5, Update the changeset
description to replace the invalid-looking `materialize(q…findOne())` expression
with valid API syntax such as `materialize(q.from(...).findOne())`, or describe
the behavior in prose while preserving the existing null-correlation-key
details.
29 additions and 5 deletionspackages/db/src/query/compiler/index.tsModified
View file
@@ -346,6 +346,12 @@
getRouting: (nsRow: any) => {
correlationKey: unknown
parentContext: Record<string, any> | null
+ /**
+ * Set when this row does not participate in the include at all
+ * (conditional-select guards failed or no routing info was found),
+ * as opposed to participating with a null correlation key.
+ */