App SDK
db
Read and write the app's own PostgreSQL database from its server code.
Server code only. Every function takes your fetch handler's env first, then one SQL statement and its values as $1, $2 parameters. The code can only reach the app's own schema: the tables its migrations create. Rows come back as objects keyed by column. See Database for a full example.
db.run
db.run(env, sql: string, params?: (string | number | boolean | null)[]): Promise<{ rowCount: number; rows: object[] }>Runs a statement that changes data: insert, update, delete. rows holds what a returning clause sends back.
db.all
db.all(env, sql: string, params?): Promise<object[]>Reads rows, in a read-only transaction. Up to 1,000 rows; add a limit for more control.
db.first
db.first(env, sql: string, params?): Promise<object | null>The first row, or null when there is none.
db.query
db.query(env, sql: string, params?, opts?: { mode?: 'read' | 'write' }): Promise<{ columns: string[]; rows: object[]; row_count: number; truncated: boolean; duration_ms: number }>The full result. mode is write unless you say read. truncated is true when there were more than 1,000 rows.
Limits and errors
- One statement per call. Transaction and session commands (
begin,set,set role) are refused. - 10 seconds per statement.
- A statement the database rejects throws an
Errorwhose message says why, for example a missing table or a broken constraint. - These calls are not written to the access log. Changes made from your AI tool with
db_queryare.