working online D1 tutorial
This commit is contained in:
1 parent
1079a1a481
commit
91623834d6
5 files changed
+50
-36
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { DurableObject } from "cloudflare:workers";
|
||||
import { DurableObject } from 'cloudflare:workers';
|
||||
|
||||
/**
|
||||
* Welcome to Cloudflare Workers! This is your first Durable Objects application.
|
||||
@@ -13,7 +13,6 @@ import { DurableObject } from "cloudflare:workers";
|
||||
* Learn more at https://developers.cloudflare.com/durable-objects
|
||||
*/
|
||||
|
||||
|
||||
/** A Durable Object's behavior is defined in an exported Javascript class */
|
||||
export class MyDurableObject extends DurableObject {
|
||||
/**
|
||||
@@ -54,12 +53,20 @@ export default {
|
||||
//
|
||||
// Requests from all Workers to the Durable Object instance named "foo"
|
||||
// will go to a single remote Durable Object instance.
|
||||
const stub = env.MY_DURABLE_OBJECT.getByName("foo");
|
||||
const stub = env.MY_DURABLE_OBJECT.getByName('foo');
|
||||
|
||||
// Call the `sayHello()` RPC method on the stub to invoke the method on
|
||||
// the remote Durable Object instance.
|
||||
const greeting = await stub.sayHello("world");
|
||||
// const greeting = await stub.sayHello("world");
|
||||
|
||||
return new Response(greeting);
|
||||
const { pathname } = new URL(request.url);
|
||||
|
||||
if (pathname === '/api/beverages') {
|
||||
// If you did not use `DB` as your binding name, change it here
|
||||
const { results } = await env.prod_d1_tutorial.prepare('SELECT * FROM Customers WHERE CompanyName = ?').bind('Bs Beverages').run();
|
||||
return Response.json(results);
|
||||
}
|
||||
|
||||
return new Response('Call /api/beverages to see everyone who works at Bs Beverages');
|
||||
},
|
||||
} satisfies ExportedHandler<Env>;
|
||||
Reference in new issue
Block a user