QueryCollectionConfig

Interface: QueryCollectionConfig<TItem, TError, TQueryKey>

定义于: packages/query-db-collection/src/query.ts:32

类型参数

TItem extends object

TError = unknown

TQueryKey extends QueryKey = QueryKey

属性

enabled?

ts
optional enabled: boolean;
optional enabled: boolean;

定义于: packages/query-db-collection/src/query.ts:42


getKey()

ts
getKey: (item) => string | number;
getKey: (item) => string | number;

定义于: packages/query-db-collection/src/query.ts:74

参数

item

TItem

Returns (返回)

string | number


id?

ts
optional id: string;
optional id: string;

定义于: packages/query-db-collection/src/query.ts:73


meta?

ts
optional meta: Record<string, unknown>;
optional meta: Record<string, unknown>;

定义于: packages/query-db-collection/src/query.ts:242

传递给查询的元数据。在 queryFn 中可通过 context.meta 访问。

示例

ts
// 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}`
}
// 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?

ts
optional onDelete: DeleteMutationFn<TItem>;
optional onDelete: DeleteMutationFn<TItem>;

定义于: packages/query-db-collection/src/query.ts:219

删除操作之前调用的可选异步处理函数

Param

包含事务和集合信息的对象

Returns (返回)

解析为 void 或 { refetch?: boolean } 的 Promise,用于控制重新获取。

示例

ts
// Basic query collection delete handler
onDelete: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  await api.deleteTodo(mutation.original.id)
  // Automatically refetches query after delete
}
// Basic query collection delete handler
onDelete: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  await api.deleteTodo(mutation.original.id)
  // Automatically refetches query after delete
}
ts
// 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 refetch control
onDelete: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  await api.deleteTodo(mutation.original.id)
  return { refetch: false } // Skip automatic refetch
}
ts
// 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 multiple items
onDelete: async ({ transaction }) => {
  const keysToDelete = transaction.mutations.map(m => m.key)
  await api.deleteTodos(keysToDelete)
  // Will refetch query to get updated data
}
ts
// 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
}
// 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?

ts
optional onInsert: InsertMutationFn<TItem>;
optional onInsert: InsertMutationFn<TItem>;

定义于: packages/query-db-collection/src/query.ts:120

插入操作之前调用的可选异步处理函数

Param

包含事务和集合信息的对象

Returns (返回)

解析为 void 或 { refetch?: boolean } 的 Promise,用于控制重新获取。

示例

ts
// Basic query collection insert handler
onInsert: async ({ transaction }) => {
  const newItem = transaction.mutations[0].modified
  await api.createTodo(newItem)
  // Automatically refetches query after insert
}
// Basic query collection insert handler
onInsert: async ({ transaction }) => {
  const newItem = transaction.mutations[0].modified
  await api.createTodo(newItem)
  // Automatically refetches query after insert
}
ts
// 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 refetch control
onInsert: async ({ transaction }) => {
  const newItem = transaction.mutations[0].modified
  await api.createTodo(newItem)
  return { refetch: false } // Skip automatic refetch
}
ts
// 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 multiple items
onInsert: async ({ transaction }) => {
  const items = transaction.mutations.map(m => m.modified)
  await api.createTodos(items)
  // Will refetch query to get updated data
}
ts
// 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
  }
}
// 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?

ts
optional onUpdate: UpdateMutationFn<TItem>;
optional onUpdate: UpdateMutationFn<TItem>;

定义于: packages/query-db-collection/src/query.ts:173

更新操作之前调用的可选异步处理函数

Param

包含事务和集合信息的对象

Returns (返回)

解析为 void 或 { refetch?: boolean } 的 Promise,用于控制重新获取。

示例

ts
// 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
}
// 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
}
ts
// 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 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
}
ts
// 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 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
}
ts
// 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
}
// 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

ts
queryClient: QueryClient;
queryClient: QueryClient;

定义于: packages/query-db-collection/src/query.ts:39


queryFn()

ts
queryFn: (context) => Promise<TItem[]>;
queryFn: (context) => Promise<TItem[]>;

定义于: packages/query-db-collection/src/query.ts:38

参数

context
client

QueryClient

direction?

unknown

已弃用

如果你想访问 direction,可以将其添加到 pageParam 中。

meta

undefined | Record<string, unknown>

pageParam?

unknown

queryKey

TQueryKey

signal

AbortSignal

Returns (返回)

Promise<TItem[]>


queryKey

ts
queryKey: TQueryKey;
queryKey: TQueryKey;

定义于: packages/query-db-collection/src/query.ts:37


refetchInterval?

ts
optional refetchInterval: number | false | (query) => undefined | number | false;
optional refetchInterval: number | false | (query) => undefined | number | false;

定义于: packages/query-db-collection/src/query.ts:43


retry?

ts
optional retry: RetryValue<TError>;
optional retry: RetryValue<TError>;

定义于: packages/query-db-collection/src/query.ts:50


retryDelay?

ts
optional retryDelay: RetryDelayValue<TError>;
optional retryDelay: RetryDelayValue<TError>;

定义于: packages/query-db-collection/src/query.ts:57


schema?

ts
optional schema: StandardSchemaV1<unknown, unknown>;
optional schema: StandardSchemaV1<unknown, unknown>;

定义于: packages/query-db-collection/src/query.ts:75


staleTime?

ts
optional staleTime: StaleTimeFunction<TItem[], TError, TItem[], TQueryKey>;
optional staleTime: StaleTimeFunction<TItem[], TError, TItem[], TQueryKey>;

定义于: packages/query-db-collection/src/query.ts:64


startSync?

ts
optional startSync: boolean;
optional startSync: boolean;

定义于: packages/query-db-collection/src/query.ts:77


sync?

ts
optional sync: SyncConfig<TItem, string | number>;
optional sync: SyncConfig<TItem, string | number>;

定义于: packages/query-db-collection/src/query.ts:76

我们的合作伙伴
Code Rabbit
Electric
Prisma
订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。

订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。