1import * as Drizzle from "@/Drizzle/Postgres.ts";
2import { ConnectPostgres } from "@/Railway/ConnectPostgres.ts";
3import { ConnectPostgresHttp } from "@/Railway/ConnectPostgresHttp.ts";
4import { Function } from "@/Railway/Function.ts";
5import * as Effect from "effect/Effect";
6import * as HttpServerResponse from "effect/http/HttpServerResponse";
7import { Partition, Site } from "./suite-env.ts";
8import { Db } from "./postgres-shared.ts";
9
10export { Db, Site };
11
12/**
13 * Effect-native canvas Function that binds {@link Db} via
14 * {@link ConnectPostgres} and answers SELECT 1. Overflows Railway's 96KB
15 * encoded start command (`FunctionTooLarge`) — live HTTP coverage uses
16 * the async fixture in `async-postgres-fn.ts` instead.
17 */
18export default class PostgresFn extends Function<PostgresFn>()(
19 "PostgresFn",
20 {
21 project: Site,
22 environment: Partition,
23 main: import.meta.url,
24 build: { install: ["pg", "drizzle-orm"] },
25 },
26 Effect.gen(function* () {
27 const conn = yield* ConnectPostgres(Db);
28 const db = yield* Drizzle.Postgres(conn.connectionString);
29 return {
30 fetch: db.execute("select 1 as ok", "objects").pipe(
31 Effect.flatMap((rows) => HttpServerResponse.json({ rows })),
32 Effect.catch((error) =>
33 HttpServerResponse.json(
34 { ok: false, error: String(error) },
35 { status: 500 },
36 ),
37 ),
38 ),
39 };
40 }).pipe(Effect.provide(ConnectPostgresHttp)),
41) {}