1import * as DynamoDB from "@distilled.cloud/aws/dynamodb";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Binding from "../../Binding.ts";
5import * as Output from "../../Output.ts";
6import { isBindingHost } from "../Lambda/Function.ts";
7import type { Bucket } from "../S3/Bucket.ts";
8import {
9 ExportTableToPointInTime,
10 type ExportTableToPointInTimeRequest,
11} from "./ExportTableToPointInTime.ts";
12import type { Table } from "./Table.ts";
13
14export const ExportTableToPointInTimeHttp = Layer.effect(
15 ExportTableToPointInTime,
16 Effect.gen(function* () {
17 const exportTableToPointInTime = yield* DynamoDB.exportTableToPointInTime;
18
19 return Effect.fn(function* <T extends Table, B extends Bucket>(
20 table: T,
21 bucket: B,
22 ) {
23 const TableArn = yield* table.tableArn;
24 const S3Bucket = yield* bucket.bucketName;
25 if (!globalThis.__ALCHEMY_RUNTIME__) {
26 const host = yield* Binding.Host;
27 if (isBindingHost(host)) {
28 yield* host.bind`Allow(${host}, AWS.DynamoDB.ExportTableToPointInTime(${table}, ${bucket}))`(
29 {
30 policyStatements: [
31 {
32 Effect: "Allow",
33 Action: ["dynamodb:ExportTableToPointInTime"],
34 Resource: [table.tableArn],
35 },
36 {
37 Effect: "Allow",
38 Action: [
39 "s3:AbortMultipartUpload",
40 "s3:PutObject",
41 "s3:PutObjectAcl",
42 ],
43 Resource: [Output.interpolate`${bucket.bucketArn}/*`],
44 },
45 ],
46 },
47 );
48 }
49 }
50 return Effect.fn(
51 `AWS.DynamoDB.ExportTableToPointInTime(${table.LogicalId}, ${bucket.LogicalId})`,
52 )(function* (request?: ExportTableToPointInTimeRequest) {
53 return yield* exportTableToPointInTime({
54 ...request,
55 TableArn: yield* TableArn,
56 S3Bucket: yield* S3Bucket,
57 });
58 });
59 });
60 }),
61);