working online D1 tutorial
This commit is contained in:
1 parent
1079a1a481
commit
91623834d6
5 files changed
+35
-21
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
DROP TABLE IF EXISTS Customers;
|
||||||
|
CREATE TABLE IF NOT EXISTS Customers (CustomerId INTEGER PRIMARY KEY, CompanyName TEXT, ContactName TEXT);
|
||||||
|
INSERT INTO Customers (CustomerID, CompanyName, ContactName) VALUES (1, 'Alfreds Futterkiste', 'Maria Anders'), (4, 'Around the Horn', 'Thomas Hardy'), (11, 'Bs Beverages', 'Victoria Ashworth'), (13, 'Bs Beverages', 'Random Name');
|
||||||
@@ -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.
|
* 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
|
* Learn more at https://developers.cloudflare.com/durable-objects
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/** A Durable Object's behavior is defined in an exported Javascript class */
|
/** A Durable Object's behavior is defined in an exported Javascript class */
|
||||||
export class MyDurableObject extends DurableObject {
|
export class MyDurableObject extends DurableObject {
|
||||||
/**
|
/**
|
||||||
@@ -54,12 +53,20 @@ export default {
|
|||||||
//
|
//
|
||||||
// Requests from all Workers to the Durable Object instance named "foo"
|
// Requests from all Workers to the Durable Object instance named "foo"
|
||||||
// will go to a single remote Durable Object instance.
|
// 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
|
// Call the `sayHello()` RPC method on the stub to invoke the method on
|
||||||
// the remote Durable Object instance.
|
// 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>;
|
} satisfies ExportedHandler<Env>;
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
/* Enable importing .json files */
|
/* Enable importing .json files */
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
|
"ignoreDeprecations": "6.0",
|
||||||
|
|
||||||
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
@@ -36,9 +37,6 @@
|
|||||||
|
|
||||||
/* Skip type checking all .d.ts files. */
|
/* Skip type checking all .d.ts files. */
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"types": [
|
"types": ["./worker-configuration.d.ts", "node"]
|
||||||
"./worker-configuration.d.ts",
|
|
||||||
"node"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+2
-1
@@ -1,7 +1,8 @@
|
|||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
// Generated by Wrangler by running `wrangler types` (hash: 03fd955e5460f9d8b4d609f5cc72b4fc)
|
// Generated by Wrangler by running `wrangler types` (hash: ed10d7e0f72eca34d1628dd7a42b0e58)
|
||||||
// Runtime types generated with workerd@1.20260828.1 2026-08-25
|
// Runtime types generated with workerd@1.20260828.1 2026-08-25
|
||||||
interface __BaseEnv_Env {
|
interface __BaseEnv_Env {
|
||||||
|
prod_d1_tutorial: D1Database;
|
||||||
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").MyDurableObject>;
|
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").MyDurableObject>;
|
||||||
}
|
}
|
||||||
declare namespace Cloudflare {
|
declare namespace Cloudflare {
|
||||||
|
|||||||
+16
-11
@@ -9,28 +9,33 @@
|
|||||||
"compatibility_date": "2026-08-25",
|
"compatibility_date": "2026-08-25",
|
||||||
"migrations": [
|
"migrations": [
|
||||||
{
|
{
|
||||||
"new_sqlite_classes": [
|
"new_sqlite_classes": ["MyDurableObject"],
|
||||||
"MyDurableObject"
|
"tag": "v1",
|
||||||
],
|
},
|
||||||
"tag": "v1"
|
|
||||||
}
|
|
||||||
],
|
],
|
||||||
"assets": {
|
"assets": {
|
||||||
// The path to the directory containing the `index.html` file to be served at `/`
|
// The path to the directory containing the `index.html` file to be served at `/`
|
||||||
"directory": "./public"
|
"directory": "./public",
|
||||||
},
|
},
|
||||||
"durable_objects": {
|
"durable_objects": {
|
||||||
"bindings": [
|
"bindings": [
|
||||||
{
|
{
|
||||||
"class_name": "MyDurableObject",
|
"class_name": "MyDurableObject",
|
||||||
"name": "MY_DURABLE_OBJECT"
|
"name": "MY_DURABLE_OBJECT",
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
},
|
},
|
||||||
"observability": {
|
"observability": {
|
||||||
"enabled": true
|
"enabled": true,
|
||||||
},
|
},
|
||||||
"upload_source_maps": true
|
"upload_source_maps": true,
|
||||||
|
"d1_databases": [
|
||||||
|
{
|
||||||
|
"binding": "prod_d1_tutorial",
|
||||||
|
"database_name": "prod-d1-tutorial",
|
||||||
|
"database_id": "1e595d69-ec95-4eef-a2ef-57ac6ada1973",
|
||||||
|
},
|
||||||
|
],
|
||||||
/**
|
/**
|
||||||
* Smart Placement
|
* Smart Placement
|
||||||
* https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
|
* https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
|
||||||
|
|||||||
Reference in new issue
Block a user