| 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 TransactWriteItemsTables = [Table, ...Table[]]; |
| 7 | |
| 8 | type NativeTransactWriteItem = NonNullable< |
| 9 | DynamoDB.TransactWriteItemsInput["TransactItems"] |
| 10 | >[number]; |
| 11 | |
| 12 | type NativeConditionCheck = NonNullable< |
| 13 | NativeTransactWriteItem["ConditionCheck"] |
| 14 | >; |
| 15 | type NativeDelete = NonNullable<NativeTransactWriteItem["Delete"]>; |
| 16 | type NativePut = NonNullable<NativeTransactWriteItem["Put"]>; |
| 17 | type NativeUpdate = NonNullable<NativeTransactWriteItem["Update"]>; |
| 18 | |
| 19 | export interface BoundConditionCheck extends Omit< |
| 20 | NativeConditionCheck, |
| 21 | "TableName" |
| 22 | > { |
| 23 | Table: string; |
| 24 | } |
| 25 | |
| 26 | export interface BoundDelete extends Omit<NativeDelete, "TableName"> { |
| 27 | Table: string; |
| 28 | } |
| 29 | |
| 30 | export interface BoundPut extends Omit<NativePut, "TableName"> { |
| 31 | Table: string; |
| 32 | } |
| 33 | |
| 34 | export interface BoundUpdate extends Omit<NativeUpdate, "TableName"> { |
| 35 | Table: string; |
| 36 | } |
| 37 | |
| 38 | export interface BoundTransactWriteItem { |
| 39 | ConditionCheck?: BoundConditionCheck; |
| 40 | Delete?: BoundDelete; |
| 41 | Put?: BoundPut; |
| 42 | Update?: BoundUpdate; |
| 43 | } |
| 44 | |
| 45 | export interface TransactWriteItemsRequest extends Omit< |
| 46 | DynamoDB.TransactWriteItemsInput, |
| 47 | "TransactItems" |
| 48 | > { |
| 49 | TransactItems: Array<BoundTransactWriteItem>; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Runtime binding for `dynamodb:TransactWriteItems`. |
| 54 | * |
| 55 | * Bind this operation to one or more tables and identify each item's target |
| 56 | * table by the bound table's `LogicalId`. |
| 57 | * ### Writing Data |
| 58 | * **Example:** Write Items Transactionally |
| 59 | * ```typescript |
| 60 | * const transactWriteItems = yield* AWS.DynamoDB.TransactWriteItems( |
| 61 | * sourceTable, |
| 62 | * archiveTable, |
| 63 | * ); |
| 64 | * |
| 65 | * yield* transactWriteItems({ |
| 66 | * TransactItems: [ |
| 67 | * { |
| 68 | * Put: { |
| 69 | * Table: sourceTable.LogicalId, |
| 70 | * Item: { pk: { S: "user#1" }, sk: { S: "profile" } }, |
| 71 | * }, |
| 72 | * }, |
| 73 | * ], |
| 74 | * }); |
| 75 | * ``` |
| 76 | * |
| 77 | * @binding |
| 78 | */ |
| 79 | export interface TransactWriteItems extends Binding.Service< |
| 80 | TransactWriteItems, |
| 81 | "AWS.DynamoDB.TransactWriteItems", |
| 82 | ( |
| 83 | ...tables: TransactWriteItemsTables |
| 84 | ) => Effect.Effect< |
| 85 | ( |
| 86 | request: TransactWriteItemsRequest, |
| 87 | ) => Effect.Effect< |
| 88 | DynamoDB.TransactWriteItemsOutput, |
| 89 | DynamoDB.TransactWriteItemsError |
| 90 | > |
| 91 | > |
| 92 | > {} |
| 93 | |
| 94 | export const TransactWriteItems = Binding.Service<TransactWriteItems>( |
| 95 | "AWS.DynamoDB.TransactWriteItems", |
| 96 | ); |