Write rows
Insert rows, bulk insert, upsert, update, and delete with the Database Data API.
Use the table client for typed writes. Every write is checked against the current environment's table permissions. The API rejects updates and deletes without a where condition.
const todos = carpo.database.withSchema<AppDatabase>().from('todos')
const inserted = await todos.insert({ title: 'Ship the change', completed: false })
const batch = await todos.insertMany([
{ title: 'Write the guide', completed: false },
{ title: 'Review the guide', completed: false },
], { returning: ['id', 'title'] })
const saved = await todos.upsert(
[{ id: 7, title: 'Ship the change', completed: true }],
{ conflict: 'update', returning: true },
)
await todos.update({ where: { id: 7 }, values: { completed: true } })
await todos.delete({ id: 8 })insertMany() accepts 1 to 100 rows. upsert() requires conflict: 'ignore' or conflict: 'update'; when needed, set conflictColumns. The default conflict target is the table primary key. returning may be true or a list of column names.
Write methods return rowsAffected. Inserts can return lastInsertRowId. Bulk inserts and upserts can also return selected rows when returning is set.
Owner scoped tables use the authenticated user ID as the owner value. The SDK does not expose an owner override. For a transaction or a SQL statement that the Data API does not support, use direct server Database access and enforce the caller's authorization in trusted code.