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<TQueryFn, TQuery>(query): CollectionForContext<ExtractContext<TQuery>, RootQueryResult<ExtractContext<TQuery>>, {
12}> & object;
13```
14​
15Defined in: [packages/db/src/query/live-query-collection.ts:128](https://github.com/TanStack/db/blob/main/packages/db/src/query/live-query-collection.ts#L128)
16​
17Creates a live query collection directly
18​
19### Type Parameters
20​
21#### TQueryFn
22​
23`TQueryFn` *extends* (`q`) => [`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`any`\>
24​
25#### TQuery
26​
27`TQuery` *extends* [`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`any`\> = `ReturnType`\<`TQueryFn`\>
28​
29### Parameters
30​
31#### query
32​
33`TQueryFn` & `RootQueryFn`\<`TQuery`\>
34​
35### Returns
36​
37`CollectionForContext`\<[`ExtractContext`](../type-aliases/ExtractContext.md)\<`TQuery`\>, `RootQueryResult`\<[`ExtractContext`](../type-aliases/ExtractContext.md)\<`TQuery`\>\>, \{
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<TQuery, TContext, TUtils>(config): CollectionForContext<TContext, RootQueryResult<TContext>, {
75}> & object;
76```
77​
78Defined in: [packages/db/src/query/live-query-collection.ts:141](https://github.com/TanStack/db/blob/main/packages/db/src/query/live-query-collection.ts#L141)
79​
80Creates a live query collection directly
81​
82### Type Parameters
83​
84#### TQuery
85​
86`TQuery` *extends* [`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`any`\>
87​
88#### TContext
89​
90`TContext` *extends* [`Context`](../interfaces/Context.md) = [`ExtractContext`](../type-aliases/ExtractContext.md)\<`TQuery`\>
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`, `RootQueryResult`\<`TContext`\>\> & `object`
102​
103### Returns
104​
105`CollectionForContext`\<`TContext`, `RootQueryResult`\<`TContext`\>, \{
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```