Back to gallery

Event handlers

A DB write automatically fires a registered handler.

Live
onDbChange (events/) — DB.insertOne triggers it

The registered handler

This lives in events/on-demo-task.ts. It is never called by app code — the runtime fires it whenever demo_tasks is written.

import { onDbChange } from '@maravilla-labs/platform/events';

export const onDemoTaskLogged = onDbChange({ collection: 'demo_tasks' }, async (event, ctx) => {
  const db = ctx.platform?.env?.DB ?? ctx.database;
  try {
    await db.insertOne('demo_event_log', {
      at: new Date().toISOString(),
      op: event?.op ?? event?.type ?? 'change',
      collection: 'demo_tasks',
      docId: event?.id ?? event?.documentId ?? null,
      note: 'onDbChange handler fired automatically'
    });
  } catch { /* best-effort marker */ }
});
ts

Fire it

Insert one demo_tasks row. That single DB write should make a marker appear in demo_event_log below — written by the handler, not by this page.

demo_event_log 11 markers
  • insert · 2026-09-03T10:00:09.495Z

    onDbChange handler fired automatically

  • insert · 2026-09-03T10:00:05.150Z

    onDbChange handler fired automatically

  • insert · 2026-08-19T08:30:33.418Z

    onDbChange handler fired automatically

  • insert · 2026-08-19T08:30:33.371Z

    onDbChange handler fired automatically

  • insert · 2026-08-19T08:30:33.323Z

    onDbChange handler fired automatically

  • insert · 2026-08-19T08:30:33.268Z

    onDbChange handler fired automatically

  • delete · 2026-08-19T08:30:33.212Z

    onDbChange handler fired automatically

  • delete · 2026-08-19T08:30:33.150Z

    onDbChange handler fired automatically

  • delete · 2026-08-19T08:30:33.084Z

    onDbChange handler fired automatically

  • delete · 2026-08-19T08:30:32.993Z

    onDbChange handler fired automatically

  • delete · 2026-08-19T08:30:32.906Z

    onDbChange handler fired automatically

Maravilla

A live kitchen-sink tour of every Maravilla cloud capability. Each mini-app runs against the local dev platform.

Built on Maravilla Cloud