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

main
ts 47 lines 1.62 KB
Raw
1 import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1";
2 import * as Output from "alchemy/Output";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as GCPBindings from "../GCPBindings.ts";
6 import { grantFor } from "../HttpBinding.ts";
7 import { ExecuteSql, type ExecuteSqlRequest } from "./ExecuteSql.ts";
8 import type { Instance } from "./Instance.ts";
9
10 /**
11 * HTTP implementation of {@link ExecuteSql}.
12 *
13 * @layer
14 * @provides GCP.SQL.ExecuteSql
15 */
16 export const ExecuteSqlHttp = Layer.effect(
17 ExecuteSql,
18 Effect.gen(function* () {
19 const bind = yield* GCPBindings.Bind;
20 const execute = yield* GCPBindings.withCredentials(sqladmin.executeSqlInstances);
21 return Effect.fn(function* (instance: Instance) {
22 if (!globalThis.__ALCHEMY_RUNTIME__) {
23 yield* bind`GCP.SQL.ExecuteSql(${instance})`({
24 // Cloud SQL has no instance-level IAM policy; an IAM Condition
25 // scopes the project grant to this instance.
26 iam: [
27 grantFor(
28 { role: "roles/cloudsql.instanceUser", scopeByCondition: true },
29 Output.interpolate`projects/${instance.project}/instances/${instance.instanceName}`,
30 ),
31 ],
32 });
33 }
34 const instanceName = yield* instance.instanceName;
35 const project = yield* instance.project;
36 return Effect.fn(`GCP.SQL.ExecuteSql(${instance.LogicalId})`)(function* (
37 request: ExecuteSqlRequest,
38 ) {
39 return yield* execute({
40 ...request,
41 instance: yield* instanceName,
42 project: yield* project,
43 });
44 });
45 });
46 }),
47 );