elements
Start Docs Blog Demos UI Pricing
Elements Manual
  • Getting Started
  • CLI
  • Build
  • Browser
  • Packages
  • TypeScript
  • Elements HTML
  • Assets
  • RPC
  • Router
  • Session
  • Database
  • Migrations
  • LiveTable
    • Options
    • Partitions
    • Windows
    • Mutations
    • Handlers
  • Channel
  • Jobs
  • Email
  • Payments
  • JSON
  • Async
  • Style
  • UI
  • JSOC
  • Config
  • Tests
  • Logs
  • Deploy
  • Editors
  • Recipes
  • Licenses
  • Issues
  • Changelog
Manual / LiveTable/ Mutations

Mutations

elements man livetable/mutations

On This Page

  • Including Every Displayed Field
  • The same write call on the server and in the browser
  • What a write returns

Inserting, updating, and deleting through a LiveView, including optimistic updates.

Mutators are sync-style and optimistic. The browser updates immediately, the server then writes the row and broadcasts it, and if the write fails the optimistic UI update is reverted.

insert, update, and delete are methods of the LiveView, the object a route opened with table.view() and passed to the template. They work the same on the server, so a write can run from a route, an @rpc, a job, or a test.

Usage
comments.insert({ text: "hi", author: "alice", }, () => form.text = ""); comments.update({ ...comment, text: "edited", }, () => setEditing(false)); comments.delete(comment);

insert, update, and delete accept a resetUI callback as a second argument. The callback runs as soon as the optimistic change is applied in the browser, before the server has acknowledged. Use it to clear the state that drove the mutation: form inputs, the row that was being edited, any draft data tied to that action. It is for resetting UI state only, not for navigation or other side effects.

Do not navigate or redirect from resetUI. The callback runs before the server has received the write. Navigating in the callback unloads the page and aborts the write, so the row is never stored in the database. Put navigation on the line after the mutation instead. The mutators are sync-style, so the compiler awaits the write before the next line runs:

Example
posts.insert({ title, url }); redirect("/");

When a form creates one record and then navigates, and no list on screen shows the record, use a plain @rpc function that inserts and returns the new row rather than LiveView.insert. The optimistic insert adds a row to a list the user is looking at, and such a page has no list. The RPC function can call redirect() itself: the browser navigates only after the call returns, so the write is committed before the page unloads.

insertAsync, updateAsync, and deleteAsync each return a promise that you await yourself, for example with Promise.all over several writes. A sync-style mutation persists once sent; navigate on the line after it, as above.

Including Every Displayed Field

The optimistic row renders before the server reconciles. It holds only the object you pass to insert(...), nothing else. If the template displays comment.author, an optimistic insert({ text }) renders undefined for author until the server's reply arrives. Pass every field the template reads at insert time, even if the server replaces their values.

Optimistic insert
// browser shows undefined author until reconcile comments.insert({ text: form.text }); // browser shows the author immediately comments.insert({ text: form.text, author: session.get("userName")!, });

This includes server-generated columns the template reads: timestamps like createdAt (default now()), sequence numbers, and anything a database trigger fills in. The database sets those values when it stores the row, so pass a client-side placeholder for each one the template displays. A displayed createdAt then renders a time from the first frame, and the server's real value replaces it a moment later.

Placeholder value
comments.insert({ text: form.text, author: session.get("userName")!, createdAt: new Date(), });

You can leave out the partition columns. A view opened with view({ postId }) sets postId on the row.

The same write call on the server and in the browser

insert, update, and delete are methods of the LiveView, and they run in the browser and on the server. The same call in a template, a route, an @rpc, a job, or a test writes the same row:

Example
// in a template, on the browser. `comments` is the view the route passed in comments.insert({ text: draft, author: session.getOrThrow("userName"), }); // in a test, on the server. same call, on a view the test opened test("a member can comment", () => { session.login({ userId: user.id, userName: "alice", }); let comments = commentsTable.view(); comments.insert({ text: "hi", author: "alice", }); equal(sql(`select count(*) from comments where author = 'alice'`).firstOrThrow().count, 1); });

A write factored into services.ts takes the view as a parameter, so the template passes the view the route opened and a test passes a view it opened:

services.ts
export function postComment(view: LiveView<Comment>, text: string) { view.insert({ text, author: session.getOrThrow("userName"), }); }

The browser and the server differ in one step. The browser adds the row to its list first and then sends the write to the server, so the UI updates before the server replies. The server has no list on screen to update, so it runs the write directly: partition check, your handler or auto-SQL, then the broadcast. The browser and the server each generate the row id and fill in the partition columns before the write, so a handler receives a row with the same fields from either caller.

resetUI is accepted on the server for signature parity and never called; there is no optimistic update to roll back.

A server-side write runs your handler against the current session, which is anonymous unless the caller logged one in, so a test asserting that an anonymous write is refused is a test you can write. It also broadcasts, so browsers watching the table on every app server see the row arrive exactly as they would for a browser-initiated write.

What a write returns

insert returns the row that went in, id included, so a handler that needs the new id (to focus it, to navigate to it) reads it off the return value. update patches the fields onto the row already held and returns that row, so it keeps its identity and its dom. delete returns the item you passed, so a handler can keep it to offer an undo.

A write through a partitioned view that names a different partition, or a row the view does not hold, is refused before anything is sent.

Prev WindowsNext Handlers
Product
Manual Pricing Compare
Company
History Contact
Legal
Privacy Policy Acceptable Use Terms License Agreement
© Elements Dev Corporation 2026