INSERT Statements

Insert data into your database with genaql's clean syntax.

Basic INSERT

basic.ts
// Insert a single row
cook`nocap:users drip:name,email fire:John,john@example.com`
// → INSERT INTO users (name, email) VALUES ($1, $2)

With RETURNING

Get back the inserted row (PostgreSQL and SQLite 3.35+):

returning.ts
// Return the inserted ID
cook`nocap:users drip:name,email fire:John,john@example.com flex:id`
// → INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id

// Return all columns
cook`nocap:users drip:name,email fire:John,john@example.com flex:*`
// → INSERT INTO users (name, email) VALUES ($1, $2) RETURNING *

// Return specific columns
cook`nocap:users drip:name,email fire:John,john@example.com flex:id,created_at`
// → INSERT INTO users ... RETURNING id, created_at

With Variables

variables.ts
// Using template interpolation
const name = "John";
const email = "john@example.com";

cook`nocap:users drip:name,email fire:${name},${email}`
// Values are automatically parameterized for safety

Bulk Insert

bulk.ts
// Insert multiple rows
const users = [
  { name: "John", email: "john@test.com" },
  { name: "Jane", email: "jane@test.com" },
  { name: "Bob", email: "bob@test.com" }
];

// Using the fluent API for bulk inserts
db.insert('users')
  .columns('name', 'email')
  .values(users.map(u => [u.name, u.email]))
  .returning('id');
// → INSERT INTO users (name, email) VALUES ($1, $2), ($3, $4), ($5, $6) RETURNING id

UPSERT (ON CONFLICT)

upsert.ts
// Insert or update on conflict (PostgreSQL)
cook`nocap:users drip:email,name fire:john@test.com,John conflict:email do:glow rizz:name=John`
// → INSERT INTO users (email, name) VALUES ($1, $2)
//   ON CONFLICT (email) DO UPDATE SET name = $3

// Insert or ignore
cook`nocap:users drip:email,name fire:john@test.com,John conflict:email do:nothing`
// → INSERT INTO users (email, name) VALUES ($1, $2)
//   ON CONFLICT (email) DO NOTHING

Security Note

All values in genaql are automatically parameterized, protecting against SQL injection. Never concatenate user input directly into queries.