Skip to content
clientdb

Search the docs

Find a page or section

Guides

Writes made outside clientdb

Rows your app writes outside clientdb (Prisma Client, seed scripts, SQL, other services) reach clients through Postgres triggers, with engine.reconcile() as the safety net.

On Prisma-managed tables, clientdb keeps sync metadata for each row in a side table (clientdb_row): the last copy it sent to clients, and its routing groups. Writes from browsers go through clientdb, so it always knows about them. Writes your server makes directly need one more piece, so clientdb learns which rows changed. The examples use the todo app from the quick start.

Where they come from

  • server actions, tRPC routers, background jobs and admin tools that write with Prisma Client;
  • seed scripts and fixtures;
  • bulk SQL, backfills, manual fixes in a SQL console, and other services sharing the database.

Default: Postgres triggers

Generate a migration that adds change triggers to every synced table, and apply it like any other Prisma migration:

Terminal
npx clientdb-prisma triggers src/sync/schema.ts > prisma/migrations/20261202_clientdb_triggers/migration.sql
npx prisma migrate dev

triggers reads your schema module (--export name if the schema isn't exported as schema) and covers every model it syncs, serverOnly ones included, since rules read those. It also drops the triggers of tables that no longer sync.

From then on, every insert, update, delete and truncate on a synced table records the changed ids in a queue, in the same transaction as the write. The engine drains the queue on the next pull or push (or right away when engine.start() runs), reads the rows, and syncs them like writes from browsers. Your write code needs no clientdb calls:

src/app/projects/actions.ts
"use server";
import { prisma } from "@/lib/prisma";

export async function addTodo(projectId: string, authorId: string, title: string) {
  await prisma.todo.create({ data: { projectId, authorId, title } });
}
  • Catches everything: Prisma Client (nested writes and createMany/updateMany/deleteMany included), raw SQL, other services, TRUNCATE, updates that change a row's id, and rows Postgres or your own triggers changed in turn (ON DELETE CASCADE, SET NULL), including after writes from browsers.
  • Atomic: the queue entry commits or rolls back with your write.
  • Same treatment as browser writes: the rows reach every user who may see them, and the access changes they cause are applied.
  • Run the command again when you add or remove a synced model, into a new migration. Prisma ignores triggers, so prisma migrate diff reports no drift.

The browser that called the action can wait for its rows with db.$action.

Safety net: engine.reconcile()

engine.reconcile() compares your tables with clientdb's last synced copies and syncs every difference: new rows, changed rows and deleted rows.

scripts/clientdb-reconcile.ts
import { engine } from "@/sync/engine";

const reconciled = await engine.reconcile(); // { todo: 3 }
console.log(reconciled);
  • clientdb migrate runs it on every deploy, after adopting rows that were never tracked. Run it again after seeding a database before the triggers were installed.
  • Run it from a cron if some writes may bypass the triggers (for example while a trigger migration is pending).
  • It reads whole tables, a page at a time: fine for thousands of rows, slow for millions. Run it from a job, never per request.

Bulk changes: resync. To send a whole model again, for example after a large backfill, use resync:

TypeScript
import { engine } from "@/sync/engine";

await engine.resync("todo"); // one model
await engine.resync(); // every synced model

Above resyncInvalidateThreshold (10,000 rows by default), clients receive one invalidation instead of the rows, and refetch the model.

Only without triggers: engine.changed

If you can't add triggers to your tables, tell the engine which rows you wrote. With the triggers installed, don't: they already do this. Model names are typed keys:

src/app/projects/actions.ts
"use server";
import { prisma } from "@/lib/prisma";
import { engine } from "@/sync/engine";

export async function addTodo(projectId: string, authorId: string, title: string) {
  const todo = await prisma.todo.create({ data: { projectId, authorId, title } });
  await engine.changed({ todo: [todo.id] });
}

To make it atomic with the write, call it inside the transaction:

src/app/projects/moveTodo.ts
"use server";
import { prismaExecutor } from "@clientdb/prisma";
import { prisma } from "@/lib/prisma";
import { engine } from "@/sync/engine";

export async function moveTodo(todoId: string, projectId: string) {
  await prisma.$transaction(async (transaction) => {
    await transaction.todo.update({ where: { id: todoId }, data: { projectId } });
    await engine.changed({ todo: [todoId] }, { transaction: prismaExecutor(transaction) });
  });
}

Called after the transaction, a crash between the write and engine.changed leaves the row unsent until engine.reconcile() runs. Each write site must list every id it touched, including nested creates and *Many calls; triggers do this for you.

The warnings

In development, clientdbHandler and createFetchHandler check two things on their first request and warn once. Tables without triggers:

Text
clientdb: writes to todo, comment made outside clientdb (Prisma Client, SQL) won't reach clients: their tables have no clientdb capture triggers. Save the output of `npx clientdb-prisma triggers` as a migration, or call engine.changed() after such writes.

And rows written outside clientdb that it has never tracked, so no client receives them:

Text
clientdb: 3 todo rows are not tracked (written outside clientdb, e.g. by a seed script), so clients never receive them. Run engine.migrate() (or engine.reconcile()) to sync them, and capture future writes with the triggers from `npx clientdb-prisma triggers`.

The checks are on unless NODE_ENV is production. Turn them off with warnUntracked: false, for example in an app that deliberately uses engine.changed:

src/app/api/sync/[...path]/route.ts
import { clientdbHandler } from "@clientdb/server/next";
import { getSyncSession } from "@/sync/session";
import { engine } from "@/sync/engine";

export const { GET, POST } = clientdbHandler(engine, { session: getSyncSession, warnUntracked: false });

Checking from code

TypeScript
import { engine } from "@/sync/engine";

await engine.untracked(); // [{ model: "todo", rows: 3 }]: rows never tracked
await engine.uncaptured(); // ["comment"]: synced models whose table has no triggers

Both return an empty list on engine-owned tables, where every write goes through the engine.

Summary

SituationFix
App code, scripts, SQL and other services writing to Prisma tablesthe trigger migration (npx clientdb-prisma triggers <schema module>)
Rows written before the triggers existed, or any driftengine.reconcile() (also runs in every clientdb migrate)
Rows changed in bulk with SQLengine.resync(model) or engine.resync()
An app that can't use triggersengine.changed({ model: [ids] }), inside the transaction for atomicity
Not sureengine.untracked() and engine.uncaptured(), or read the dev warnings