Skip to content

Commit 49f185c

Browse files
committed
fix Durable Objects example to be idiomatic
1 parent 5085da3 commit 49f185c

File tree

2 files changed

+71
-23
lines changed

2 files changed

+71
-23
lines changed

src/content/docs/connect-cloudflare-do.mdx

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,31 @@ export class MyDurableObject extends DurableObject {
7272
super(ctx, env);
7373
this.storage = ctx.storage;
7474
this.db = drizzle(this.storage, { logger: false });
75+
76+
// Make sure all migrations complete before accepting queries.
77+
// Otherwise you will need to run `this.migrate()` in any function
78+
// that accesses the Drizzle database `this.db`.
79+
ctx.blockConcurrencyWhile(async () => {
80+
await this._migrate();
81+
});
7582
}
7683

77-
async migrate() {
78-
migrate(this.db, migrations);
79-
}
84+
async insertAndList(user: typeof usersTable.$inferInsert) {
85+
await this.insert(user);
86+
return this.select();
87+
}
8088

8189
async insert(user: typeof usersTable.$inferInsert) {
82-
await this.db.insert(usersTable).values(user);
83-
}
90+
await this.db.insert(usersTable).values(user);
91+
}
8492

8593
async select() {
86-
return this.db.select().from(usersTable);
87-
}
94+
return this.db.select().from(usersTable);
95+
}
96+
97+
async _migrate() {
98+
migrate(this.db, migrations);
99+
}
88100
}
89101

90102
export default {
@@ -99,8 +111,20 @@ export default {
99111
async fetch(request: Request, env: Env): Promise<Response> {
100112
const id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName('durable-object');
101113
const stub = env.MY_DURABLE_OBJECT.get(id);
102-
await stub.migrate();
103114

115+
// Option A - Maximum performance.
116+
// Prefer to bundle all the database interaction within a single Durable Object call
117+
// for maximum performance, since database access is fast within a DO.
118+
const usersAll = await stub.insertAndList({
119+
name: 'John',
120+
age: 30,
121+
122+
});
123+
console.log('New user created. Getting all users from the database: ', users);
124+
125+
// Option B - Slow but maybe useful sometimes for debugging.
126+
// You can also directly call individual Drizzle queries if they are exposed
127+
// but keep in mind every query is a round-trip to the Durable Object instance.
104128
await stub.insert({
105129
name: 'John',
106130
age: 30,
@@ -111,8 +135,8 @@ export default {
111135
const users = await stub.select();
112136
console.log('Getting all users from the database: ', users);
113137

114-
return new Response();
115-
}
138+
return Response.json(users);
139+
}
116140
}
117141
```
118142

src/content/docs/get-started/do-new.mdx

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ export class MyDurableObject extends DurableObject {
143143
this.db = drizzle(this.storage, { logger: false });
144144
}
145145

146-
async migrate() {
147-
migrate(this.db, migrations);
148-
}
146+
async migrate() {
147+
migrate(this.db, migrations);
148+
}
149149
}
150150
```
151151

@@ -167,19 +167,31 @@ export class MyDurableObject extends DurableObject {
167167
super(ctx, env);
168168
this.storage = ctx.storage;
169169
this.db = drizzle(this.storage, { logger: false });
170+
171+
// Make sure all migrations complete before accepting queries.
172+
// Otherwise you will need to run `this.migrate()` in any function
173+
// that accesses the Drizzle database `this.db`.
174+
ctx.blockConcurrencyWhile(async () => {
175+
await this._migrate();
176+
});
170177
}
171178

172-
async migrate() {
173-
migrate(this.db, migrations);
174-
}
179+
async insertAndList(user: typeof usersTable.$inferInsert) {
180+
await this.insert(user);
181+
return this.select();
182+
}
175183

176184
async insert(user: typeof usersTable.$inferInsert) {
177-
await this.db.insert(usersTable).values(user);
178-
}
185+
await this.db.insert(usersTable).values(user);
186+
}
179187

180188
async select() {
181-
return this.db.select().from(usersTable);
182-
}
189+
return this.db.select().from(usersTable);
190+
}
191+
192+
async _migrate() {
193+
migrate(this.db, migrations);
194+
}
183195
}
184196

185197
export default {
@@ -194,8 +206,20 @@ export default {
194206
async fetch(request: Request, env: Env): Promise<Response> {
195207
const id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName('durable-object');
196208
const stub = env.MY_DURABLE_OBJECT.get(id);
197-
await stub.migrate();
198209

210+
// Option A - Maximum performance.
211+
// Prefer to bundle all the database interaction within a single Durable Object call
212+
// for maximum performance, since database access is fast within a DO.
213+
const usersAll = await stub.insertAndList({
214+
name: 'John',
215+
age: 30,
216+
217+
});
218+
console.log('New user created. Getting all users from the database: ', users);
219+
220+
// Option B - Slow but maybe useful sometimes for debugging.
221+
// You can also directly call individual Drizzle queries if they are exposed
222+
// but keep in mind every query is a round-trip to the Durable Object instance.
199223
await stub.insert({
200224
name: 'John',
201225
age: 30,
@@ -206,7 +230,7 @@ export default {
206230
const users = await stub.select();
207231
console.log('Getting all users from the database: ', users);
208232

209-
return new Response();
210-
}
233+
return Response.json(users);
234+
}
211235
}
212236
```

0 commit comments

Comments
 (0)