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