Skip to content
TypeScript
2.9 KB
Raw Blame
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Drizzle from "@alchemy.run/drizzle/Postgres";
import { eq } from "drizzle-orm";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import * as Layer from "effect/Layer";
import { relations, Widgets } from "./schema.ts";
import { Hyperdrive } from "./Stack.ts";

/**
 * Worker fixture that binds a Cloudflare Hyperdrive (pointed at a
 * Planetscale Postgres role) and exercises Drizzle's Effect-native
 * client. Mirrors the cloudflare-planetscale-drizzle example but lives
 * inside the package so the runtime path is covered by an integration
 * test next to the resource it exercises.
 */
export default class HyperdriveWorker extends Cloudflare.Worker<HyperdriveWorker>()(
  "PlanetscaleHyperdriveWorker",
  {
    main: import.meta.url,
  },
  Effect.gen(function* () {
    const conn = yield* Cloudflare.Hyperdrive.Connect(Hyperdrive);
    const db = yield* Drizzle.Postgres(conn.connectionString, { relations });

    return {
      fetch: Effect.gen(function* () {
        const request = yield* HttpServerRequest;
        const url = new URL(request.url, "http://x");

        if (request.method === "GET" && url.pathname === "/hyperdrive") {
          return yield* HttpServerResponse.json({
            host: yield* conn.host,
            port: yield* conn.port,
            user: yield* conn.user,
            database: yield* conn.database,
          });
        }

        if (request.method === "GET" && url.pathname === "/widgets") {
          const widgets = yield* db.select().from(Widgets);
          return yield* HttpServerResponse.json({ widgets });
        }

        if (request.method === "POST" && url.pathname === "/widgets") {
          const body = (yield* request.json) as { id: number; name: string };
          const [inserted] = yield* db
            .insert(Widgets)
            .values({ id: body.id, name: body.name })
            .onConflictDoUpdate({
              target: Widgets.id,
              set: { name: body.name },
            })
            .returning();
          return yield* HttpServerResponse.json({ widget: inserted });
        }

        const idMatch = url.pathname.match(/^\/widgets\/(\d+)$/);
        if (request.method === "DELETE" && idMatch) {
          const id = Number(idMatch[1]);
          const [deleted] = yield* db.delete(Widgets).where(eq(Widgets.id, id)).returning();
          return yield* HttpServerResponse.json({ widget: deleted ?? null });
        }

        return HttpServerResponse.text("Not Found", { status: 404 });
      }).pipe(
        Effect.catch((cause: any) =>
          HttpServerResponse.json({ ok: false, error: String(cause) }, { status: 500 }),
        ),
      ),
    };
  }).pipe(Effect.provide(Layer.mergeAll(Cloudflare.Hyperdrive.ConnectBinding))),
) {}