CollectionConfig

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

Definiert in: packages/db/src/types.ts:349

Typparameter

T extends object = Record<string, unknown>

TKey erweitert string | number = string | number

TSchema extends StandardSchemaV1 = StandardSchemaV1

TInsertInput extends object = T

Eigenschaften

autoIndex?

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

Definiert in: packages/db/src/types.ts:388

Auto-Indexing-Modus für die Collection. Wenn aktiviert, werden Indizes automatisch für einfache Where-Ausdrücke erstellt.

Standard

ts
"eager"
"eager"

Beschreibung

  • "off": Kein automatisches Indexing
  • "eager": Erstellt automatisch Indizes für einfache Where-Ausdrücke in subscribeChanges (Standard)

compare()?

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

Definiert in: packages/db/src/types.ts:399

Optionale Funktion zum Vergleichen zweier Elemente. Diese wird verwendet, um die Elemente in der Collection zu sortieren.

Parameter

x

T

Das erste zu vergleichende Element

y

T

Das zweite zu vergleichende Element

Gibt zurück

number

Eine Zahl, die die Reihenfolge der Elemente angibt

Beispiel

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;

Definiert in: packages/db/src/types.ts:374

Zeit in Millisekunden, nach der die Collection gesammelt wird, wenn sie keine aktiven Abonnenten hat. Standardmäßig 5 Minuten (300000ms).


getKey()

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

Definiert in: packages/db/src/types.ts:369

Funktion zum Extrahieren der ID aus einem Objekt. Dies ist erforderlich für Update/Delete-Operationen, die jetzt nur noch IDs akzeptieren.

Parameter

item

T

Das Element, aus dem die ID extrahiert werden soll

Gibt zurück

TKey

Die ID-Zeichenkette für das Element

Beispiel

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;

Definiert in: packages/db/src/types.ts:357


onDelete?

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

Definiert in: packages/db/src/types.ts:528

Optionale asynchrone Handler-Funktion, die vor einer Delete-Operation aufgerufen wird.

Param

Objekt, das Transaktions- und Collection-Informationen enthält

Gibt zurück

Promise, das zu einem beliebigen Wert aufgelöst wird

Beispiele

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>>;

Definiert in: packages/db/src/types.ts:441

Optionale asynchrone Handler-Funktion, die vor einer Insert-Operation aufgerufen wird.

Param

Objekt, das Transaktions- und Collection-Informationen enthält

Gibt zurück

Promise, das zu einem beliebigen Wert aufgelöst wird

Beispiele

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>>;

Definiert in: packages/db/src/types.ts:485

Optionale asynchrone Handler-Funktion, die vor einer Update-Operation aufgerufen wird.

Param

Objekt, das Transaktions- und Collection-Informationen enthält

Gibt zurück

Promise, das zu einem beliebigen Wert aufgelöst wird

Beispiele

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;

Definiert in: packages/db/src/types.ts:359


startSync?

ts
optional startSync: boolean;
optional startSync: boolean;

Definiert in: packages/db/src/types.ts:379

Gibt an, ob sofort mit der Synchronisierung begonnen werden soll, wenn die Collection erstellt wird. Standardmäßig false für Lazy Loading. Auf true setzen, um sofort zu synchronisieren.


sync

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

Definiert in: packages/db/src/types.ts:358

Unsere Partner
Code Rabbit
Electric
Prisma
Bytes abonnieren

Ihre wöchentliche Dosis JavaScript-Nachrichten. Jeden Montag kostenlos an über 100.000 Entwickler geliefert.

Bytes

Kein Spam. Jederzeit kündbar.

Bytes abonnieren

Ihre wöchentliche Dosis JavaScript-Nachrichten. Jeden Montag kostenlos an über 100.000 Entwickler geliefert.

Bytes

Kein Spam. Jederzeit kündbar.