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

createLiveQueryCollection.md

137 lines3.6 KB

1---
2id: createLiveQueryCollection
3title: createLiveQueryCollection
4---
5​
6# Function: createLiveQueryCollection()
7​
8## Call Signature
9​
10```ts
11function createLiveQueryCollection<TContext, TResult>(query): CollectionForContext<TContext, TResult, {
12}> & object;
13```
14​
15Defined in: [packages/db/src/query/live-query-collection.ts:115](https://github.com/TanStack/db/blob/main/packages/db/src/query/live-query-collection.ts#L115)
16​
17Creates a live query collection directly
18​
19### Type Parameters
20​
21#### TContext
22​
23`TContext` *extends* [`Context`](../interfaces/Context.md)
24​
25#### TResult
26​
27`TResult` *extends* `object` = \{ \[K in string \| number \| symbol\]: (TContext\["result"\] extends object ? any\[any\] : TContext\["hasJoins"\] extends true ? TContext\["schema"\] : TContext\["schema"\]\[TContext\["fromSourceName"\]\])\[K\] \}
28​
29### Parameters
30​
31#### query
32​
33(`q`) => [`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
34​
35### Returns
36​
37`CollectionForContext`\<`TContext`, `TResult`, \{
38\}\> & `object`
39​
40### Example
41​
42```typescript
43// Minimal usage - just pass a query function
44const activeUsers = createLiveQueryCollection(
45 (q) => q
46 .from({ user: usersCollection })
47 .where(({ user }) => eq(user.active, true))
48 .select(({ user }) => ({ id: user.id, name: user.name }))
50​
51// Full configuration with custom options
52const searchResults = createLiveQueryCollection({
53 id: "search-results", // Custom ID (auto-generated if omitted)
54 query: (q) => q
55 .from({ post: postsCollection })
56 .where(({ post }) => like(post.title, `%${searchTerm}%`))
57 .select(({ post }) => ({
58 id: post.id,
59 title: post.title,
60 excerpt: post.excerpt,
61 })),
62 getKey: (item) => item.id, // Custom key function (uses stream key if omitted)
63 utils: {
64 updateSearchTerm: (newTerm: string) => {
65 // Custom utility functions
66 }
67 }
68})
69```
70​
71## Call Signature
72​
73```ts
74function createLiveQueryCollection<TContext, TResult, TUtils>(config): CollectionForContext<TContext, TResult, {
75}> & object;
76```
77​
78Defined in: [packages/db/src/query/live-query-collection.ts:125](https://github.com/TanStack/db/blob/main/packages/db/src/query/live-query-collection.ts#L125)
79​
80Creates a live query collection directly
81​
82### Type Parameters
83​
84#### TContext
85​
86`TContext` *extends* [`Context`](../interfaces/Context.md)
87​
88#### TResult
89​
90`TResult` *extends* `object` = \{ \[K in string \| number \| symbol\]: (TContext\["result"\] extends object ? any\[any\] : TContext\["hasJoins"\] extends true ? TContext\["schema"\] : TContext\["schema"\]\[TContext\["fromSourceName"\]\])\[K\] \}
91​
92#### TUtils
93​
94`TUtils` *extends* [`UtilsRecord`](../type-aliases/UtilsRecord.md) = \{
95\}
96​
97### Parameters
98​
99#### config
100​
101[`LiveQueryCollectionConfig`](../interfaces/LiveQueryCollectionConfig.md)\<`TContext`, `TResult`\> & `object`
102​
103### Returns
104​
105`CollectionForContext`\<`TContext`, `TResult`, \{
106\}\> & `object`
107​
108### Example
109​
110```typescript
111// Minimal usage - just pass a query function
112const activeUsers = createLiveQueryCollection(
113 (q) => q
114 .from({ user: usersCollection })
115 .where(({ user }) => eq(user.active, true))
116 .select(({ user }) => ({ id: user.id, name: user.name }))
118​
119// Full configuration with custom options
120const searchResults = createLiveQueryCollection({
121 id: "search-results", // Custom ID (auto-generated if omitted)
122 query: (q) => q
123 .from({ post: postsCollection })
124 .where(({ post }) => like(post.title, `%${searchTerm}%`))
125 .select(({ post }) => ({
126 id: post.id,
127 title: post.title,
128 excerpt: post.excerpt,
129 })),
130 getKey: (item) => item.id, // Custom key function (uses stream key if omitted)
131 utils: {
132 updateSearchTerm: (newTerm: string) => {
133 // Custom utility functions
134 }
135 }
136})
137```