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

LiveQueryCollectionConfig.md

183 lines4.1 KB

1---
2id: LiveQueryCollectionConfig
3title: LiveQueryCollectionConfig
4---
5​
6Defined in: [packages/db/src/query/live/types.ts:65](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L65)
7​
8Configuration interface for live query collection options
9​
10## Example
11​
12```typescript
13const config: LiveQueryCollectionConfig<any, any> = {
14 // id is optional - will auto-generate "live-query-1", "live-query-2", etc.
15 query: (q) => q
16 .from({ comment: commentsCollection })
17 .join(
18 { user: usersCollection },
19 ({ comment, user }) => eq(comment.user_id, user.id)
20 )
21 .where(({ comment }) => eq(comment.active, true))
22 .select(({ comment, user }) => ({
23 id: comment.id,
24 content: comment.content,
25 authorName: user.name,
26 })),
27 // getKey is optional - defaults to using stream key
28 getKey: (item) => item.id,
30```
31​
32## Type Parameters
33​
34### TContext
35​
36`TContext` *extends* [`Context`](Context.md)
37​
38### TResult
39​
40`TResult` *extends* `object` = `RootQueryResult`\<`TContext`\>
41​
42## Properties
43​
44### defaultStringCollation?
45​
46```ts
47optional defaultStringCollation: StringCollationConfig;
48```
49​
50Defined in: [packages/db/src/query/live/types.ts:121](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L121)
51​
52Optional compare options for string sorting.
53If provided, these will be used instead of inheriting from the FROM collection.
54​
55***
56​
57### gcTime?
58​
59```ts
60optional gcTime: number;
61```
62​
63Defined in: [packages/db/src/query/live/types.ts:110](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L110)
64​
65GC time for the collection
66​
67***
68​
69### getKey()?
70​
71```ts
72optional getKey: (item) => string | number;
73```
74​
75Defined in: [packages/db/src/query/live/types.ts:88](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L88)
76​
77Function to extract the key from result items
78If not provided, defaults to using the key from the D2 stream
79​
80#### Parameters
81​
82##### item
83​
84`TResult`
85​
86#### Returns
87​
88`string` \| `number`
89​
90***
91​
92### id?
93​
94```ts
95optional id: string;
96```
97​
98Defined in: [packages/db/src/query/live/types.ts:73](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L73)
99​
100Unique identifier for the collection
101If not provided, defaults to `live-query-${number}` with auto-incrementing number
102​
103***
104​
105### onDelete?
106​
107```ts
108optional onDelete: DeleteMutationFn<TResult, string | number, UtilsRecord, any>;
109```
110​
111Defined in: [packages/db/src/query/live/types.ts:100](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L100)
112​
113***
114​
115### onInsert?
116​
117```ts
118optional onInsert: InsertMutationFn<TResult, string | number, UtilsRecord, any>;
119```
120​
121Defined in: [packages/db/src/query/live/types.ts:98](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L98)
122​
123Optional mutation handlers
124​
125***
126​
127### onUpdate?
128​
129```ts
130optional onUpdate: UpdateMutationFn<TResult, string | number, UtilsRecord, any>;
131```
132​
133Defined in: [packages/db/src/query/live/types.ts:99](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L99)
134​
135***
136​
137### query
138​
139```ts
140query:
141 | (q) => QueryBuilder<TContext> & RootObjectResultConstraint<TContext>
142| QueryBuilder<TContext> & RootObjectResultConstraint<TContext>;
143```
144​
145Defined in: [packages/db/src/query/live/types.ts:78](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L78)
146​
147Query builder function that defines the live query
148​
149***
150​
151### schema?
152​
153```ts
154optional schema: undefined;
155```
156​
157Defined in: [packages/db/src/query/live/types.ts:93](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L93)
158​
159Optional schema for validation
160​
161***
162​
163### singleResult?
164​
165```ts
166optional singleResult: true;
167```
168​
169Defined in: [packages/db/src/query/live/types.ts:115](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L115)
170​
171If enabled the collection will return a single object instead of an array
172​
173***
174​
175### startSync?
176​
177```ts
178optional startSync: boolean;
179```
180​
181Defined in: [packages/db/src/query/live/types.ts:105](https://github.com/TanStack/db/blob/main/packages/db/src/query/live/types.ts#L105)
182​
183Start sync / the query immediately