MCPcopy Index your code
hub / github.com/TanStack/db / createLiveQueryCollection

Function createLiveQueryCollection

packages/db/src/query/live-query-collection.ts:155–203  ·  view source on GitHub ↗
(
  configOrQuery:
    | (LiveQueryCollectionConfig<TContext, TResult> & { utils?: TUtils })
    | ((
        q: InitialQueryBuilder,
      ) => QueryBuilder<TContext> & RootObjectResultConstraint<TContext>),
)

Source from the content-addressed store, hash-verified

153
154// Implementation
155export function createLiveQueryCollection<
156 TContext extends Context,
157 TResult extends object = RootQueryResult<TContext>,
158 TUtils extends UtilsRecord = {},
159>(
160 configOrQuery:
161 | (LiveQueryCollectionConfig<TContext, TResult> & { utils?: TUtils })
162 | ((
163 q: InitialQueryBuilder,
164 ) => QueryBuilder<TContext> & RootObjectResultConstraint<TContext>),
165): CollectionForContext<TContext, TResult> & {
166 utils: LiveQueryCollectionUtils & TUtils
167} {
168 // Determine if the argument is a function (query) or a config object
169 if (typeof configOrQuery === `function`) {
170 // Simple query function case
171 const config: LiveQueryCollectionConfig<TContext, TResult> = {
172 query: configOrQuery as (
173 q: InitialQueryBuilder,
174 ) => QueryBuilder<TContext> & RootObjectResultConstraint<TContext>,
175 }
176 // The implementation accepts both overload shapes, but TypeScript cannot
177 // preserve the overload-specific query-builder inference through this branch.
178 const options = liveQueryCollectionOptions(config as any)
179 return bridgeToCreateCollection(options) as CollectionForContext<
180 TContext,
181 TResult
182 > & { utils: LiveQueryCollectionUtils & TUtils }
183 } else {
184 // Config object case
185 const config = configOrQuery as LiveQueryCollectionConfig<
186 TContext,
187 TResult
188 > & { utils?: TUtils }
189 // Same overload implementation limitation as above: the config has already
190 // been validated by the public signatures, but the branch loses that precision.
191 const options = liveQueryCollectionOptions(config as any)
192
193 // Merge custom utils if provided, preserving the getBuilder() method for dependency tracking
194 if (config.utils) {
195 options.utils = { ...options.utils, ...config.utils }
196 }
197
198 return bridgeToCreateCollection(options) as CollectionForContext<
199 TContext,
200 TResult
201 > & { utils: LiveQueryCollectionUtils & TUtils }
202 }
203}
204
205/**
206 * Bridge function that handles the type compatibility between query2's TResult

Callers 15

useLiveQueryFunction · 0.90
createMutationsTestSuiteFunction · 0.90
createJoinsTestSuiteFunction · 0.90
createCollationTestSuiteFunction · 0.90
useLiveQueryFunction · 0.90

Calls 2

bridgeToCreateCollectionFunction · 0.85

Tested by 15

createBasicTestsFunction · 0.68
testJoinTypeFunction · 0.68
createJoinTestsFunction · 0.68
createSubqueryTestsFunction · 0.68
createJoinSubqueryTestsFunction · 0.68
createDistinctTestsFunction · 0.68
buildIncludesQueryFunction · 0.68
buildNestedQueryFunction · 0.68
buildToArrayQueryFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…