Collection
Read and write records
A Collection<T> maps to a single table or object store. All operations are async.
Create
const record = await collection.create(data: Partial<T>): Promise<Model<T> & T>
- Applies schema defaults
- Validates against schema
- Runs
onBeforeCreateplugin hooks (can modify data) - Creates record in adapter
- Runs
onAfterCreateplugin hooks - Emits a change event
const task = await todos.create({ title: "Write docs" })
console.log(task.id, task.title)
Read
const record = await collection.get(id: ID)
const all = await collection.getAll()
get returns a single record or undefined. getAll returns every record.
const task = await todos.get(1)
if (task) console.log(task.title)
Update
const updated = await collection.update(id: ID, changes: Partial<T>)
- Merges changes with existing data
- Validates the merged result
- Runs
onBeforeUpdate/onAfterUpdateplugin hooks - Emits a change event
await todos.update(1, { done: true })
Delete
await collection.delete(id: ID)
await collection.deleteMany(ids: ID[])
- Runs
onBeforeDelete/onAfterDeleteplugin hooks - Emits a change event for each deleted record
deletereturns normally even if the record does not existdeleteManysilently skips missing IDs
Upsert
const record = await collection.put(data: T & { id?: ID })
If data.id matches an existing record, updates it. Otherwise creates it.
Count
const count = await collection.count()
Query builder
const results = await collection.query()
.where("status", "==", "active")
.sort({ createdAt: "desc" })
.limit(10)
.fetch()
See Query Engine for details.
Change subscriptions
const unsub = collection.onChange((event) => {
console.log(event.type, event.recordId)
})
Fires on every create, update, or delete in this collection. Returns an unsubscribe function.
Typing
interface Task {
title: string
done: boolean
}
const tasks = db.collection<Task>("todos")
const item = await tasks.create({ title: "Typed" })
// item.title is typed as stringHow is this guide?
Last updated on Jun 20, 2026