| 1 | import * as DynamoDB from "@distilled.cloud/aws/dynamodb"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Output from "../../Output.ts"; |
| 6 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 7 | import { |
| 8 | BatchExecuteStatement, |
| 9 | type BatchExecuteStatementRequest, |
| 10 | type BatchExecuteStatementTables, |
| 11 | sortBatchExecuteStatementTables, |
| 12 | } from "./BatchExecuteStatement.ts"; |
| 13 | |
| 14 | export const BatchExecuteStatementHttp = Layer.effect( |
| 15 | BatchExecuteStatement, |
| 16 | Effect.gen(function* () { |
| 17 | const batchExecuteStatement = yield* DynamoDB.batchExecuteStatement; |
| 18 | |
| 19 | return Effect.fn(function* (...tables: BatchExecuteStatementTables) { |
| 20 | const sortedTables = sortBatchExecuteStatementTables(tables); |
| 21 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 22 | const host = yield* Binding.Host; |
| 23 | if (isBindingHost(host)) { |
| 24 | yield* host.bind`Allow(${host}, AWS.DynamoDB.BatchExecuteStatement(${sortedTables}))`( |
| 25 | { |
| 26 | policyStatements: [ |
| 27 | { |
| 28 | Effect: "Allow", |
| 29 | Action: [ |
| 30 | "dynamodb:PartiQLDelete", |
| 31 | "dynamodb:PartiQLInsert", |
| 32 | "dynamodb:PartiQLSelect", |
| 33 | "dynamodb:PartiQLUpdate", |
| 34 | ], |
| 35 | Resource: sortedTables.flatMap((table) => [ |
| 36 | table.tableArn, |
| 37 | Output.interpolate`${table.tableArn}/index/*`, |
| 38 | ]), |
| 39 | }, |
| 40 | ], |
| 41 | }, |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 | return Effect.fn(`AWS.DynamoDB.BatchExecuteStatement(${sortedTables})`)( |
| 46 | function* (request: BatchExecuteStatementRequest) { |
| 47 | return yield* batchExecuteStatement(request); |
| 48 | }, |
| 49 | ); |
| 50 | }); |
| 51 | }), |
| 52 | ); |