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/ Handlers

Handlers

elements man livetable/handlers

On This Page

  • Mutators
  • Custom SQL
  • Select
  • Composite tables

Replacing a LiveTable's read or write with your own SQL, and where to put the authorization check.

Mutators

Each of insert, update, and delete is one function. It receives the row and returns the row it wrote. session is available in the handler, so the authorization check is the first line of the handler, the same as in an @rpc.

Mutators
import { LiveTable, session, sql } from "@elements/app"; export let comments: LiveTable<Comment> = new LiveTable<Comment>({ insert: (item) => { session.isLoggedInOrThrow(); return comments.insert(item); }, update: (item) => { if (item.author !== session.get("userName")) { throw new ForbiddenError(); } return comments.update(item); }, delete: (item) => { session.isLoggedInOrThrow(); comments.delete(item); }, });

comments.insert(item) on the declaration is the raw auto-SQL: one insert ... returning against the table, no handler, no broadcast. It runs when you declare no handler, and a handler calls it to run the default write after an authorization check. comments.update and comments.delete are the same for the other two. Because the handler names the variable it is assigned to, the declaration needs its type written out, let comments: LiveTable<Comment>.

When a handler is omitted, the mutator runs auto-SQL with no authorization check. To refuse a write for every caller, declare a handler that throws:

Refused delete
delete: () => { throw new ForbiddenError(); },

The browser receives a thrown error as the same class: AuthError, ForbiddenError, and ValidationError are safe and carry their message, and anything else is wrapped in ServerError. See rpc for catching them.

Custom SQL

Auto-SQL writes one row to one table. Write the SQL yourself when the write derives a column from session, touches several rows, or targets a join.

Custom insert
export let comments: LiveTable<Comment> = new LiveTable<Comment>({ insert: (item) => { session.isLoggedInOrThrow(); return sql<Comment>(` insert into comments ( id, text, author, postId, createdAt ) values ( ${item.id}, ${item.text}, ${session.getOrThrow("userName")}, ${item.postId}, now() ) returning * `).firstOrThrow(); }, });

item is Partial<Comment> for insert and Comment for update and delete; you do not annotate it.

A custom insert has two requirements. It must preserve item.id: the browser generated the id and the optimistic row has it, and if the stored row comes back with a different id the broadcast row matches no row in the browser's list, so the list shows the row twice. And it must return the row: the server broadcasts the return value, which must include the partition columns and, on a windowed view, the orderBy columns, so the browser can place the row. returning * satisfies both requirements.

Select

select is the initial query. It receives the partition the view was opened with and the window, and returns the rows.

Custom select
select: ({ postId }) => sql<Comment>(` select c.id, c.text, c.author, c.createdAt, c.postId, u.avatarUrl from comments c join users u on u.id = c.userId where c.postId = ${postId} `)

Use it for joins, projections, and computed columns. Every row it returns is checked against the partition, so a select that ignores the object it was given is refused rather than served. A windowed view also passes the select its order, limit and cursor as a second argument, and the query must apply all three; windows shows the sql.raw fragment that writes each clause.

A select may filter rows further than the partition, where archived = false say, to keep the first render small. Later writes are matched against the partition, not against the select, so a row that matches the partition is sent to the browser whether or not the select would have returned it. Filter in the template with filter() to decide which rows show. The select's WHERE only makes the first render smaller. See partitions.

Rows of any size broadcast. A row over the 8000-byte Postgres notification limit is broadcast as its id. Each app server reads the stored row once, matches it against its views, and reads it through select once per partition it matched, with the window's keyset() narrowed to that id, then sends the row to its browsers as usual. A select that includes ${w.keyset()} reads one row; one that leaves it out reads the partition and the row is picked out.

Composite tables

table names the database table the automatic handlers use. To back a LiveTable with a view, a join, or any custom SQL, override select and the mutators as needed. The rows still need an id, and mutations still return the row they wrote.

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