Interface: QueryCollectionConfig<TItem, TError, TQueryKey>#
Defined in: packages/query-db-collection/src/query.ts:32
Type Parameters#
• TItem extends object
• TError = unknown
• TQueryKey extends QueryKey = QueryKey
Properties#
enabled?#
optional enabled: boolean;
Defined in: packages/query-db-collection/src/query.ts:42
getKey()#
getKey: (item) => string | number;
Defined in: packages/query-db-collection/src/query.ts:74
Parameters#
item#
TItem
Returns#
string | number
id?#
optional id: string;
Defined in: packages/query-db-collection/src/query.ts:73
meta?#
optional meta: Record<string, unknown>;
Defined in: packages/query-db-collection/src/query.ts:242
Metadata to pass to the query. Available in queryFn via context.meta
Example#
// Using meta for error context
queryFn: async (context) => {
try {
return await api.getTodos(userId)
} catch (error) {
// Use meta for better error messages
throw new Error(
context.meta?.errorMessage || 'Failed to load todos'
)
}
},
meta: {
errorMessage: `Failed to load todos for user ${userId}`
}
onDelete?#
optional onDelete: DeleteMutationFn<TItem>;
Defined in: packages/query-db-collection/src/query.ts:219
Optional asynchronous handler function called before a delete operation
Param#
Object containing transaction and collection information
Returns#
Promise resolving to void or { refetch?: boolean } to control refetching
Examples#
// Basic query collection delete handler
onDelete: async ({ transaction }) => {
const mutation = transaction.mutations[0]
await api.deleteTodo(mutation.original.id)
// Automatically refetches query after delete
}
// Delete handler with refetch control
onDelete: async ({ transaction }) => {
const mutation = transaction.mutations[0]
await api.deleteTodo(mutation.original.id)
return { refetch: false } // Skip automatic refetch
}
// Delete handler with multiple items
onDelete: async ({ transaction }) => {
const keysToDelete = transaction.mutations.map(m => m.key)
await api.deleteTodos(keysToDelete)
// Will refetch query to get updated data
}
// Delete handler with related collection refetch
onDelete: async ({ transaction, collection }) => {
const mutation = transaction.mutations[0]
await api.deleteTodo(mutation.original.id)
// Refetch related collections when this item is deleted
await Promise.all([
collection.utils.refetch(), // Refetch this collection
usersCollection.utils.refetch(), // Refetch users
projectsCollection.utils.refetch() // Refetch projects
])
return { refetch: false } // Skip automatic refetch since we handled it manually
}
onInsert?#
optional onInsert: InsertMutationFn<TItem>;
Defined in: packages/query-db-collection/src/query.ts:120
Optional asynchronous handler function called before an insert operation
Param#
Object containing transaction and collection information
Returns#
Promise resolving to void or { refetch?: boolean } to control refetching
Examples#
// Basic query collection insert handler
onInsert: async ({ transaction }) => {
const newItem = transaction.mutations[0].modified
await api.createTodo(newItem)
// Automatically refetches query after insert
}
// Insert handler with refetch control
onInsert: async ({ transaction }) => {
const newItem = transaction.mutations[0].modified
await api.createTodo(newItem)
return { refetch: false } // Skip automatic refetch
}
// Insert handler with multiple items
onInsert: async ({ transaction }) => {
const items = transaction.mutations.map(m => m.modified)
await api.createTodos(items)
// Will refetch query to get updated data
}
// Insert handler with error handling
onInsert: async ({ transaction }) => {
try {
const newItem = transaction.mutations[0].modified
await api.createTodo(newItem)
} catch (error) {
console.error('Insert failed:', error)
throw error // Transaction will rollback optimistic changes
}
}
onUpdate?#
optional onUpdate: UpdateMutationFn<TItem>;
Defined in: packages/query-db-collection/src/query.ts:173
Optional asynchronous handler function called before an update operation
Param#
Object containing transaction and collection information
Returns#
Promise resolving to void or { refetch?: boolean } to control refetching
Examples#
// Basic query collection update handler
onUpdate: async ({ transaction }) => {
const mutation = transaction.mutations[0]
await api.updateTodo(mutation.original.id, mutation.changes)
// Automatically refetches query after update
}
// Update handler with multiple items
onUpdate: async ({ transaction }) => {
const updates = transaction.mutations.map(m => ({
id: m.key,
changes: m.changes
}))
await api.updateTodos(updates)
// Will refetch query to get updated data
}
// Update handler with manual refetch
onUpdate: async ({ transaction, collection }) => {
const mutation = transaction.mutations[0]
await api.updateTodo(mutation.original.id, mutation.changes)
// Manually trigger refetch
await collection.utils.refetch()
return { refetch: false } // Skip automatic refetch
}
// Update handler with related collection refetch
onUpdate: async ({ transaction, collection }) => {
const mutation = transaction.mutations[0]
await api.updateTodo(mutation.original.id, mutation.changes)
// Refetch related collections when this item changes
await Promise.all([
collection.utils.refetch(), // Refetch this collection
usersCollection.utils.refetch(), // Refetch users
tagsCollection.utils.refetch() // Refetch tags
])
return { refetch: false } // Skip automatic refetch since we handled it manually
}
queryClient#
queryClient: QueryClient;
Defined in: packages/query-db-collection/src/query.ts:39
queryFn()#
queryFn: (context) => Promise<TItem[]>;
Defined in: packages/query-db-collection/src/query.ts:38
Parameters#
context#
client#
QueryClient
direction?#
unknown
Deprecated
if you want access to the direction, you can add it to the pageParam
meta#
undefined | Record<string, unknown>
pageParam?#
unknown
queryKey#
TQueryKey
signal#
AbortSignal
Returns#
Promise<TItem[]>
queryKey#
queryKey: TQueryKey;
Defined in: packages/query-db-collection/src/query.ts:37
refetchInterval?#
optional refetchInterval: number | false | (query) => undefined | number | false;
Defined in: packages/query-db-collection/src/query.ts:43
retry?#
optional retry: RetryValue<TError>;
Defined in: packages/query-db-collection/src/query.ts:50
retryDelay?#
optional retryDelay: RetryDelayValue<TError>;
Defined in: packages/query-db-collection/src/query.ts:57
schema?#
optional schema: StandardSchemaV1<unknown, unknown>;
Defined in: packages/query-db-collection/src/query.ts:75
staleTime?#
optional staleTime: StaleTimeFunction<TItem[], TError, TItem[], TQueryKey>;
Defined in: packages/query-db-collection/src/query.ts:64
startSync?#
optional startSync: boolean;
Defined in: packages/query-db-collection/src/query.ts:77
sync?#
optional sync: SyncConfig<TItem, string | number>;
Defined in: packages/query-db-collection/src/query.ts:76