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>>
Definiert in: packages/db/src/collection.ts:160
Erstellt eine neue Collection-Instanz mit der gegebenen Konfiguration
• TExplicit = unknown
Der explizite Typ der Elemente in der Collection (höchste Priorität)
• TKey erweitert string | number = string | number
Der Typ des Schlüssels für die Collection
• TUtils erweitert UtilsRecord = {}
Der Utility-Record-Typ
• TSchema erweitert StandardSchemaV1<unknown, unknown> = StandardSchemaV1<unknown, unknown>
Der Schema-Typ für Validierung und Typableitung (zweite Priorität)
• TFallback erweitert object = Record<string, unknown>
Der Fallback-Typ, wenn kein expliziter Typ oder kein Schema-Typ angegeben wird
CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>, TKey, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>> & object
Collection-Optionen mit optionalen Utilities
Collection<ResolveType<TExplicit, TSchema, TFallback>, TKey, TUtils, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>>
Eine neue Collection mit Utilities, die sowohl auf oberster Ebene als auch unter .utils verfügbar sind
// 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.
Ihre wöchentliche Dosis JavaScript-Nachrichten. Jeden Montag kostenlos an über 100.000 Entwickler geliefert.