working online D1 tutorial
This commit is contained in:
1 parent
1079a1a481
commit
91623834d6
5 files changed
+50
-36
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.
|
||||
@@ -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>;
|
||||
+16
-18
@@ -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"]
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -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<import("./src/index").MyDurableObject>;
|
||||
}
|
||||
declare namespace Cloudflare {
|
||||
|
||||
+17
-12
@@ -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" } ]
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user