@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 116 lines 3.93 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import type { RuntimeContext } from "alchemy";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import type * as Redacted from "effect/Redacted";
6 import { Database } from "./Database.ts";
7 import { makeMySQLService, type MySQLOptions } from "./MySQL.ts";
8 import { makePostgresService, type MigrateSource, type PostgresOptions } from "./Postgres.ts";
9
10 export interface CloudflareHyperdriveOptions {
11 /**
12 * Origin dialect behind the Hyperdrive connection.
13 * @default "postgres"
14 */
15 readonly dialect?: "postgres" | "mysql";
16 /**
17 * Deploy-resolvable ORIGIN connection source for schema migrations.
18 * Hyperdrive's own connection string is minted per-invocation inside the
19 * Worker and cannot be used at deploy time — pass the origin database URL
20 * (e.g. Neon `branch.connectionUri`) or leave unset to skip migrations.
21 */
22 readonly migrate?: MigrateSource;
23 readonly pool?: PostgresOptions["pool"] | MySQLOptions["pool"];
24 }
25
26 /**
27 * Cloudflare Hyperdrive layer for Better Auth — pooled TCP Postgres (or
28 * MySQL) through a Hyperdrive connection.
29 *
30 * Hyperdrive's connection string is minted per-invocation inside the
31 * Worker and cannot be used at deploy time, so pass the ORIGIN database
32 * URL as `migrate` for deploy-time schema migrations (or omit it to skip
33 * them).
34 *
35 *
36 * ### Pooled Postgres through Hyperdrive
37 * The Worker connects over TCP through Hyperdrive's pooler; auth reads
38 * should disable Hyperdrive caching so sessions are never stale.
39 * **Example:** Hyperdrive over a Neon origin
40 * ```typescript
41 * import { BetterAuth } from "@alchemy.run/better-auth";
42 * import { CloudflareHyperdrive } from "@alchemy.run/better-auth/CloudflareHyperdrive";
43 *
44 * export const PgProject = Neon.Project("AuthDb");
45 * export const Hyperdrive = Effect.gen(function* () {
46 * const project = yield* PgProject;
47 * return yield* Cloudflare.Hyperdrive.Connection("AuthHd", {
48 * origin: project.origin,
49 * caching: { disabled: true },
50 * });
51 * });
52 *
53 * Effect.gen(function* () {
54 * const auth = yield* BetterAuth({ emailAndPassword: { enabled: true } });
55 * return { fetch: ... };
56 * }).pipe(
57 * Effect.provide(
58 * Layer.unwrap(
59 * Effect.gen(function* () {
60 * const project = yield* PgProject;
61 * const connection = yield* Hyperdrive;
62 * return CloudflareHyperdrive(connection, {
63 * migrate: project.connectionUri,
64 * });
65 * }),
66 * ),
67 * ),
68 * )
69 * ```
70 *
71 * ### MySQL origins
72 * Hyperdrive also fronts MySQL origins — select the dialect and the layer
73 * drives `mysql2` instead of `pg`.
74 * **Example:** MySQL behind Hyperdrive
75 * ```typescript
76 * CloudflareHyperdrive(connection, {
77 * dialect: "mysql",
78 * migrate: password.connectionUrl,
79 * })
80 * ```
81 *
82 * @layer
83 * @provides BetterAuth.Database
84 * @peer pg
85 * @peer mysql2
86 * @product Hyperdrive
87 */
88 export const CloudflareHyperdrive = (
89 connection:
90 | Cloudflare.Hyperdrive.Connection
91 | Effect.Effect<Cloudflare.Hyperdrive.Connection, never, any>,
92 options?: CloudflareHyperdriveOptions,
93 ) =>
94 Layer.effect(
95 Database,
96 Effect.gen(function* () {
97 const conn = Effect.isEffect(connection)
98 ? yield* connection as Effect.Effect<Cloudflare.Hyperdrive.Connection>
99 : connection;
100 const client = yield* Cloudflare.Hyperdrive.Connect(conn);
101 const source = client.connectionString as Effect.Effect<
102 Redacted.Redacted<string>,
103 never,
104 RuntimeContext
105 >;
106 return options?.dialect === "mysql"
107 ? yield* makeMySQLService(source, {
108 migrate: options?.migrate ?? false,
109 pool: options?.pool as MySQLOptions["pool"],
110 })
111 : yield* makePostgresService(source, {
112 migrate: options?.migrate ?? false,
113 pool: options?.pool as PostgresOptions["pool"],
114 });
115 }),
116 ).pipe(Layer.provide(Cloudflare.Hyperdrive.ConnectBinding));