Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

materialize.md

56 lines1.4 KB

1---
2id: materialize
3title: materialize
4---
5​
6```ts
7function materialize<TContext>(query): MaterializeWrapper<GetInlineResult<TContext>, TContext extends SingleResult ? true : false>;
8```
9​
10Defined in: [packages/db/src/query/builder/functions.ts:878](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L878)
11​
12Materialize an includes subquery into a plain value on the parent row.
13​
14- For multi-row subqueries, the parent receives an `Array<T>` snapshot
15 (equivalent to `toArray()`).
16- For `findOne()` subqueries, the parent receives a single `T | undefined`
17 value — `undefined` when no child matches.
18​
19The snapshot updates reactively: parent rows re-emit when the underlying
20children change.
21​
22## Type Parameters
23​
24### TContext
25​
26`TContext` *extends* [`Context`](../interfaces/Context.md)
27​
28## Parameters
29​
30### query
31​
32[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
33​
34## Returns
35​
36`MaterializeWrapper`\<`GetInlineResult`\<`TContext`\>, `TContext` *extends* [`SingleResult`](../type-aliases/SingleResult.md) ? `true` : `false`\>
37​
38## Example
39​
40```ts
41// Multi-row: produces Array<Issue> on each project
42select(({ p }) => ({
43 ...p,
44 issues: materialize(
45 q.from({ i: issues }).where(({ i }) => eq(i.projectId, p.id)),
46 ),
47}))
48​
49// Singleton: produces Author | undefined on each post
50select(({ p }) => ({
51 ...p,
52 author: materialize(
53 q.from({ a: authors }).where(({ a }) => eq(a.id, p.authorId)).findOne(),
54 ),
55}))
56```