Skip to content

Batch operations

type Task = { id: string; title: string; status: string; project_id: string };
const { count, records } = await vaif.from<Task>("tasks").createMany(
[
{ title: "Write the spec", status: "open", project_id: "p1" },
{ title: "Review the spec", status: "open", project_id: "p1" },
],
{ returnRecords: true }
);
console.log(count, records);

One call inserts up to 1000 rows, and every row in it must have the same columns. Pass { returnRecords: true } to get the inserted rows back.

Filter the builder, then call updateMany() or deleteMany():

type Job = { id: string; project_id: string; status: string };
const archived = await vaif.from<Job>("jobs").eq("project_id", "p1").eq("status", "done").updateMany({ status: "archived" });
const removed = await vaif.from<Job>("jobs").eq("status", "archived").deleteMany();
console.log(archived.count, removed.count);

Bulk updates and deletes accept equality filters only: .eq(), or .where() with an AND of equality clauses. A range, pattern or OR filter throws a VaifValidationError before any request is sent, so a bulk write never reaches more rows than you wrote.

type Profile = { id: string; email: string; display_name: string };
await vaif.from<Profile>("profiles").upsert(
{ email: "user@example.com", display_name: "Sam" },
{ conflictFields: ["email"] }
);

conflictFields must name columns with a unique constraint. When a row with the same values exists, every other column is updated.

The data API does not run several statements as one transaction: there is no transaction endpoint, and the SDK’s transaction() and withTransaction() throw a VaifError with the code NOT_SUPPORTED.

What to do instead:

  • One table, many rows: one createMany(), updateMany() or deleteMany() call is one request.
  • Several tables that must change together: make each step safe to retry (for example, write a row with a unique key and treat a conflict as “already done”), and run the steps in order from your server or from a function, so a failed step can be retried.
  • Changes to the schema with data: a SQL migration file applied with vaif db push runs in a single transaction. See Migrations.