Skip to content

Insert

insert() adds rows. Pass a single object or an array for multi-row insert. Both compile to a single bound-parameter statement.

await db.from(users).insert({ email: "ada@example.com", age: 36 });
await db.from(users).insert([
{ email: "a@b.c", age: 20 },
{ email: "c@d.e", age: 30 },
]);

Chain .returning(...) to get the inserted row(s), including generated IDs:

const { rows } = await db.from(users).returning("id", "email").insert({
email: "ada@example.com",
});
// rows[0].id is the generated primary key
db.from(users).insert({ email: "a@b.c" });
// → INSERT INTO "users" ("email") VALUES (?)
export async function createUser(email: string, age: number) {
const { rows } = await db.from(users).returning("id").insert({ email, age });
return rows[0].id;
}
  • Use multi-row insert for bulk loads — one statement, many params.
  • Use returning("id") when you need the generated key.
  • Let InferTable type the values you pass.
  • Awaiting insert but ignoring the returned rows when you need the ID.
  • Hand-building a multi-row statement instead of passing an array.
  • Upsert — insert-or-update on conflict.
  • Update — modify existing rows.