Kadak

Insert

Create one row or many rows with validation.

Insert

Problem: invalid payloads crash production APIs.

Solution: validate with insertValidator() before insert().

Insert one row

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

Insert many rows

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

Safe insert from request body

const parsed = usersTable.insertValidator().safeParse(req.body)

if (!parsed.success) {
  return res.status(400).json({ error: parsed.error.flatten() })
}

const user = await db.users.insert(parsed.data)

Important: decimal fields use strings ('12.50'), not JS floats.

On this page