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
| 1 | import * as glue from "@distilled.cloud/aws/glue"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { AWSEnvironment } from "../Environment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | cleanMap, |
| 14 | databaseArn, |
| 15 | deleteGlueDatabase, |
| 16 | retryWhileConcurrentModification, |
| 17 | } from "./internal.ts"; |
| 18 | |
| 19 | export interface DatabaseProps { |
| 20 | /** |
| 21 | * Name of the database. Glue lowercases database names, so provide a |
| 22 | * lowercase name. If omitted, a unique lowercase name is generated. |
| 23 | * Changing the name replaces the database. |
| 24 | * @default a generated lowercase physical name |
| 25 | */ |
| 26 | databaseName?: string; |
| 27 | /** |
| 28 | * A description of the database. |
| 29 | */ |
| 30 | description?: string; |
| 31 | /** |
| 32 | * The location of the database (for example, an S3 path) used as the |
| 33 | * default for tables that do not specify their own location. |
| 34 | */ |
| 35 | locationUri?: string; |
| 36 | /** |
| 37 | * Free-form key/value properties stored on the database. Alchemy adds its |
| 38 | * own `alchemy::*` ownership markers to this map (Glue databases are not |
| 39 | * ARN-taggable) — user keys are preserved alongside them. |
| 40 | */ |
| 41 | parameters?: Record<string, string>; |
| 42 | /** |
| 43 | * The AWS account ID of the Data Catalog the database lives in. Changing it |
| 44 | * replaces the database. |
| 45 | * @default the caller's account (the default Data Catalog) |
| 46 | */ |
| 47 | catalogId?: string; |
| 48 | } |
| 49 | |
| 50 | export interface Database extends Resource< |
| 51 | "AWS.Glue.Database", |
| 52 | DatabaseProps, |
| 53 | { |
| 54 | /** The (lowercase) name of the database. */ |
| 55 | databaseName: string; |
| 56 | /** The ARN of the database. */ |
| 57 | databaseArn: string; |
| 58 | /** The AWS account ID of the Data Catalog the database lives in. */ |
| 59 | catalogId: string; |
| 60 | /** |
| 61 | * Description, location and parameters (including the ownership |
| 62 | * markers), as observed. Lets `alchemy drift` detect out-of-band |
| 63 | * changes. |
| 64 | */ |
| 65 | configuration?: { |
| 66 | description?: string; |
| 67 | locationUri?: string; |
| 68 | parameters: Record<string, string>; |
| 69 | }; |
| 70 | }, |
| 71 | {}, |
| 72 | Providers |
| 73 | > {} |
| 74 | |
| 75 | /** |
| 76 | * An AWS Glue Data Catalog database — the top-level container for Glue tables |
| 77 | * that Athena, EMR, Redshift Spectrum, and Glue jobs query. Databases are free |
| 78 | * and instant to create. |
| 79 | * ### Creating Databases |
| 80 | * **Example:** Basic Database |
| 81 | * ```typescript |
| 82 | * import * as AWS from "@alchemy.run/aws"; |
| 83 | * |
| 84 | * const database = yield* AWS.Glue.Database("Analytics", { |
| 85 | * databaseName: "analytics", |
| 86 | * }); |
| 87 | * ``` |
| 88 | * |
| 89 | * **Example:** Database with a Default S3 Location |
| 90 | * ```typescript |
| 91 | * const database = yield* AWS.Glue.Database("Analytics", { |
| 92 | * databaseName: "analytics", |
| 93 | * description: "Curated analytics tables", |
| 94 | * locationUri: "s3://my-data-lake/analytics/", |
| 95 | * parameters: { classification: "parquet" }, |
| 96 | * }); |
| 97 | * ``` |
| 98 | * |
| 99 | * @resource |
| 100 | */ |
| 101 | export const Database = Resource<Database>("AWS.Glue.Database"); |
| 102 | |
| 103 | /** |
| 104 | * The settings `read` and `reconcile` both report, so a converged database |
| 105 | * reads unchanged. |
| 106 | */ |
| 107 | const observeDatabaseConfiguration = (db: glue.Database) => ({ |
| 108 | description: db.Description || undefined, |
| 109 | locationUri: db.LocationUri || undefined, |
| 110 | parameters: cleanMap(db.Parameters), |
| 111 | }); |
| 112 | |
| 113 | export const DatabaseProvider = () => |
| 114 | Provider.effect( |
| 115 | Database, |
| 116 | Effect.gen(function* () { |
| 117 | const createName = Effect.fn(function* ( |
| 118 | id: string, |
| 119 | props: { databaseName?: string | undefined }, |
| 120 | ) { |
| 121 | return ( |
| 122 | props.databaseName ?? (yield* createPhysicalName({ id, maxLength: 255, lowercase: true })) |
| 123 | ); |
| 124 | }); |
| 125 | |
| 126 | const observe = Effect.fn(function* (name: string, catalogId: string | undefined) { |
| 127 | return yield* glue.getDatabase({ Name: name, CatalogId: catalogId }).pipe( |
| 128 | Effect.map((r) => r.Database), |
| 129 | Effect.catchTag("EntityNotFoundException", () => Effect.undefined), |
| 130 | ); |
| 131 | }); |
| 132 | |
| 133 | return Database.Provider.of({ |
| 134 | stables: ["databaseName", "databaseArn", "catalogId"], |
| 135 | |
| 136 | list: () => |
| 137 | Effect.gen(function* () { |
| 138 | const { accountId, region } = yield* AWSEnvironment.current; |
| 139 | const pages = yield* glue.getDatabases.pages({}).pipe(Stream.runCollect); |
| 140 | return Array.from(pages) |
| 141 | .flatMap((page) => page.DatabaseList ?? []) |
| 142 | .map((db) => ({ |
| 143 | databaseName: db.Name, |
| 144 | databaseArn: databaseArn(region, db.CatalogId ?? accountId, db.Name), |
| 145 | catalogId: db.CatalogId ?? accountId, |
| 146 | })); |
| 147 | }), |
| 148 | |
| 149 | read: Effect.fn(function* ({ id, olds, output }) { |
| 150 | const { accountId, region } = yield* AWSEnvironment.current; |
| 151 | const catalogId = output?.catalogId ?? olds?.catalogId ?? accountId; |
| 152 | const name = output?.databaseName ?? (yield* createName(id, olds ?? {})); |
| 153 | const db = yield* observe(name, catalogId); |
| 154 | if (db === undefined) return undefined; |
| 155 | const attrs = { |
| 156 | databaseName: db.Name, |
| 157 | databaseArn: databaseArn(region, db.CatalogId ?? catalogId, db.Name), |
| 158 | catalogId: db.CatalogId ?? catalogId, |
| 159 | configuration: observeDatabaseConfiguration(db), |
| 160 | }; |
| 161 | // Glue databases are not ARN-taggable — ownership markers live in |
| 162 | // the database Parameters map. |
| 163 | return (yield* hasAlchemyTags(id, cleanMap(db.Parameters))) ? attrs : Unowned(attrs); |
| 164 | }), |
| 165 | |
| 166 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 167 | if (!isResolved(news)) return undefined; |
| 168 | const oldName = yield* createName(id, olds); |
| 169 | const newName = yield* createName(id, news); |
| 170 | if (oldName !== newName) return { action: "replace" } as const; |
| 171 | if ((olds.catalogId ?? undefined) !== (news.catalogId ?? undefined)) { |
| 172 | return { action: "replace" } as const; |
| 173 | } |
| 174 | // description / locationUri / parameters → update |
| 175 | }), |
| 176 | |
| 177 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 178 | const { accountId, region } = yield* AWSEnvironment.current; |
| 179 | const catalogId = news.catalogId ?? output?.catalogId ?? accountId; |
| 180 | const name = output?.databaseName ?? (yield* createName(id, news)); |
| 181 | const internalTags = yield* createInternalTags(id); |
| 182 | const desiredParameters = { ...news.parameters, ...internalTags }; |
| 183 | |
| 184 | const databaseInput = { |
| 185 | Name: name, |
| 186 | Description: news.description, |
| 187 | LocationUri: news.locationUri, |
| 188 | Parameters: desiredParameters, |
| 189 | }; |
| 190 | |
| 191 | // 1. OBSERVE |
| 192 | let db = yield* observe(name, catalogId); |
| 193 | |
| 194 | // 2. ENSURE |
| 195 | if (db === undefined) { |
| 196 | yield* glue |
| 197 | .createDatabase({ |
| 198 | CatalogId: catalogId, |
| 199 | DatabaseInput: databaseInput, |
| 200 | }) |
| 201 | .pipe( |
| 202 | Effect.catchTag("AlreadyExistsException", () => Effect.void), |
| 203 | retryWhileConcurrentModification, |
| 204 | ); |
| 205 | db = yield* observe(name, catalogId); |
| 206 | } else { |
| 207 | // 3. SYNC — UpdateDatabase replaces the full DatabaseInput. |
| 208 | yield* glue |
| 209 | .updateDatabase({ |
| 210 | CatalogId: catalogId, |
| 211 | Name: name, |
| 212 | DatabaseInput: databaseInput, |
| 213 | }) |
| 214 | .pipe(retryWhileConcurrentModification); |
| 215 | db = yield* observe(name, catalogId); |
| 216 | } |
| 217 | |
| 218 | yield* session.note(name); |
| 219 | return { |
| 220 | databaseName: name, |
| 221 | databaseArn: databaseArn(region, db?.CatalogId ?? catalogId, name), |
| 222 | catalogId: db?.CatalogId ?? catalogId, |
| 223 | configuration: db && observeDatabaseConfiguration(db), |
| 224 | }; |
| 225 | }), |
| 226 | |
| 227 | delete: Effect.fn(function* ({ output }) { |
| 228 | yield* deleteGlueDatabase(output.databaseName, output.catalogId); |
| 229 | }), |
| 230 | }); |
| 231 | }), |
| 232 | ); |