1import * as DynamoDB from "@distilled.cloud/aws/dynamodb";
2import * as Effect from "effect/Effect";
3import * as Binding from "../../Binding.ts";
4import * as Layer from "effect/Layer";
5import { isBindingHost } from "../Lambda/Function.ts";
6import {
7 RestoreTableToPointInTime,
8 type RestoreTableToPointInTimeRequest,
9} from "./RestoreTableToPointInTime.ts";
10import type { Table } from "./Table.ts";
11
12export const RestoreTableToPointInTimeHttp = Layer.effect(
13 RestoreTableToPointInTime,
14 Effect.gen(function* () {
15 const restoreTableToPointInTime = yield* DynamoDB.restoreTableToPointInTime;
16
17 return Effect.fn(function* <From extends Table, To extends Table>(
18 from: From,
19 to: To,
20 ) {
21 const SourceTableName = yield* from.tableName;
22 const TargetTableName = yield* to.tableName;
23 if (!globalThis.__ALCHEMY_RUNTIME__) {
24 const host = yield* Binding.Host;
25 if (isBindingHost(host)) {
26 yield* host.bind`Allow(${host}, AWS.DynamoDB.RestoreTableToPointInTime(${from}, ${to}))`(
27 {
28 policyStatements: [
29 {
30 Effect: "Allow",
31 Action: ["dynamodb:RestoreTableToPointInTime"],
32 Resource: [from.tableArn],
33 },
34 {
35 Effect: "Allow",
36 Action: [
37 "dynamodb:PutItem",
38 "dynamodb:UpdateItem",
39 "dynamodb:DeleteItem",
40 "dynamodb:GetItem",
41 "dynamodb:Query",
42 "dynamodb:Scan",
43 "dynamodb:BatchWriteItem",
44 ],
45 Resource: [to.tableArn],
46 },
47 ],
48 },
49 );
50 }
51 }
52 return Effect.fn(
53 `AWS.DynamoDB.RestoreTableToPointInTime(${from.LogicalId}, ${to.LogicalId})`,
54 )(function* (request: RestoreTableToPointInTimeRequest) {
55 return yield* restoreTableToPointInTime({
56 ...request,
57 SourceTableName: yield* SourceTableName,
58 TargetTableName: yield* TargetTableName,
59 });
60 });
61 });
62 }),
63);