CollectionConfig

Interface: CollectionConfig<T, TKey, TSchema, TInsertInput>

定义于: packages/db/src/types.ts:349

类型参数

T extends object = Record<string, unknown>

TKey extends string | number = string | number

TSchema extends StandardSchemaV1 = StandardSchemaV1

TInsertInput extends object = T

属性

autoIndex?

ts
optional autoIndex: "off" | "eager";
optional autoIndex: "off" | "eager";

定义于: packages/db/src/types.ts:388

集合的自动索引模式。启用时,将为简单的 where 表达式自动创建索引。

默认

ts
"eager"
"eager"

描述

  • "off": 无自动索引
  • "eager": 在 subscribeChanges 中自动为简单的 where 表达式创建索引(默认)

compare()?

ts
optional compare: (x, y) => number;
optional compare: (x, y) => number;

定义于: packages/db/src/types.ts:399

用于比较两个项目的可选函数。这用于对集合中的项目进行排序。

参数

x

T

要比较的第一个项目

y

T

要比较的第二个项目

Returns (返回)

number

一个指示项目顺序的数字

示例

ts
// For a collection with a 'createdAt' field
compare: (x, y) => x.createdAt.getTime() - y.createdAt.getTime()
// For a collection with a 'createdAt' field
compare: (x, y) => x.createdAt.getTime() - y.createdAt.getTime()

gcTime?

ts
optional gcTime: number;
optional gcTime: number;

定义于: packages/db/src/types.ts:374

集合在没有活动订阅者时被垃圾回收的时间(以毫秒为单位)。默认为 5 分钟(300000 毫秒)。


getKey()

ts
getKey: (item) => TKey;
getKey: (item) => TKey;

定义于: packages/db/src/types.ts:369

用于从对象中提取 ID 的函数。对于现在只接受 ID 的 update/delete 操作是必需的。

参数

item

T

要从中提取 ID 的项目

Returns (返回)

TKey

项目的 ID 字符串

示例

ts
// For a collection with a 'uuid' field as the primary key
getKey: (item) => item.uuid
// For a collection with a 'uuid' field as the primary key
getKey: (item) => item.uuid

id?

ts
optional id: string;
optional id: string;

定义于: packages/db/src/types.ts:357


onDelete?

ts
optional onDelete: DeleteMutationFn<T, TKey, Record<string, Fn>>;
optional onDelete: DeleteMutationFn<T, TKey, Record<string, Fn>>;

定义于: packages/db/src/types.ts:528

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

Param

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

Returns (返回)

解析为任何值的 Promise

示例

ts
// Basic delete handler
onDelete: async ({ transaction, collection }) => {
  const deletedKey = transaction.mutations[0].key
  await api.deleteTodo(deletedKey)
}
// Basic delete handler
onDelete: async ({ transaction, collection }) => {
  const deletedKey = transaction.mutations[0].key
  await api.deleteTodo(deletedKey)
}
ts
// Delete handler with multiple items
onDelete: async ({ transaction, collection }) => {
  const keysToDelete = transaction.mutations.map(m => m.key)
  await api.deleteTodos(keysToDelete)
}
// Delete handler with multiple items
onDelete: async ({ transaction, collection }) => {
  const keysToDelete = transaction.mutations.map(m => m.key)
  await api.deleteTodos(keysToDelete)
}
ts
// Delete handler with confirmation
onDelete: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  const shouldDelete = await confirmDeletion(mutation.original)
  if (!shouldDelete) {
    throw new Error('Delete cancelled by user')
  }
  await api.deleteTodo(mutation.original.id)
}
// Delete handler with confirmation
onDelete: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  const shouldDelete = await confirmDeletion(mutation.original)
  if (!shouldDelete) {
    throw new Error('Delete cancelled by user')
  }
  await api.deleteTodo(mutation.original.id)
}
ts
// Delete handler with optimistic rollback
onDelete: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  try {
    await api.deleteTodo(mutation.original.id)
  } catch (error) {
    // Transaction will automatically rollback optimistic changes
    console.error('Delete failed, rolling back:', error)
    throw error
  }
}
// Delete handler with optimistic rollback
onDelete: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  try {
    await api.deleteTodo(mutation.original.id)
  } catch (error) {
    // Transaction will automatically rollback optimistic changes
    console.error('Delete failed, rolling back:', error)
    throw error
  }
}

onInsert?

ts
optional onInsert: InsertMutationFn<TInsertInput, TKey, Record<string, Fn>>;
optional onInsert: InsertMutationFn<TInsertInput, TKey, Record<string, Fn>>;

定义于: packages/db/src/types.ts:441

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

Param

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

Returns (返回)

解析为任何值的 Promise

示例

ts
// Basic insert handler
onInsert: async ({ transaction, collection }) => {
  const newItem = transaction.mutations[0].modified
  await api.createTodo(newItem)
}
// Basic insert handler
onInsert: async ({ transaction, collection }) => {
  const newItem = transaction.mutations[0].modified
  await api.createTodo(newItem)
}
ts
// Insert handler with multiple items
onInsert: async ({ transaction, collection }) => {
  const items = transaction.mutations.map(m => m.modified)
  await api.createTodos(items)
}
// Insert handler with multiple items
onInsert: async ({ transaction, collection }) => {
  const items = transaction.mutations.map(m => m.modified)
  await api.createTodos(items)
}
ts
// Insert handler with error handling
onInsert: async ({ transaction, collection }) => {
  try {
    const newItem = transaction.mutations[0].modified
    const result = await api.createTodo(newItem)
    return result
  } catch (error) {
    console.error('Insert failed:', error)
    throw error // This will cause the transaction to fail
  }
}
// Insert handler with error handling
onInsert: async ({ transaction, collection }) => {
  try {
    const newItem = transaction.mutations[0].modified
    const result = await api.createTodo(newItem)
    return result
  } catch (error) {
    console.error('Insert failed:', error)
    throw error // This will cause the transaction to fail
  }
}
ts
// Insert handler with metadata
onInsert: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  await api.createTodo(mutation.modified, {
    source: mutation.metadata?.source,
    timestamp: mutation.createdAt
  })
}
// Insert handler with metadata
onInsert: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  await api.createTodo(mutation.modified, {
    source: mutation.metadata?.source,
    timestamp: mutation.createdAt
  })
}

onUpdate?

ts
optional onUpdate: UpdateMutationFn<T, TKey, Record<string, Fn>>;
optional onUpdate: UpdateMutationFn<T, TKey, Record<string, Fn>>;

定义于: packages/db/src/types.ts:485

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

Param

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

Returns (返回)

解析为任何值的 Promise

示例

ts
// Basic update handler
onUpdate: async ({ transaction, collection }) => {
  const updatedItem = transaction.mutations[0].modified
  await api.updateTodo(updatedItem.id, updatedItem)
}
// Basic update handler
onUpdate: async ({ transaction, collection }) => {
  const updatedItem = transaction.mutations[0].modified
  await api.updateTodo(updatedItem.id, updatedItem)
}
ts
// Update handler with partial updates
onUpdate: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  const changes = mutation.changes // Only the changed fields
  await api.updateTodo(mutation.original.id, changes)
}
// Update handler with partial updates
onUpdate: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  const changes = mutation.changes // Only the changed fields
  await api.updateTodo(mutation.original.id, changes)
}
ts
// Update handler with multiple items
onUpdate: async ({ transaction, collection }) => {
  const updates = transaction.mutations.map(m => ({
    id: m.key,
    changes: m.changes
  }))
  await api.updateTodos(updates)
}
// Update handler with multiple items
onUpdate: async ({ transaction, collection }) => {
  const updates = transaction.mutations.map(m => ({
    id: m.key,
    changes: m.changes
  }))
  await api.updateTodos(updates)
}
ts
// Update handler with optimistic rollback
onUpdate: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  try {
    await api.updateTodo(mutation.original.id, mutation.changes)
  } catch (error) {
    // Transaction will automatically rollback optimistic changes
    console.error('Update failed, rolling back:', error)
    throw error
  }
}
// Update handler with optimistic rollback
onUpdate: async ({ transaction, collection }) => {
  const mutation = transaction.mutations[0]
  try {
    await api.updateTodo(mutation.original.id, mutation.changes)
  } catch (error) {
    // Transaction will automatically rollback optimistic changes
    console.error('Update failed, rolling back:', error)
    throw error
  }
}

schema?

ts
optional schema: TSchema;
optional schema: TSchema;

定义于: packages/db/src/types.ts:359


startSync?

ts
optional startSync: boolean;
optional startSync: boolean;

定义于: packages/db/src/types.ts:379

集合创建时是否立即开始同步。默认为 false,用于延迟加载。设置为 true 以立即同步。


sync

ts
sync: SyncConfig<T, TKey>;
sync: SyncConfig<T, TKey>;

定义于: packages/db/src/types.ts:358

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

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

Bytes

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

订阅 Bytes

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

Bytes

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