Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

React to table changes

Listen to the rows of another luna type's table, pick and filter what arrives, and handle each upsert, delete and clear.

Emitting an event next to every write repeats work, and one code path that forgets to emit leaves readers wrong. When the news is a row that changed, a luna type can listen to the table itself.

Listen to a table

Every luna type has a changes field with one source per table. Card.changes.assignees sends each committed change to the assignees table of every Card.

Here each User keeps the cards assigned to it. The key function sends each row to the User luna it names.

import { defineLuna, t, table } from "luna";
import { Card } from "./card.ts";

export const User = defineLuna({
  name: "User",
  key: { id: t.string() },
  schema: { cards: table({ board: t.string(), card: t.int() }) },
  on: (listen) => [
    listen(Card.changes.assignees, { key: (row) => row.user }, (ctx, change, from) => {
      const where = { board: String(from.key.board), card: Number(from.key.card) };
      ctx.db.cards.delete({ where });
      if (change.op === "upsert") ctx.db.cards.insert(where);
    }),
  ],
  handlers: (h) => ({
    cards: h({ query: true }, (ctx) => ctx.db.cards.find()),
  }),
});

The Card type is the one from Emit events, and it emits nothing here. Luna notes a table’s changes only while some luna type listens to them.

Choosing the listening luna, order, batches and retries work as for events. Listen to events covers them.

Handle each kind of change

The handler gets a TableChange with an op field.

op Means Also carries
upsert A row was inserted or updated key, the row’s primary key values, and row, the row as it is now
delete A row was deleted, or no longer reaches this listener key
clear Every row from that luna goes: the luna was destroyed, or the listen changed and every row is sent again Nothing

A row whose key column changes moves between listening lunas. The luna it left gets a delete, and the luna it reached gets an upsert.

An update that changes no column the listen uses sends nothing.

Pick columns with map

map names what the listener gets of each row. It takes the row and the emitting luna’s key fields.

const assigned = Card.changes.assignees.map((row, from) => ({
  board: from.board,
  card: from.card,
}));

Listen to assigned instead of Card.changes.assignees. Each upsert then carries { board, card } in change.row. The key function still reads the whole row.

With a map, an update reaches the listener only when it changes a mapped column, a column the key names, or whether the row matches where. Like a key, a map may only name fields and constants.

Filter rows with where

where keeps only the rows that match. Pass a value to compare for equality, null to match an empty column, or an object of operators.

const people = Card.changes.assignees.where({ user: { ne: "bot" } });
Operator Matches
eq, ne Equal, not equal
lt, lte, gt, gte Less than, at most, greater than, at least
in One of a list of values
isNull An empty column with true, a filled one with false

A row that starts to match arrives as an upsert. A row that stops matching arrives as a delete. where and map chain in either order.

Rows that existed before the listener

A deploy that adds a listen sends it every existing row that passes its filter, from each luna of the emitting type, in the background.

Changing a listen’s map, where or key makes each emitting luna send a clear, then all its rows again as the listen now declares them. Your handler must treat clear as the end of every row from that luna.

Next steps

Was this page helpful?