定义于: 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
optional autoIndex: "off" | "eager";
optional autoIndex: "off" | "eager";
定义于: packages/db/src/types.ts:388
集合的自动索引模式。启用时,将为简单的 where 表达式自动创建索引。
"eager"
"eager"
optional compare: (x, y) => number;
optional compare: (x, y) => number;
定义于: packages/db/src/types.ts:399
用于比较两个项目的可选函数。这用于对集合中的项目进行排序。
T
要比较的第一个项目
T
要比较的第二个项目
number
一个指示项目顺序的数字
// 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()
optional gcTime: number;
optional gcTime: number;
定义于: packages/db/src/types.ts:374
集合在没有活动订阅者时被垃圾回收的时间(以毫秒为单位)。默认为 5 分钟(300000 毫秒)。
getKey: (item) => TKey;
getKey: (item) => TKey;
定义于: packages/db/src/types.ts:369
用于从对象中提取 ID 的函数。对于现在只接受 ID 的 update/delete 操作是必需的。
T
要从中提取 ID 的项目
TKey
项目的 ID 字符串
// 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
optional id: string;
optional id: string;
定义于: packages/db/src/types.ts:357
optional onDelete: DeleteMutationFn<T, TKey, Record<string, Fn>>;
optional onDelete: DeleteMutationFn<T, TKey, Record<string, Fn>>;
定义于: packages/db/src/types.ts:528
删除操作之前调用的可选异步处理函数
包含事务和集合信息的对象
解析为任何值的 Promise
// 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)
}
// 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)
}
// 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)
}
// 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
}
}
optional onInsert: InsertMutationFn<TInsertInput, TKey, Record<string, Fn>>;
optional onInsert: InsertMutationFn<TInsertInput, TKey, Record<string, Fn>>;
定义于: packages/db/src/types.ts:441
插入操作之前调用的可选异步处理函数
包含事务和集合信息的对象
解析为任何值的 Promise
// 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)
}
// 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)
}
// 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
}
}
// 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
})
}
optional onUpdate: UpdateMutationFn<T, TKey, Record<string, Fn>>;
optional onUpdate: UpdateMutationFn<T, TKey, Record<string, Fn>>;
定义于: packages/db/src/types.ts:485
更新操作之前调用的可选异步处理函数
包含事务和集合信息的对象
解析为任何值的 Promise
// 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)
}
// 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)
}
// 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)
}
// 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
}
}
optional schema: TSchema;
optional schema: TSchema;
定义于: packages/db/src/types.ts:359
optional startSync: boolean;
optional startSync: boolean;
定义于: packages/db/src/types.ts:379
集合创建时是否立即开始同步。默认为 false,用于延迟加载。设置为 true 以立即同步。
sync: SyncConfig<T, TKey>;
sync: SyncConfig<T, TKey>;
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。