Defined in: 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";
Defined in: packages/db/src/types.ts:388
Auto-indexing mode for the collection. When enabled, indexes will be automatically created for simple where expressions.
"eager"
"eager"
optional compare: (x, y) => number;
optional compare: (x, y) => number;
Defined in: packages/db/src/types.ts:399
Optional function to compare two items. This is used to order the items in the collection.
T
The first item to compare
T
The second item to compare
number
A number indicating the order of the items
// 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;
Defined in: packages/db/src/types.ts:374
Time in milliseconds after which the collection will be garbage collected when it has no active subscribers. Defaults to 5 minutes (300000ms).
getKey: (item) => TKey;
getKey: (item) => TKey;
Defined in: packages/db/src/types.ts:369
Function to extract the ID from an object This is required for update/delete operations which now only accept IDs
T
The item to extract the ID from
TKey
The ID string for the item
// 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;
Defined in: packages/db/src/types.ts:357
optional onDelete: DeleteMutationFn<T, TKey, Record<string, Fn>>;
optional onDelete: DeleteMutationFn<T, TKey, Record<string, Fn>>;
Defined in: packages/db/src/types.ts:528
Optional asynchronous handler function called before a delete operation
Object containing transaction and collection information
Promise resolving to any value
// 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>>;
Defined in: packages/db/src/types.ts:441
Optional asynchronous handler function called before an insert operation
Object containing transaction and collection information
Promise resolving to any value
// 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>>;
Defined in: packages/db/src/types.ts:485
Optional asynchronous handler function called before an update operation
Object containing transaction and collection information
Promise resolving to any value
// 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;
Defined in: packages/db/src/types.ts:359
optional startSync: boolean;
optional startSync: boolean;
Defined in: packages/db/src/types.ts:379
Whether to start syncing immediately when the collection is created. Defaults to false for lazy loading. Set to true to immediately sync.
sync: SyncConfig<T, TKey>;
sync: SyncConfig<T, TKey>;
Defined in: packages/db/src/types.ts:358
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.