You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/agents/examples/manage-and-sync-state.mdx
+34-3Lines changed: 34 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,21 +112,52 @@ Common use cases:
112
112
113
113
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.
114
114
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
116
116
117
117
<TypeScriptExample>
118
118
119
119
```ts
120
+
exportclassMyAgentextendsAgent<Env> {
121
+
async onRequest(request:Request) {
122
+
let userId =newURL(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 =awaitthis.sql`SELECT * FROM users WHERE id = ${userId}`
127
+
returnResponse.json(user)
128
+
}
129
+
}
130
+
```
131
+
132
+
</TypeScriptExample>
120
133
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:
121
135
136
+
```ts
137
+
typeUser= {
138
+
id:string;
139
+
name:string;
140
+
email:string;
141
+
};
142
+
143
+
exportclassMyAgentextendsAgent<Env> {
144
+
async onRequest(request:Request) {
145
+
let userId =newURL(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 =awaitthis.sql<User>`SELECT * FROM users WHERE id = ${userId}`;
150
+
returnResponse.json(user)
151
+
}
152
+
}
122
153
```
123
154
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.
125
156
126
157
:::note
127
158
128
159
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/).
129
160
130
161
:::
131
162
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