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

materialize.md

58 lines1.5 KB

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