| 1 | import * as DynamoDB from "@distilled.cloud/aws/dynamodb"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import * as Output from "../../Output.ts"; |
| 6 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 7 | import { |
| 8 | RestoreTableFromBackup, |
| 9 | type RestoreTableFromBackupRequest, |
| 10 | } from "./RestoreTableFromBackup.ts"; |
| 11 | import type { Table } from "./Table.ts"; |
| 12 | |
| 13 | export const RestoreTableFromBackupHttp = Layer.effect( |
| 14 | RestoreTableFromBackup, |
| 15 | Effect.gen(function* () { |
| 16 | const restoreTableFromBackup = yield* DynamoDB.restoreTableFromBackup; |
| 17 | |
| 18 | return Effect.fn(function* <From extends Table, To extends Table>( |
| 19 | from: From, |
| 20 | to: To, |
| 21 | ) { |
| 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.RestoreTableFromBackup(${from}, ${to}))`( |
| 27 | { |
| 28 | policyStatements: [ |
| 29 | { |
| 30 | Effect: "Allow", |
| 31 | Action: ["dynamodb:RestoreTableFromBackup"], |
| 32 | Resource: [Output.interpolate`${from.tableArn}/backup/*`], |
| 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.RestoreTableFromBackup(${from.LogicalId}, ${to.LogicalId})`, |
| 54 | )(function* (request: RestoreTableFromBackupRequest) { |
| 55 | return yield* restoreTableFromBackup({ |
| 56 | ...request, |
| 57 | TargetTableName: yield* TargetTableName, |
| 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | }), |
| 62 | ); |