1import * as Cloudflare from "@/Cloudflare";
2import * as Alchemy from "@/index";
3import * as Effect from "effect/Effect";
4import * as pathe from "pathe";
5import { TestDatabase } from "./database.ts";
6import D1EffectWorker from "./effect-worker.ts";
7
8/**
9 * Async (non-Effect) Worker bound to the shared {@link TestDatabase} via its
10 * `env`. `InferEnv` maps the `Cloudflare.D1.Database` marker to the native
11 * `cf.D1Database`, so `env.DB` is the runtime binding inside `async-worker.ts`.
12 */
13export const AsyncWorker = Cloudflare.Worker("D1AsyncWorker", {
14 main: pathe.resolve(import.meta.dirname, "async-worker.ts"),
15 env: {
16 DB: TestDatabase,
17 },
18});
19
20export type AsyncWorkerEnv = Cloudflare.InferEnv<typeof AsyncWorker>;
21
22/**
23 * One stack deploying both invocation styles against ONE shared D1 database.
24 */
25export default Alchemy.Stack(
26 "D1BindingStack",
27 {
28 providers: Cloudflare.providers(),
29 state: Cloudflare.state(),
30 },
31 Effect.gen(function* () {
32 const effectWorker = yield* D1EffectWorker;
33 const asyncWorker = yield* AsyncWorker;
34 return {
35 effectWorkerUrl: effectWorker.url.as<string>(),
36 asyncWorkerUrl: asyncWorker.url.as<string>(),
37 };
38 }),
39);