@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 58 lines 1.93 KB
Raw
1 import {
2 runMigrations,
3 withPgClient as withClient,
4 withPgExecutor,
5 type NormalizedMigrationsInput,
6 type StampedMigrationsState,
7 } from "@alchemy.run/sql/Migrations";
8 import { stripSslQueryParams } from "@alchemy.run/sql/PostgresDriver";
9 import { applySqlImports } from "@alchemy.run/sql/SqlFile";
10 import * as Effect from "effect/Effect";
11 import * as Redacted from "effect/Redacted";
12 import type { Client, ClientConfig } from "pg";
13
14 /**
15 * Neon's URIs carry `sslmode=require`; TLS is set on the client instead,
16 * without verifying Neon's certificate chain.
17 */
18 const clientConfig = (connectionUri: Redacted.Redacted<string>): ClientConfig => ({
19 connectionString: connectionUri.pipe(Redacted.value, stripSslQueryParams),
20 ssl: { rejectUnauthorized: false },
21 });
22
23 /** A `pg` client on a Neon connection URI for the scope of `use`. */
24 export const withPgClient = <A, E, R>(
25 connectionUri: Redacted.Redacted<string>,
26 use: (client: Client) => Effect.Effect<A, E, R>,
27 ) => withClient(clientConfig(connectionUri), use);
28
29 /** Run one SQL script (several statements are fine). */
30 export const runSql = (connectionUri: Redacted.Redacted<string>, sql: string) =>
31 withPgExecutor(clientConfig(connectionUri), (executor) => executor.query(sql)).pipe(
32 Effect.asVoid,
33 );
34
35 export const runPgMigrations = (options: {
36 connectionUri: Redacted.Redacted<string>;
37 input: NormalizedMigrationsInput;
38 stamped: StampedMigrationsState;
39 }) =>
40 runMigrations({
41 input: options.input,
42 stamped: options.stamped,
43 withExecutor: (apply) => withPgExecutor(clientConfig(options.connectionUri), apply),
44 });
45
46 /** Apply `importFiles`, each once and again only when it changes. */
47 export const runImports = (
48 connectionUri: Redacted.Redacted<string>,
49 importFiles: ReadonlyArray<string>,
50 rootDir: string,
51 previous: Record<string, string>,
52 ) =>
53 applySqlImports({
54 importFiles,
55 rootDir,
56 previous,
57 apply: (file) => runSql(connectionUri, file.sql),
58 });