Skip to content

Commit 7267caf

Browse files
committed
sql API yo yo yo
1 parent c43f550 commit 7267caf

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/content/docs/agents/examples/manage-and-sync-state.mdx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,52 @@ Common use cases:
112112

113113
Every individual Agent instance has its own SQL (SQLite) database that runs _within the same context_ as the Agent itself. This means that inserting or querying data within your Agent is effectively zero-latency: the Agent doesn't have to round-trip across a continent or the world to access its own data.
114114

115-
TODO
115+
You can access the SQL API within any method on an Agent via `this.sql`. The SQL API accepts template literals, and
116116

117117
<TypeScriptExample>
118118

119119
```ts
120+
export class MyAgent extends Agent<Env> {
121+
async onRequest(request: Request) {
122+
let userId = new URL(request.url).searchParams.get('userId');
123+
124+
// 'users' is just an example here: you can create arbitrary tables and define your own schemas
125+
// within each Agent's database using SQL (SQLite syntax).
126+
let user = await this.sql`SELECT * FROM users WHERE id = ${userId}`
127+
return Response.json(user)
128+
}
129+
}
130+
```
131+
132+
</TypeScriptExample>
120133

134+
You can also supply a [TypeScript type argument](https://www.typescriptlang.org/docs/handbook/2/generics.html#using-type-parameters-in-generic-constraints) the query, which will be used to infer the type of the result:
121135

136+
```ts
137+
type User = {
138+
id: string;
139+
name: string;
140+
email: string;
141+
};
142+
143+
export class MyAgent extends Agent<Env> {
144+
async onRequest(request: Request) {
145+
let userId = new URL(request.url).searchParams.get('userId');
146+
// Supply the type paramter to the query when calling this.sql
147+
// This assumes the results return a single User row with "id", "name", and "email" columns
148+
// You can also pass an array as the type argument to the query - e.g. User[]
149+
const user = await this.sql<User>`SELECT * FROM users WHERE id = ${userId}`;
150+
return Response.json(user)
151+
}
152+
}
122153
```
123154

124-
</TypeScriptExample>
155+
Providing a type parameter does not validate that the result matches your type definition. In TypeScript, properties (fields) that do not exist or conform to the type you provided will be dropped. If you need to validate incoming events, we recommend a library such as [zod](https://zod.dev/) or your own validator logic.
125156

126157
:::note
127158

128159
Learn more about the zero-latency SQL storage that powers both Agents and Durable Objects [on our blog](https://blog.cloudflare.com/sqlite-in-durable-objects/).
129160

130161
:::
131162

132-
The SQL API exposed to an Agent is identical to the one [within by Durable Objects](/durable-objects/api/sql-storage/). This means that you can use the same SQL queries and commands to interact with the Agent's database.
163+
The SQL API exposed to an Agent is similar to the one [within Durable Objects](/durable-objects/api/sql-storage/). This means that you can use the same SQL queries with the Agent's database, create tables, and query data.

0 commit comments

Comments
 (0)