function createCollection<TExplicit, TKey, TUtils, TSchema, TFallback>(options): Collection<ResolveType<TExplicit, TSchema, TFallback>, TKey, TUtils, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>>
function createCollection<TExplicit, TKey, TUtils, TSchema, TFallback>(options): Collection<ResolveType<TExplicit, TSchema, TFallback>, TKey, TUtils, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>>
定义于: packages/db/src/collection.ts:160
使用给定的配置创建一个新的 Collection 实例
• TExplicit = 未知
集合中项目的显式类型(最高优先级)
• TKey extends string | number = string | number
Collection 的键的类型
• TUtils extends UtilsRecord = {}
工具记录的类型
• TSchema extends StandardSchemaV1<未知, 未知> = StandardSchemaV1<未知, 未知>
用于验证和类型推断的模式类型(第二优先级)
• TFallback extends object = Record<string, 未知>
如果没有提供显式类型或模式类型,则回退类型
CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>, TKey, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>> & object
带有可选工具的 Collection 选项
Collection<ResolveType<TExplicit, TSchema, TFallback>, TKey, TUtils, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>>
一个包含工具(在顶层和 .utils 下都可以访问)的新 Collection
// Pattern 1: With operation handlers (direct collection calls)
const todos = createCollection({
id: "todos",
getKey: (todo) => todo.id,
schema,
onInsert: async ({ transaction, collection }) => {
// Send to API
await api.createTodo(transaction.mutations[0].modified)
},
onUpdate: async ({ transaction, collection }) => {
await api.updateTodo(transaction.mutations[0].modified)
},
onDelete: async ({ transaction, collection }) => {
await api.deleteTodo(transaction.mutations[0].key)
},
sync: { sync: () => {} }
})
// Direct usage (handlers manage transactions)
const tx = todos.insert({ id: "1", text: "Buy milk", completed: false })
await tx.isPersisted.promise
// Pattern 1: With operation handlers (direct collection calls)
const todos = createCollection({
id: "todos",
getKey: (todo) => todo.id,
schema,
onInsert: async ({ transaction, collection }) => {
// Send to API
await api.createTodo(transaction.mutations[0].modified)
},
onUpdate: async ({ transaction, collection }) => {
await api.updateTodo(transaction.mutations[0].modified)
},
onDelete: async ({ transaction, collection }) => {
await api.deleteTodo(transaction.mutations[0].key)
},
sync: { sync: () => {} }
})
// Direct usage (handlers manage transactions)
const tx = todos.insert({ id: "1", text: "Buy milk", completed: false })
await tx.isPersisted.promise
// Pattern 2: Manual transaction management
const todos = createCollection({
getKey: (todo) => todo.id,
schema: todoSchema,
sync: { sync: () => {} }
})
// Explicit transaction usage
const tx = createTransaction({
mutationFn: async ({ transaction }) => {
// Handle all mutations in transaction
await api.saveChanges(transaction.mutations)
}
})
tx.mutate(() => {
todos.insert({ id: "1", text: "Buy milk" })
todos.update("2", draft => { draft.completed = true })
})
await tx.isPersisted.promise
// Pattern 2: Manual transaction management
const todos = createCollection({
getKey: (todo) => todo.id,
schema: todoSchema,
sync: { sync: () => {} }
})
// Explicit transaction usage
const tx = createTransaction({
mutationFn: async ({ transaction }) => {
// Handle all mutations in transaction
await api.saveChanges(transaction.mutations)
}
})
tx.mutate(() => {
todos.insert({ id: "1", text: "Buy milk" })
todos.update("2", draft => { draft.completed = true })
})
await tx.isPersisted.promise
// Using schema for type inference (preferred as it also gives you client side validation)
const todoSchema = z.object({
id: z.string(),
title: z.string(),
completed: z.boolean()
})
const todos = createCollection({
schema: todoSchema,
getKey: (todo) => todo.id,
sync: { sync: () => {} }
})
// Note: You must provide either an explicit type or a schema, but not both.
// Using schema for type inference (preferred as it also gives you client side validation)
const todoSchema = z.object({
id: z.string(),
title: z.string(),
completed: z.boolean()
})
const todos = createCollection({
schema: todoSchema,
getKey: (todo) => todo.id,
sync: { sync: () => {} }
})
// Note: You must provide either an explicit type or a schema, but not both.
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。