Writes execute as Matter commands under the current member’s identity. They update every local read and live watch on this computer immediately (optimistically), and resolve once the server commits them. To write tasks, see Boards and tasks.
const deals = matter.table('deals')
// Insert one or many rows
const id = await deals.insert({ title: 'Acme', values: { stage: 'lead', owner: '$viewer' } })
// Conditional update (only succeeds if stage is still 'lead')
await deals.update(id, { stage: 'won' }, { if: { stage: 'lead' } })
// Upsert against a unique index
await deals.upsert('by_email', [{ title: 'Acme', values: { email: 'sam@acme.com', stage: 'lead' } }])
// Delete by ID or array of IDs
await deals.delete(id)Write methods#
| Method | Behavior |
|---|---|
insert(row | rows, opts?) |
Creates up to 500 rows at once. Resolves to the new row ID (or array of IDs). |
update(id, values, { title?, if?, createOptions? }) |
Updates the specified fields (and optional title) on a row. Pass null for a
field to clear its value.
|
updateMany(items, opts?) |
Updates up to 500 rows at once: each item is { id, title?, values?, if? }. |
upsert(on, rows, opts?) |
Matches up to 500 rows against a unique index name (or a unique field key): updates existing matches and inserts the rest. Resolves to their IDs. |
delete(id | ids, { if? }) |
Deletes one or up to 1,000 rows. With if, only if every row matches. |
matter.batch(ops, opts?) |
Runs several table operations in order, all or none. |
Claiming work safely with if#
Because a background worker runs on every computer where the member
opened the app (and a dev worker can run alongside a live worker), two processes may see a row enter an
actionable state at the same time. Use an atomic if precondition to claim the row before
performing external side effects:
try {
await actions.update(id, { status: 'sending' }, { if: { status: 'approved' } })
} catch (error) {
if (error.code === 'precondition_failed') return // another worker already claimed it
throw error
}
// Safe to send the message now:
await deliverMessage(id)
await actions.update(id, { status: 'sent' })
if can check any field, the name field included, and delete takes it too. To make
several writes depend on one check, put them in a matter.batch with it: if the check fails,
none of them happens. Checking the name field, delete’s if, and all-or-nothing
batches need Matter 0.5.11.
Creating select options on the fly#
By default, writing an unknown option key to a select or multi_select field fails
with invalid so typos are caught early. To intentionally create new options during a write to a
table no app owns, pass { createOptions: true }. Your app’s own tables have exactly the options
its manifest declares, so add options there and deploy.
await deals.update(id, { stage: 'on_hold' }, { createOptions: true })Batches and offline behavior#
matter.batch(ops) runs writes to one or more tables in order, in one transaction: if any of
them fails, none of them happens. Each op names its table and op and takes what
that write takes: rows for insert and update, on and
rows for upsert, ids for delete, and if on
an update’s rows or a delete. A batch writes up to 1,000 rows in all.
await matter.batch([
{ table: 'deals', op: 'update', rows: [{ id, values: { stage: 'won' }, if: { stage: 'lead' } }] },
{ table: 'activity', op: 'insert', rows: [{ title: 'Won Acme', values: { deal: id } }] },
])
// -> one result per op, in order
Offline resilience: If the computer is offline when you call a write, the optimistic change
is immediately visible in all local reads on that computer. After 20 seconds without a server connection,
insert and upsert resolve with the new row IDs and update /
delete resolve with { pending: true }, syncing to the server automatically when
the connection returns.