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