Skip to content

Commit 6a958df

Browse files
authored
Update DO + assets template to use sqlite (#8610)
* update do + assets template * Update lazy-beers-teach.md * update types
1 parent 29cb306 commit 6a958df

File tree

7 files changed

+42
-30
lines changed

7 files changed

+42
-30
lines changed

.changeset/lazy-beers-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": patch
3+
---
4+
5+
The Hello World DO templates were previously updated to use SQLite - this just also updates the DO + Assets template.

packages/create-cloudflare/templates/hello-world-durable-object-with-assets/js/src/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@ export default {
5252
* @returns {Promise<Response>} The response to be sent back to the client
5353
*/
5454
async fetch(request, env, ctx) {
55-
// We will create a `DurableObjectId` using the pathname from the Worker request
56-
// This id refers to a unique instance of our 'MyDurableObject' class above
57-
let id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
55+
// Create a `DurableObjectId` for an instance of the `MyDurableObject`
56+
// class named "foo". Requests from all Workers to the instance named
57+
// "foo" will go to a single globally unique Durable Object instance.
58+
const id = env.MY_DURABLE_OBJECT.idFromName("foo");
5859

59-
// This stub creates a communication channel with the Durable Object instance
60-
// The Durable Object constructor will be invoked upon the first call for a given id
61-
let stub = env.MY_DURABLE_OBJECT.get(id);
60+
// Create a stub to open a communication channel with the Durable
61+
// Object instance.
62+
const stub = env.MY_DURABLE_OBJECT.get(id);
6263

63-
// We call the `sayHello()` RPC method on the stub to invoke the method on the remote
64-
// Durable Object instance
65-
let greeting = await stub.sayHello("world");
64+
// Call the `sayHello()` RPC method on the stub to invoke the method on
65+
// the remote Durable Object instance
66+
const greeting = await stub.sayHello("world");
6667

6768
return new Response(greeting);
68-
},
69+
}
6970
};

packages/create-cloudflare/templates/hello-world-durable-object-with-assets/js/wrangler.jsonc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
}
1515
]
1616
},
17-
"migrations": [
17+
"migrations": [
1818
{
19-
"new_classes": [
19+
"new_sqlite_classes": [
2020
"MyDurableObject"
2121
],
2222
"tag": "v1"

packages/create-cloudflare/templates/hello-world-durable-object-with-assets/ts/src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,18 @@ export default {
4949
* @returns The response to be sent back to the client
5050
*/
5151
async fetch(request, env, ctx): Promise<Response> {
52-
// We will create a `DurableObjectId` using the pathname from the Worker request
53-
// This id refers to a unique instance of our 'MyDurableObject' class above
54-
let id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
52+
// Create a `DurableObjectId` for an instance of the `MyDurableObject`
53+
// class named "foo". Requests from all Workers to the instance named
54+
// "foo" will go to a single globally unique Durable Object instance.
55+
const id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName("foo");
5556

56-
// This stub creates a communication channel with the Durable Object instance
57-
// The Durable Object constructor will be invoked upon the first call for a given id
58-
let stub = env.MY_DURABLE_OBJECT.get(id);
57+
// Create a stub to open a communication channel with the Durable
58+
// Object instance.
59+
const stub = env.MY_DURABLE_OBJECT.get(id);
5960

60-
// We call the `sayHello()` RPC method on the stub to invoke the method on the remote
61-
// Durable Object instance
62-
let greeting = await stub.sayHello("world");
61+
// Call the `sayHello()` RPC method on the stub to invoke the method on
62+
// the remote Durable Object instance
63+
const greeting = await stub.sayHello("world");
6364

6465
return new Response(greeting);
6566
},
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// Generated by Wrangler by running `wrangler types`
2-
3-
interface Env {
4-
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").MyDurableObject>;
1+
// Generated by Wrangler by running `wrangler types --include-runtime=false` (hash: 24be54e90498ce3774054fcb8bb2bc1a)
2+
declare namespace Cloudflare {
3+
interface Env {
4+
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").MyDurableObject>;
5+
ASSETS: Fetcher;
6+
}
57
}
8+
interface Env extends Cloudflare.Env {}

packages/create-cloudflare/templates/hello-world-durable-object-with-assets/ts/wrangler.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"compatibility_date": "<TBD>",
55
"migrations": [
66
{
7-
"new_classes": [
7+
"new_sqlite_classes": [
88
"MyDurableObject"
99
],
1010
"tag": "v1"
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// Generated by Wrangler by running `wrangler types`
2-
3-
interface Env {
4-
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").MyDurableObject>;
1+
// Generated by Wrangler by running `wrangler types --include-runtime=false` (hash: 4c81d17e8de07f650cddf73ce751ba8d)
2+
declare namespace Cloudflare {
3+
interface Env {
4+
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").MyDurableObject>;
5+
}
56
}
7+
interface Env extends Cloudflare.Env {}

0 commit comments

Comments
 (0)