1import * as DynamoDB from "@distilled.cloud/aws/dynamodb";
2import * as Effect from "effect/Effect";
3import * as Binding from "../../Binding.ts";
4import type { Table } from "./Table.ts";
5
6export type TransactWriteItemsTables = [Table, ...Table[]];
7
8type NativeTransactWriteItem = NonNullable<
9 DynamoDB.TransactWriteItemsInput["TransactItems"]
10>[number];
11
12type NativeConditionCheck = NonNullable<
13 NativeTransactWriteItem["ConditionCheck"]
14>;
15type NativeDelete = NonNullable<NativeTransactWriteItem["Delete"]>;
16type NativePut = NonNullable<NativeTransactWriteItem["Put"]>;
17type NativeUpdate = NonNullable<NativeTransactWriteItem["Update"]>;
18
19export interface BoundConditionCheck extends Omit<
20 NativeConditionCheck,
21 "TableName"
22> {
23 Table: string;
24}
25
26export interface BoundDelete extends Omit<NativeDelete, "TableName"> {
27 Table: string;
28}
29
30export interface BoundPut extends Omit<NativePut, "TableName"> {
31 Table: string;
32}
33
34export interface BoundUpdate extends Omit<NativeUpdate, "TableName"> {
35 Table: string;
36}
37
38export interface BoundTransactWriteItem {
39 ConditionCheck?: BoundConditionCheck;
40 Delete?: BoundDelete;
41 Put?: BoundPut;
42 Update?: BoundUpdate;
43}
44
45export 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 */
79export 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
94export const TransactWriteItems = Binding.Service<TransactWriteItems>(
95 "AWS.DynamoDB.TransactWriteItems",
96);