function useLiveQuery<TContext>(queryFn, deps?): UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>;
function useLiveQuery<TContext>(queryFn, deps?): UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>;
Defined in: useLiveQuery.svelte.ts:155
Create a live query using a query function
TContext extends Context
(q) => QueryBuilder<TContext>
Query function that defines what data to fetch
() => unknown[]
Array of reactive dependencies that trigger query re-execution when changed
UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>
Reactive object with query data, state, and status information
IMPORTANT - Destructuring in Svelte 5: Direct destructuring breaks reactivity. To destructure, wrap with $derived:
❌ Incorrect - Loses reactivity:
const { data, isLoading } = useLiveQuery(...)
const { data, isLoading } = useLiveQuery(...)
✅ Correct - Maintains reactivity:
// Option 1: Use dot notation (recommended)
const query = useLiveQuery(...)
// Access: query.data, query.isLoading
// Option 2: Wrap with $derived for destructuring
const query = useLiveQuery(...)
const { data, isLoading } = $derived(query)
// Option 1: Use dot notation (recommended)
const query = useLiveQuery(...)
// Access: query.data, query.isLoading
// Option 2: Wrap with $derived for destructuring
const query = useLiveQuery(...)
const { data, isLoading } = $derived(query)
This is a fundamental Svelte 5 limitation, not a library bug. See: https://github.com/sveltejs/svelte/issues/11002
// Basic query with object syntax (recommended pattern)
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.completed, false))
.select(({ todos }) => ({ id: todos.id, text: todos.text }))
)
// Access via: todosQuery.data, todosQuery.isLoading, etc.
// Basic query with object syntax (recommended pattern)
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.completed, false))
.select(({ todos }) => ({ id: todos.id, text: todos.text }))
)
// Access via: todosQuery.data, todosQuery.isLoading, etc.
// With reactive dependencies
let minPriority = $state(5)
const todosQuery = useLiveQuery(
(q) => q.from({ todos: todosCollection })
.where(({ todos }) => gt(todos.priority, minPriority)),
[() => minPriority] // Re-run when minPriority changes
)
// With reactive dependencies
let minPriority = $state(5)
const todosQuery = useLiveQuery(
(q) => q.from({ todos: todosCollection })
.where(({ todos }) => gt(todos.priority, minPriority)),
[() => minPriority] // Re-run when minPriority changes
)
// Destructuring with $derived (if needed)
const query = useLiveQuery((q) =>
q.from({ todos: todosCollection })
)
const { data, isLoading, isError } = $derived(query)
// Now data, isLoading, and isError maintain reactivity
// Destructuring with $derived (if needed)
const query = useLiveQuery((q) =>
q.from({ todos: todosCollection })
)
const { data, isLoading, isError } = $derived(query)
// Now data, isLoading, and isError maintain reactivity
// Join pattern
const issuesQuery = useLiveQuery((q) =>
q.from({ issues: issueCollection })
.join({ persons: personCollection }, ({ issues, persons }) =>
eq(issues.userId, persons.id)
)
.select(({ issues, persons }) => ({
id: issues.id,
title: issues.title,
userName: persons.name
}))
)
// Join pattern
const issuesQuery = useLiveQuery((q) =>
q.from({ issues: issueCollection })
.join({ persons: personCollection }, ({ issues, persons }) =>
eq(issues.userId, persons.id)
)
.select(({ issues, persons }) => ({
id: issues.id,
title: issues.title,
userName: persons.name
}))
)
// Handle loading and error states in template
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
// In template:
// {#if todosQuery.isLoading}
// <div>Loading...</div>
// {:else if todosQuery.isError}
// <div>Error: {todosQuery.status}</div>
// {:else}
// <ul>
// {#each todosQuery.data as todo (todo.id)}
// <li>{todo.text}</li>
// {/each}
// </ul>
// {/if}
// Handle loading and error states in template
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
// In template:
// {#if todosQuery.isLoading}
// <div>Loading...</div>
// {:else if todosQuery.isError}
// <div>Error: {todosQuery.status}</div>
// {:else}
// <ul>
// {#each todosQuery.data as todo (todo.id)}
// <li>{todo.text}</li>
// {/each}
// </ul>
// {/if}
function useLiveQuery<TContext>(queryFn, deps?): UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>;
function useLiveQuery<TContext>(queryFn, deps?): UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>;
Defined in: useLiveQuery.svelte.ts:161
Create a live query using a query function
TContext extends Context
(q) => QueryBuilder<TContext> | null | undefined
Query function that defines what data to fetch
() => unknown[]
Array of reactive dependencies that trigger query re-execution when changed
UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>
Reactive object with query data, state, and status information
IMPORTANT - Destructuring in Svelte 5: Direct destructuring breaks reactivity. To destructure, wrap with $derived:
❌ Incorrect - Loses reactivity:
const { data, isLoading } = useLiveQuery(...)
const { data, isLoading } = useLiveQuery(...)
✅ Correct - Maintains reactivity:
// Option 1: Use dot notation (recommended)
const query = useLiveQuery(...)
// Access: query.data, query.isLoading
// Option 2: Wrap with $derived for destructuring
const query = useLiveQuery(...)
const { data, isLoading } = $derived(query)
// Option 1: Use dot notation (recommended)
const query = useLiveQuery(...)
// Access: query.data, query.isLoading
// Option 2: Wrap with $derived for destructuring
const query = useLiveQuery(...)
const { data, isLoading } = $derived(query)
This is a fundamental Svelte 5 limitation, not a library bug. See: https://github.com/sveltejs/svelte/issues/11002
// Basic query with object syntax (recommended pattern)
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.completed, false))
.select(({ todos }) => ({ id: todos.id, text: todos.text }))
)
// Access via: todosQuery.data, todosQuery.isLoading, etc.
// Basic query with object syntax (recommended pattern)
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.completed, false))
.select(({ todos }) => ({ id: todos.id, text: todos.text }))
)
// Access via: todosQuery.data, todosQuery.isLoading, etc.
// With reactive dependencies
let minPriority = $state(5)
const todosQuery = useLiveQuery(
(q) => q.from({ todos: todosCollection })
.where(({ todos }) => gt(todos.priority, minPriority)),
[() => minPriority] // Re-run when minPriority changes
)
// With reactive dependencies
let minPriority = $state(5)
const todosQuery = useLiveQuery(
(q) => q.from({ todos: todosCollection })
.where(({ todos }) => gt(todos.priority, minPriority)),
[() => minPriority] // Re-run when minPriority changes
)
// Destructuring with $derived (if needed)
const query = useLiveQuery((q) =>
q.from({ todos: todosCollection })
)
const { data, isLoading, isError } = $derived(query)
// Now data, isLoading, and isError maintain reactivity
// Destructuring with $derived (if needed)
const query = useLiveQuery((q) =>
q.from({ todos: todosCollection })
)
const { data, isLoading, isError } = $derived(query)
// Now data, isLoading, and isError maintain reactivity
// Join pattern
const issuesQuery = useLiveQuery((q) =>
q.from({ issues: issueCollection })
.join({ persons: personCollection }, ({ issues, persons }) =>
eq(issues.userId, persons.id)
)
.select(({ issues, persons }) => ({
id: issues.id,
title: issues.title,
userName: persons.name
}))
)
// Join pattern
const issuesQuery = useLiveQuery((q) =>
q.from({ issues: issueCollection })
.join({ persons: personCollection }, ({ issues, persons }) =>
eq(issues.userId, persons.id)
)
.select(({ issues, persons }) => ({
id: issues.id,
title: issues.title,
userName: persons.name
}))
)
// Handle loading and error states in template
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
// In template:
// {#if todosQuery.isLoading}
// <div>Loading...</div>
// {:else if todosQuery.isError}
// <div>Error: {todosQuery.status}</div>
// {:else}
// <ul>
// {#each todosQuery.data as todo (todo.id)}
// <li>{todo.text}</li>
// {/each}
// </ul>
// {/if}
// Handle loading and error states in template
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
// In template:
// {#if todosQuery.isLoading}
// <div>Loading...</div>
// {:else if todosQuery.isError}
// <div>Error: {todosQuery.status}</div>
// {:else}
// <ul>
// {#each todosQuery.data as todo (todo.id)}
// <li>{todo.text}</li>
// {/each}
// </ul>
// {/if}
function useLiveQuery<TContext>(config, deps?): UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>;
function useLiveQuery<TContext>(config, deps?): UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>;
Defined in: useLiveQuery.svelte.ts:206
Create a live query using configuration object
TContext extends Context
LiveQueryCollectionConfig<TContext>
Configuration object with query and options
() => unknown[]
Array of reactive dependencies that trigger query re-execution when changed
UseLiveQueryReturn<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>
Reactive object with query data, state, and status information
// Basic config object usage
const todosQuery = useLiveQuery({
query: (q) => q.from({ todos: todosCollection }),
gcTime: 60000
})
// Basic config object usage
const todosQuery = useLiveQuery({
query: (q) => q.from({ todos: todosCollection }),
gcTime: 60000
})
// With reactive dependencies
let filter = $state('active')
const todosQuery = useLiveQuery({
query: (q) => q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.status, filter))
}, [() => filter])
// With reactive dependencies
let filter = $state('active')
const todosQuery = useLiveQuery({
query: (q) => q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.status, filter))
}, [() => filter])
// Handle all states uniformly
const itemsQuery = useLiveQuery({
query: (q) => q.from({ items: itemCollection })
})
// In template:
// {#if itemsQuery.isLoading}
// <div>Loading...</div>
// {:else if itemsQuery.isError}
// <div>Something went wrong</div>
// {:else if !itemsQuery.isReady}
// <div>Preparing...</div>
// {:else}
// <div>{itemsQuery.data.length} items loaded</div>
// {/if}
// Handle all states uniformly
const itemsQuery = useLiveQuery({
query: (q) => q.from({ items: itemCollection })
})
// In template:
// {#if itemsQuery.isLoading}
// <div>Loading...</div>
// {:else if itemsQuery.isError}
// <div>Something went wrong</div>
// {:else if !itemsQuery.isReady}
// <div>Preparing...</div>
// {:else}
// <div>{itemsQuery.data.length} items loaded</div>
// {/if}
function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): UseLiveQueryReturnWithCollection<TResult, TKey, TUtils>;
function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): UseLiveQueryReturnWithCollection<TResult, TKey, TUtils>;
Defined in: useLiveQuery.svelte.ts:255
Subscribe to an existing query collection (can be reactive)
TResult extends object
TKey extends string | number
TUtils extends Record<string, any>
MaybeGetter<Collection<TResult, TKey, TUtils, StandardSchemaV1<unknown, unknown>, TResult>>
Pre-created query collection to subscribe to (can be a getter)
UseLiveQueryReturnWithCollection<TResult, TKey, TUtils>
Reactive object with query data, state, and status information
// Using pre-created query collection
const myLiveQuery = createLiveQueryCollection((q) =>
q.from({ todos: todosCollection }).where(({ todos }) => eq(todos.active, true))
)
const queryResult = useLiveQuery(myLiveQuery)
// Using pre-created query collection
const myLiveQuery = createLiveQueryCollection((q) =>
q.from({ todos: todosCollection }).where(({ todos }) => eq(todos.active, true))
)
const queryResult = useLiveQuery(myLiveQuery)
// Reactive query collection reference
let selectedQuery = $state(todosQuery)
const queryResult = useLiveQuery(() => selectedQuery)
// Switch queries reactively
selectedQuery = archiveQuery
// Reactive query collection reference
let selectedQuery = $state(todosQuery)
const queryResult = useLiveQuery(() => selectedQuery)
// Switch queries reactively
selectedQuery = archiveQuery
// Access query collection methods directly
const queryResult = useLiveQuery(existingQuery)
// Use underlying collection for mutations
const handleToggle = (id) => {
queryResult.collection.update(id, draft => { draft.completed = !draft.completed })
}
// Access query collection methods directly
const queryResult = useLiveQuery(existingQuery)
// Use underlying collection for mutations
const handleToggle = (id) => {
queryResult.collection.update(id, draft => { draft.completed = !draft.completed })
}
// Handle states consistently
const queryResult = useLiveQuery(sharedQuery)
// In template:
// {#if queryResult.isLoading}
// <div>Loading...</div>
// {:else if queryResult.isError}
// <div>Error loading data</div>
// {:else}
// {#each queryResult.data as item (item.id)}
// <Item {...item} />
// {/each}
// {/if}
// Handle states consistently
const queryResult = useLiveQuery(sharedQuery)
// In template:
// {#if queryResult.isLoading}
// <div>Loading...</div>
// {:else if queryResult.isError}
// <div>Error loading data</div>
// {:else}
// {#each queryResult.data as item (item.id)}
// <Item {...item} />
// {/each}
// {/if}
