db_query
SQL query, run SQL, SELECT, INSERT, UPDATE, DELETE, execute SQL against the project database — the read/write-ROWS entry point. A documented Postgres-flavored syntax subset is translated; the default database keeps its own expression, typing, comparison, and ordering semantics. For example, division by zero returns NULL rather than throwing. The database is created on first use, but tables are not — new projects declare them in db/schema.ts and deploy raw source; db_migrate is for SQL-mode projects. Raw SQL reaches the project's raw SQL database only; on a project that keeps its tables in the managed database, rows are read with db_browse (plane: "managed"), schema with db_describe, and writes go through sw.db in functions.
Schema changes (CREATE / ALTER / DROP) belong in db/schema.ts plus deploy for managed projects, or db_migrate for SQL-mode projects, not here, and not inline in a deployed function's handler: runtime DDL runs on every request, isn't versioned, and hides new tables from the platform's per-user data protections (a table with an owner_id/user_id column is cross-user readable until it's scoped). db_query is for the rows.
Every successful query returns { data: [], count: N, changes: N, last_row_id: X }: data contains returned rows, count counts those rows, and changes counts affected rows. A write without RETURNING has empty data and a zero count even when it changes rows. last_row_id is a number, an exact decimal integer string when the ID exceeds JavaScript’s safe integer range, or null; other transport metadata may also be present.
Example:
{
"project_id": "my-saas",
"sql": "SELECT id, email FROM users WHERE created_at > ? ORDER BY created_at DESC LIMIT 20",
"params": [1704067200]
}The database has a 30-second hard query ceiling. timeout_ms bounds how long this call waits (default 30000, max 30000) and returns QUERY_TIMEOUT; it does not promise cancellation of work already accepted by the database.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"project_id": "<project_id>",
"sql": "<sql>"
},
"name": "db_query"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | SQL to execute. User input is represented by bound ? placeholders with params rather than string concatenation. | |
| draft | No | Alias for the preview target flag. | |
| params | No | Bind parameters for ? placeholders. A native JSON array such as ["alice@example.com", 42] is preferred; a JSON-stringified array is also accepted. The field is optional when no placeholders exist. | |
| target | No | Database target. Preview requires preview_session_id (or draft_id). | |
| preview | No | Explicit preview target flag. Cannot be combined with a production target. | |
| draft_id | No | Alias for preview_session_id. | |
| preview_id | No | Optional expected preview candidate release id. | |
| production | No | Explicit production target flag. Cannot be combined with a preview target. | |
| project_id | Yes | Project ID (UUID), subdomain, or slug — resolved server-side, no UUID lookup needed. 'default' also works when the account has exactly one project; multi-project accounts must name one (the error lists them). | |
| timeout_ms | No | Stop waiting after N ms. Default 30000. Clamped to [1, 30000]. Returns QUERY_TIMEOUT; already-accepted database work may continue up to the hard ceiling. | |
| preview_session_id | No | Identifier for an active preview session's isolated database. | |
| candidate_release_id | No | Alias for preview_id. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ok | Yes | ||
| data | Yes |