sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Prisma/ fixtures/ read-compute.ts

import { Compute } from "@alchemy.run/prisma/Compute";
import { ReadBucket, ReadBucketBinding } from "@alchemy.run/prisma/ReadBucket";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { TestBucket, TestProject } from "./bucket.ts";
import { readRoutes } from "./read-routes.ts";

/** Read-only access via the native Compute binding (`ReadBucketBinding`). */
export default Compute(
  "PrismaReadBucketCompute",
  Effect.gen(function* () {
    const project = yield* TestProject;
    return {
      project,
      appName: "alchemy-bucket-binding-read",
      main: import.meta.filename,
      port: 8080,
      timeoutSeconds: 240,
      destroyOldDeployment: true,
    };
  }),
  Effect.gen(function* () {
    const bucket = yield* TestBucket;
    const store = yield* ReadBucket(bucket);
    return {
      fetch: Effect.gen(function* () {
        const request = yield* HttpServerRequest;
        const url = new URL(request.url, "http://x");
        const handled = yield* readRoutes(store, url);
        return handled ?? HttpServerResponse.text("Not Found", { status: 404 });
      }),
    };
  }).pipe(Effect.provide(ReadBucketBinding)),
);