Kadak

Insert

Insert one or many rows with validator-first inputs, automatic defaults, and predictable return behavior.

Insert

insert() is Kadak’s create path. It supports single-row and bulk insertion with the same method.

Contract

db.users.insert(data: InferInsert<typeof users>): Promise<Infer<typeof users>>
db.users.insert(data: InferInsert<typeof users>[]): Promise<Infer<typeof users>[]>

Input is validated with your table’s insertValidator() before SQL execution.

Single insert

Use when creating one entity:

const row = await db.users.insert({
  email: 'amit@example.com',
  name: 'Amit',
  isActive: true,
})

Bulk insert

Use when writing multiple rows in one call:

const rows = await db.users.insert([
  { email: 'a@example.com', name: 'A', isActive: true },
  { email: 'b@example.com', name: 'B', isActive: true },
])

API-safe request flow

Treat HTTP payloads as untrusted. Validate first, insert second.

const parsed = usersTable.insertValidator().safeParse(req.body)
if (!parsed.success) {
  return res.status(400).json(parsed.error.flatten())
}

const row = await db.users.insert(parsed.data)
return res.status(201).json(row)

Runtime behavior

  • Applies schema defaults (default / defaultSql strategy).
  • Adds automation fields like updatedAt when configured as autoUpdate.
  • Returns plain JavaScript objects with camelCase keys.

Production guardrails

  • Do not accept raw request payloads without insertValidator().
  • Keep server-owned fields out of create contracts (id, createdAt, updatedAt, deletedAt).
  • For money/precision values, use decimal-safe handling strategy in your domain layer.

On this page