@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, 04:45 UTC

main
ts 255 lines 8.51 KB
Raw
1 import * as dataexchange from "@distilled.cloud/aws/dataexchange";
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 Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import type { Providers } from "../Providers.ts";
12 import { readDataExchangeTags, syncDataExchangeTags } from "./internal.ts";
13
14 /**
15 * The type of assets a data set can hold. Immutable — changing it replaces
16 * the data set.
17 */
18 export type DataSetAssetType =
19 | "S3_SNAPSHOT"
20 | "REDSHIFT_DATA_SHARE"
21 | "API_GATEWAY_API"
22 | "S3_DATA_ACCESS"
23 | "LAKE_FORMATION_DATA_PERMISSION";
24
25 export interface DataSetProps {
26 /**
27 * Name of the data set. If omitted, a unique name is generated from the
28 * app, stage, and logical ID. The name is mutable — changing it updates the
29 * data set in place.
30 */
31 name?: string;
32 /**
33 * The type of assets the data set holds. `S3_SNAPSHOT` (static files
34 * snapshotted from S3) is the most common.
35 * Immutable — changing it replaces the data set.
36 * @default "S3_SNAPSHOT"
37 */
38 assetType?: DataSetAssetType;
39 /**
40 * A description of the data set, shown to subscribers on AWS Data Exchange.
41 * @default the data set name
42 */
43 description?: string;
44 /**
45 * Tags to apply to the data set. Merged with internal Alchemy tags.
46 */
47 tags?: Record<string, string>;
48 }
49
50 export interface DataSet extends Resource<
51 "AWS.DataExchange.DataSet",
52 DataSetProps,
53 {
54 /**
55 * The unique identifier of the data set.
56 */
57 dataSetId: string;
58 /**
59 * The ARN of the data set.
60 */
61 dataSetArn: string;
62 /**
63 * The name of the data set.
64 */
65 name: string;
66 /**
67 * The type of assets the data set holds.
68 */
69 assetType: string;
70 /**
71 * The origin of the data set — `OWNED` for data sets created by this
72 * account, `ENTITLED` for data sets obtained through a subscription.
73 */
74 origin: string;
75 },
76 never,
77 Providers
78 > {}
79
80 /**
81 * An AWS Data Exchange data set — the top-level container that data providers
82 * publish revisions of data into. An owned data set holds revisions, each of
83 * which holds assets (e.g. S3 snapshot files) that subscribers receive.
84 *
85 * ### Creating Data Sets
86 * **Example:** Basic S3-snapshot data set
87 * ```typescript
88 * import * as AWS from "@alchemy.run/aws";
89 *
90 * const dataSet = yield* AWS.DataExchange.DataSet("Prices", {
91 * description: "Daily commodity price snapshots",
92 * });
93 * ```
94 *
95 * **Example:** Named data set with tags
96 * ```typescript
97 * const dataSet = yield* AWS.DataExchange.DataSet("Prices", {
98 * name: "commodity-prices",
99 * assetType: "S3_SNAPSHOT",
100 * description: "Daily commodity price snapshots",
101 * tags: { team: "data" },
102 * });
103 * ```
104 *
105 * ### Publishing Revisions
106 * **Example:** Add a revision to a data set
107 * ```typescript
108 * const revision = yield* AWS.DataExchange.Revision("PricesV1", {
109 * dataSetId: dataSet.dataSetId,
110 * comment: "Initial snapshot",
111 * });
112 * ```
113 *
114 * @resource
115 */
116 export const DataSet = Resource<DataSet>("AWS.DataExchange.DataSet");
117
118 export const DataSetProvider = () =>
119 Provider.effect(
120 DataSet,
121 Effect.gen(function* () {
122 const createName = Effect.fn(function* (id: string, props: DataSetProps) {
123 return props.name ?? (yield* createPhysicalName({ id, maxLength: 98 }));
124 });
125
126 /** Get a data set by id; typed not-found → undefined. */
127 const getById = Effect.fn(function* (dataSetId: string) {
128 return yield* dataexchange
129 .getDataSet({ DataSetId: dataSetId })
130 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
131 });
132
133 /**
134 * Scan owned data sets for one with the given name. Data set ids are
135 * server-generated, so recovery from a lost output (state-persistence
136 * failure) goes through the deterministic physical name.
137 */
138 const findByName = Effect.fn(function* (name: string) {
139 const head = yield* dataexchange.listDataSets.items({ Origin: "OWNED" }).pipe(
140 Stream.filter((dataSet) => dataSet.Name === name),
141 Stream.runHead,
142 );
143 if (head._tag === "None") return undefined;
144 return yield* getById(head.value.Id);
145 });
146
147 const toAttrs = (dataSet: {
148 Id?: string;
149 Arn?: string;
150 Name?: string;
151 AssetType?: string;
152 Origin?: string;
153 }): DataSet["Attributes"] => ({
154 dataSetId: dataSet.Id!,
155 dataSetArn: dataSet.Arn!,
156 name: dataSet.Name!,
157 assetType: dataSet.AssetType!,
158 origin: dataSet.Origin!,
159 });
160
161 return {
162 stables: ["dataSetId", "dataSetArn", "assetType", "origin"],
163
164 read: Effect.fn(function* ({ id, olds, output }) {
165 const dataSet = output?.dataSetId
166 ? yield* getById(output.dataSetId)
167 : yield* findByName(yield* createName(id, olds ?? {}));
168 if (dataSet === undefined) return undefined;
169 const attrs = toAttrs(dataSet);
170 const tags = yield* readDataExchangeTags(attrs.dataSetArn);
171 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
172 }),
173
174 diff: Effect.fn(function* ({ news, olds }) {
175 if (!isResolved(news)) return undefined;
176 // The asset type is immutable; name and description update in place.
177 if ((news.assetType ?? "S3_SNAPSHOT") !== (olds.assetType ?? "S3_SNAPSHOT")) {
178 return { action: "replace" } as const;
179 }
180 }),
181
182 reconcile: Effect.fn(function* ({ id, news, output, session }) {
183 const name = yield* createName(id, news);
184 const assetType = news.assetType ?? "S3_SNAPSHOT";
185 const description = news.description ?? name;
186 const internalTags = yield* createInternalTags(id);
187 const desiredTags = { ...news.tags, ...internalTags };
188
189 // 1. Observe — the output id is only a cache; fall back to the
190 // deterministic name when it's missing or stale.
191 let dataSet = output?.dataSetId ? yield* getById(output.dataSetId) : undefined;
192 if (dataSet === undefined) {
193 dataSet = yield* findByName(name);
194 }
195
196 // 2. Ensure — create when missing. Data set names are not unique in
197 // DataExchange, so there is no AlreadyExists race to tolerate.
198 if (dataSet === undefined) {
199 dataSet = yield* dataexchange.createDataSet({
200 AssetType: assetType,
201 Name: name,
202 Description: description,
203 Tags: desiredTags,
204 });
205 }
206
207 // 3. Sync — name and description are mutable in place.
208 if (dataSet.Name !== name || dataSet.Description !== description) {
209 dataSet = yield* dataexchange.updateDataSet({
210 DataSetId: dataSet.Id!,
211 Name: name,
212 Description: description,
213 });
214 }
215
216 // 3b. Sync tags against OBSERVED cloud tags (adoption-safe).
217 yield* syncDataExchangeTags(dataSet.Arn!, desiredTags);
218
219 yield* session.note(dataSet.Arn!);
220 return {
221 dataSetId: dataSet.Id!,
222 dataSetArn: dataSet.Arn!,
223 name,
224 assetType: dataSet.AssetType ?? assetType,
225 origin: dataSet.Origin ?? "OWNED",
226 };
227 }),
228
229 delete: Effect.fn(function* ({ output }) {
230 yield* dataexchange.deleteDataSet({ DataSetId: output.dataSetId }).pipe(
231 // A revision-deletion race can transiently reject the delete;
232 // the engine deletes revisions first, so retry briefly.
233 Effect.retry({
234 while: (e) => e._tag === "ConflictException",
235 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
236 }),
237 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
238 );
239 }),
240
241 list: () =>
242 dataexchange.listDataSets.items({}).pipe(
243 Stream.map((entry) => ({
244 dataSetId: entry.Id,
245 dataSetArn: entry.Arn,
246 name: entry.Name,
247 assetType: entry.AssetType,
248 origin: entry.Origin,
249 })),
250 Stream.runCollect,
251 Effect.map((chunk) => Array.from(chunk)),
252 ),
253 };
254 }),
255 );