From 91623834d68301d56a2740746cd824ed5fb650a9 Mon Sep 17 00:00:00 2001 From: polv Date: Sun, 30 Aug 2026 19:15:59 +0700 Subject: [PATCH] working online D1 tutorial --- d1-tutorial/schema.sql | 3 +++ d1-tutorial/src/index.ts | 17 ++++++++++---- d1-tutorial/tsconfig.json | 34 +++++++++++++-------------- d1-tutorial/worker-configuration.d.ts | 3 ++- d1-tutorial/wrangler.jsonc | 29 +++++++++++++---------- 5 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 d1-tutorial/schema.sql diff --git a/d1-tutorial/schema.sql b/d1-tutorial/schema.sql new file mode 100644 index 0000000..93eb9bf --- /dev/null +++ b/d1-tutorial/schema.sql @@ -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'); \ No newline at end of file diff --git a/d1-tutorial/src/index.ts b/d1-tutorial/src/index.ts index 82deb04..8b85311 100644 --- a/d1-tutorial/src/index.ts +++ b/d1-tutorial/src/index.ts @@ -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; diff --git a/d1-tutorial/tsconfig.json b/d1-tutorial/tsconfig.json index bee8652..905d979 100644 --- a/d1-tutorial/tsconfig.json +++ b/d1-tutorial/tsconfig.json @@ -2,43 +2,41 @@ "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ - /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ "target": "es2024", - /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + /* Specify a set of bundled library declaration files that describe the target runtime environment. */ "lib": ["es2024"], - /* Specify what JSX code is generated. */ + /* Specify what JSX code is generated. */ "jsx": "react-jsx", - /* Specify what module code is generated. */ + /* Specify what module code is generated. */ "module": "es2022", - /* Specify how TypeScript looks up a file from a given module specifier. */ + /* Specify how TypeScript looks up a file from a given module specifier. */ "moduleResolution": "node", - /* Enable importing .json files */ + /* Enable importing .json files */ "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, - /* Enable error reporting in type-checked JavaScript files. */ + /* Enable error reporting in type-checked JavaScript files. */ "checkJs": false, - /* Disable emitting files from a compilation. */ + /* Disable emitting files from a compilation. */ "noEmit": true, - /* Ensure that each file can be safely transpiled without relying on other imports. */ + /* Ensure that each file can be safely transpiled without relying on other imports. */ "isolatedModules": true, - /* Allow 'import x from y' when a module doesn't have a default export. */ + /* Allow 'import x from y' when a module doesn't have a default export. */ "allowSyntheticDefaultImports": true, - /* Ensure that casing is correct in imports. */ + /* Ensure that casing is correct in imports. */ "forceConsistentCasingInFileNames": true, - /* Enable all strict type-checking options. */ + /* Enable all strict type-checking options. */ "strict": true, - /* Skip type checking all .d.ts files. */ + /* Skip type checking all .d.ts files. */ "skipLibCheck": true, - "types": [ - "./worker-configuration.d.ts", - "node" - ] + "types": ["./worker-configuration.d.ts", "node"] } } diff --git a/d1-tutorial/worker-configuration.d.ts b/d1-tutorial/worker-configuration.d.ts index 0d09517..c919796 100644 --- a/d1-tutorial/worker-configuration.d.ts +++ b/d1-tutorial/worker-configuration.d.ts @@ -1,7 +1,8 @@ /* 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 interface __BaseEnv_Env { + prod_d1_tutorial: D1Database; MY_DURABLE_OBJECT: DurableObjectNamespace; } declare namespace Cloudflare { diff --git a/d1-tutorial/wrangler.jsonc b/d1-tutorial/wrangler.jsonc index cad6e20..5a13304 100644 --- a/d1-tutorial/wrangler.jsonc +++ b/d1-tutorial/wrangler.jsonc @@ -9,28 +9,33 @@ "compatibility_date": "2026-08-25", "migrations": [ { - "new_sqlite_classes": [ - "MyDurableObject" - ], - "tag": "v1" - } + "new_sqlite_classes": ["MyDurableObject"], + "tag": "v1", + }, ], "assets": { // The path to the directory containing the `index.html` file to be served at `/` - "directory": "./public" + "directory": "./public", }, "durable_objects": { "bindings": [ { "class_name": "MyDurableObject", - "name": "MY_DURABLE_OBJECT" - } - ] + "name": "MY_DURABLE_OBJECT", + }, + ], }, "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 * https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement @@ -54,4 +59,4 @@ * https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings */ // "services": [ { "binding": "MY_SERVICE", "service": "my-service" } ] -} \ No newline at end of file +}