superapp
ClientQueries

Insert

Insert one or more rows with type-safe values.

Use db.insert() to add rows to a table.

import { drizzle } from '@superapp/db'
import * as schema from './generated/schema'

const db = drizzle({ connection, token, schema })

// The backend validates that the logged-in user has insert permission
// and may auto-set fields like customerId from the user's session.
const [order] = await db.insert(schema.orders).values({
  amount: 250,
  status: 'pending',
  customerId: 'cust_abc123',
}).returning()
// order: { id, amount, status, customerId, createdAt }

Insert Without Returning

await db.insert(schema.orders).values({
  amount: 250,
  status: 'pending',
  customerId: 'cust_abc123',
})

Insert Multiple Rows

await db.insert(schema.orders).values([
  { amount: 100, status: 'pending', customerId: 'cust_1' },
  { amount: 200, status: 'pending', customerId: 'cust_2' },
  { amount: 300, status: 'pending', customerId: 'cust_3' },
])

Upsert (On Conflict)

await db.insert(schema.customers)
  .values({ id: 'cust_abc123', name: 'Acme Corp', email: 'billing@acme.com' })
  .onConflictDoNothing()

Column Presets

If your server configuration defines column presets (e.g., automatically setting organizationId or createdBy from the session), those columns are injected server-side. You don't need to include them:

// If the server has a preset: organizationId = $user.organizationId
await db.insert(schema.orders).values({
  amount: 250,
  status: 'pending',
  customerId: 'cust_abc123',
})
// organizationId is automatically set by the server

On this page