Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to add durable object - not working #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Runtime = import("@astrojs/cloudflare").Runtime<Env>;

declare namespace App {
interface Locals extends Runtime {}
}
38 changes: 38 additions & 0 deletions src/pages/do/[name].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
import Layout from '~/layouts/Layout.astro';
import { DurableObject } from "cloudflare:workers";

// Durable Object boilerplate from:
// https://developers.cloudflare.com/durable-objects/get-started/tutorial-with-sql-api/

const { env } = Astro.locals.runtime;
console.log(env);

export class MyDurableObject extends DurableObject {
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env)
}

async sayHello() {
let result = this.ctx.storage.sql
.exec("SELECT 'Hello' as greeting")
.one();
return result.greeting;
}
}

// entry point for the worker fetch handler on this route
const { name } = Astro.params;
if (name === undefined) return Astro.redirect('/404');

// let id = env.MY_DURABLE_OBJECT.idFromName(name);
// let stub = env.MY_DURABLE_OBJECT.get(id);
// let greeting = await stub.sayHello();
---

<Layout>
<main class="prose mx-auto p-2">
<h3>{'greeting'} {name}</h3>
<a href="/">&lt;&lt; Back to home page</a>
</main>
</Layout>
4 changes: 4 additions & 0 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const posts = await getCollection('markdown');
<Layout>
<AstroLogo />
<main class="prose mx-auto p-2">
<h3>Durable object</h3>
<ul class="">
<li><a href="/do/papa">/do/papa</a></li>
</ul>
<h3>Markdown</h3>
<ul class="">
{
Expand Down
5 changes: 5 additions & 0 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Generated by Wrangler by running `wrangler types`

interface Env {
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/pages/do/[name].astro").MyDurableObject>;
}
8 changes: 8 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ compatibility_flags = ["nodejs_compat"]
main = "./dist/_worker.js/index.js"
assets = { directory = "./dist", binding = "ASSETS" }

[[durable_objects.bindings]]
name = "MY_DURABLE_OBJECT"
class_name = "MyDurableObject"

[[migrations]]
tag = "v1" # Should be unique for each entry
new_sqlite_classes = ["MyDurableObject"] # Array of new classes

# Workers Logs
# Docs: https://developers.cloudflare.com/workers/observability/logs/workers-logs/
# Configuration: https://developers.cloudflare.com/workers/observability/logs/workers-logs/#enable-workers-logs
Expand Down