Skip to content

Commit 7d314b1

Browse files
authored
Update C3 templates to use Durable Object getByName (#10419)
1 parent 3024ec1 commit 7d314b1

File tree

7 files changed

+49
-56
lines changed

7 files changed

+49
-56
lines changed

.changeset/curvy-feet-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": minor
3+
---
4+
5+
Update create-cloudflare hello-world Durable Objects templates to use `getByName()`

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ export default {
5252
* @returns {Promise<Response>} The response to be sent back to the client
5353
*/
5454
async fetch(request, env, ctx) {
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");
59-
60-
// Create a stub to open a communication channel with the Durable
61-
// Object instance.
62-
const stub = env.MY_DURABLE_OBJECT.get(id);
55+
// Create a stub to open a communication channel with the Durable Object
56+
// instance named "foo".
57+
//
58+
// Requests from all Workers to the Durable Object instance named "foo"
59+
// will go to a single remote Durable Object instance.
60+
const stub = env.MY_DURABLE_OBJECT.getByName("foo");
6361

6462
// Call the `sayHello()` RPC method on the stub to invoke the method on
65-
// the remote Durable Object instance
63+
// the remote Durable Object instance.
6664
const greeting = await stub.sayHello("world");
6765

6866
return new Response(greeting);

packages/create-cloudflare/templates/hello-world-durable-object-with-assets/py/src/entry.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,15 @@ async def say_hello(self, name):
5050
"""
5151
class Default(WorkerEntrypoint):
5252
async def fetch(self, request):
53-
# Create a `DurableObjectId` for an instance of the `MyDurableObject`
54-
# class named "foo". Requests from all Workers to the instance named
55-
# "foo" will go to a single globally unique Durable Object instance.
56-
id = self.env.MY_DURABLE_OBJECT.idFromName("foo")
53+
# Create a stub to open a communication channel with the Durable Object
54+
# instance named "foo".
55+
#
56+
# Requests from all Workers to the Durable Object instance named "foo"
57+
# will go to a single remote Durable Object instance.
58+
stub = self.env.MY_DURABLE_OBJECT.getByName("foo")
5759

58-
# Create a stub to open a communication channel with the Durable
59-
# Object instance.
60-
stub = self.env.MY_DURABLE_OBJECT.get(id)
61-
62-
# Call the `say_hello()` RPC method on the stub to invoke the method on
63-
# the remote Durable Object instance
60+
# Call the `say_hello()` RPC method on the stub to invoke the method on
61+
# the remote Durable Object instance.
6462
greeting = await stub.say_hello("world")
6563

6664
return Response(greeting)

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@ export default {
4949
* @returns The response to be sent back to the client
5050
*/
5151
async fetch(request, env, ctx): Promise<Response> {
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");
56-
57-
// Create a stub to open a communication channel with the Durable
58-
// Object instance.
59-
const stub = env.MY_DURABLE_OBJECT.get(id);
52+
// Create a stub to open a communication channel with the Durable Object
53+
// instance named "foo".
54+
//
55+
// Requests from all Workers to the Durable Object instance named "foo"
56+
// will go to a single remote Durable Object instance.
57+
const stub = env.MY_DURABLE_OBJECT.getByName("foo");
6058

6159
// Call the `sayHello()` RPC method on the stub to invoke the method on
62-
// the remote Durable Object instance
60+
// the remote Durable Object instance.
6361
const greeting = await stub.sayHello("world");
6462

6563
return new Response(greeting);

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ export default {
5252
* @returns {Promise<Response>} The response to be sent back to the client
5353
*/
5454
async fetch(request, env, ctx) {
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");
59-
60-
// Create a stub to open a communication channel with the Durable
61-
// Object instance.
62-
const stub = env.MY_DURABLE_OBJECT.get(id);
55+
// Create a stub to open a communication channel with the Durable Object
56+
// instance named "foo".
57+
//
58+
// Requests from all Workers to the Durable Object instance named "foo"
59+
// will go to a single remote Durable Object instance.
60+
const stub = env.MY_DURABLE_OBJECT.getByName("foo");
6361

6462
// Call the `sayHello()` RPC method on the stub to invoke the method on
65-
// the remote Durable Object instance
63+
// the remote Durable Object instance.
6664
const greeting = await stub.sayHello("world");
6765

6866
return new Response(greeting);

packages/create-cloudflare/templates/hello-world-durable-object/py/src/entry.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,15 @@ class Default(WorkerEntrypoint):
5050
* @returns {Promise<Response>} The response to be sent back to the client
5151
"""
5252
async def fetch(self, request):
53-
# Create a `DurableObjectId` for an instance of the `MyDurableObject`
54-
# class named "foo". Requests from all Workers to the instance named
55-
# "foo" will go to a single globally unique Durable Object instance.
56-
id = self.env.MY_DURABLE_OBJECT.idFromName("foo")
53+
# Create a stub to open a communication channel with the Durable Object
54+
# instance named "foo".
55+
#
56+
# Requests from all Workers to the Durable Object instance named "foo"
57+
# will go to a single remote Durable Object instance.
58+
stub = self.env.MY_DURABLE_OBJECT.getByName("foo")
5759

58-
# Create a stub to open a communication channel with the Durable
59-
# Object instance.
60-
stub = self.env.MY_DURABLE_OBJECT.get(id)
61-
62-
# Call the `say_hello()` RPC method on the stub to invoke the method on
63-
# the remote Durable Object instance
60+
# Call the `say_hello()` RPC method on the stub to invoke the method on
61+
# the remote Durable Object instance.
6462
greeting = await stub.say_hello("world")
6563

6664
return Response(greeting)

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,15 @@ export default {
4848
* @returns The response to be sent back to the client
4949
*/
5050
async fetch(request, env, ctx): Promise<Response> {
51-
// Create a `DurableObjectId` for an instance of the `MyDurableObject`
52-
// class named "foo". Requests from all Workers to the instance named
53-
// "foo" will go to a single globally unique Durable Object instance.
54-
const id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName("foo");
55-
56-
// Create a stub to open a communication channel with the Durable
57-
// Object instance.
58-
const stub = env.MY_DURABLE_OBJECT.get(id);
51+
// Create a stub to open a communication channel with the Durable Object
52+
// instance named "foo".
53+
//
54+
// Requests from all Workers to the Durable Object instance named "foo"
55+
// will go to a single remote Durable Object instance.
56+
const stub = env.MY_DURABLE_OBJECT.getByName("foo");
5957

6058
// Call the `sayHello()` RPC method on the stub to invoke the method on
61-
// the remote Durable Object instance
59+
// the remote Durable Object instance.
6260
const greeting = await stub.sayHello("world");
6361

6462
return new Response(greeting);

0 commit comments

Comments
 (0)