| 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 type { Table } from "./Table.ts"; |
| 5 | |
| 6 | export type BatchWriteItemTables = [Table, ...Table[]]; |
| 7 | |
| 8 | type BatchWriteRequests = NonNullable< |
| 9 | DynamoDB.BatchWriteItemInput["RequestItems"] |
| 10 | >[string]; |
| 11 | |
| 12 | export const sortBatchWriteItemTables = (tables: BatchWriteItemTables) => |
| 13 | [ |
| 14 | ...new Map( |
| 15 | tables.map((table) => [table.LogicalId, table] as const), |
| 16 | ).values(), |
| 17 | ].sort((a, b) => |
| 18 | a.LogicalId.localeCompare(b.LogicalId), |
| 19 | ) as BatchWriteItemTables; |
| 20 | |
| 21 | export interface BatchWriteItemRequest extends Omit< |
| 22 | DynamoDB.BatchWriteItemInput, |
| 23 | "RequestItems" |
| 24 | > { |
| 25 | RequestItems: Record<string, BatchWriteRequests>; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Runtime binding for `dynamodb:BatchWriteItem`. |
| 30 | * |
| 31 | * Bind this operation to one or more tables and key the request by each bound |
| 32 | * table's `LogicalId`. |
| 33 | * ### Writing Data |
| 34 | * **Example:** Write Items Across Multiple Tables |
| 35 | * ```typescript |
| 36 | * const batchWriteItem = yield* AWS.DynamoDB.BatchWriteItem(sourceTable, archiveTable); |
| 37 | * |
| 38 | * const response = yield* batchWriteItem({ |
| 39 | * RequestItems: { |
| 40 | * [sourceTable.LogicalId]: [ |
| 41 | * { |
| 42 | * PutRequest: { |
| 43 | * Item: { |
| 44 | * pk: { S: "user#1" }, |
| 45 | * sk: { S: "profile" }, |
| 46 | * }, |
| 47 | * }, |
| 48 | * }, |
| 49 | * ], |
| 50 | * }, |
| 51 | * }); |
| 52 | * ``` |
| 53 | * |
| 54 | * @binding |
| 55 | */ |
| 56 | export interface BatchWriteItem extends Binding.Service< |
| 57 | BatchWriteItem, |
| 58 | "AWS.DynamoDB.BatchWriteItem", |
| 59 | ( |
| 60 | ...tables: BatchWriteItemTables |
| 61 | ) => Effect.Effect< |
| 62 | ( |
| 63 | request: BatchWriteItemRequest, |
| 64 | ) => Effect.Effect< |
| 65 | DynamoDB.BatchWriteItemOutput, |
| 66 | DynamoDB.BatchWriteItemError |
| 67 | > |
| 68 | > |
| 69 | > {} |
| 70 | |
| 71 | export const BatchWriteItem = Binding.Service<BatchWriteItem>( |
| 72 | "AWS.DynamoDB.BatchWriteItem", |
| 73 | ); |