feat: add materialize() helper for includes subqueries#1504
Open
feat: add materialize() helper for includes subqueries#1504
Conversation
materialize() resolves to Array<T> for multi-row subqueries and T | undefined for findOne() subqueries, so callers don't have to unwrap a singleton array when the child query returns at most one row. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Contributor
|
Size Change: +126 B (+0.11%) Total Size: 114 kB 📦 View Changed
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 4.24 kB ℹ️ View Unchanged
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1481.
Summary
materialize()— a single helper for materializing includes subqueries onto parent rows. It resolves toArray<T>when the wrapped subquery returns multiple rows, and toT | undefinedwhen the subquery isfindOne(). The intent is to spare callers from unwrapping a singleton array whenever they know the child query yields at most one row.toArray()is unchanged — it still always producesArray<T>.materialize()is purely additive.IncludesMaterializationenum with'singleton', threads it through the IR / compiler / runtime, and updates thematerializeIncludedValuehelper to return the first child row (orundefined) for the singleton case. The reactive re-emit machinery routes through that helper, so insert/update/delete/no-match transitions all flow naturally.materialize(q.from(...).findOne())infersT | undefined,materialize(q.from(...))infersArray<T>. Detected at the type level via a phantomIsSingleflag onMaterializeWrapperthat's set fromTContext extends SingleResult.materialize()is also rejected inside expressions (coalesce(),eq(), …) with the same kind of error message thattoArray()produces.Example
A changeset will follow in a separate commit.
Test plan
tests/query/includes.test.tscover singleton attach, missing-match →undefined, insert / update / delete reactivity, two parents sharing a correlation key, multi-row fallback, scalar singleton, and nestedmaterialize(array of singletons).tests/query/includes.test-d.tscover array, singleton (with select / without select / scalar), and a nested array-of-singleton case.packages/dbtest suite passes (1502 tests, 0 type errors).🤖 Generated with Claude Code