dyno-table 0.1.7 → 0.2.0-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +681 -160
- package/dist/builder-types-C_PDZhnP.d.ts +118 -0
- package/dist/builder-types-DtwbqMeF.d.cts +118 -0
- package/dist/builders/condition-check-builder.cjs +394 -0
- package/dist/builders/condition-check-builder.cjs.map +1 -0
- package/dist/builders/condition-check-builder.d.cts +157 -0
- package/dist/builders/condition-check-builder.d.ts +157 -0
- package/dist/builders/condition-check-builder.js +392 -0
- package/dist/builders/condition-check-builder.js.map +1 -0
- package/dist/builders/delete-builder.cjs +405 -0
- package/dist/builders/delete-builder.cjs.map +1 -0
- package/dist/builders/delete-builder.d.cts +166 -0
- package/dist/builders/delete-builder.d.ts +166 -0
- package/dist/builders/delete-builder.js +403 -0
- package/dist/builders/delete-builder.js.map +1 -0
- package/dist/builders/paginator.cjs +199 -0
- package/dist/builders/paginator.cjs.map +1 -0
- package/dist/builders/paginator.d.cts +179 -0
- package/dist/builders/paginator.d.ts +179 -0
- package/dist/builders/paginator.js +197 -0
- package/dist/builders/paginator.js.map +1 -0
- package/dist/builders/put-builder.cjs +476 -0
- package/dist/builders/put-builder.cjs.map +1 -0
- package/dist/builders/put-builder.d.cts +274 -0
- package/dist/builders/put-builder.d.ts +274 -0
- package/dist/builders/put-builder.js +474 -0
- package/dist/builders/put-builder.js.map +1 -0
- package/dist/builders/query-builder.cjs +674 -0
- package/dist/builders/query-builder.cjs.map +1 -0
- package/dist/builders/query-builder.d.cts +6 -0
- package/dist/builders/query-builder.d.ts +6 -0
- package/dist/builders/query-builder.js +672 -0
- package/dist/builders/query-builder.js.map +1 -0
- package/dist/builders/transaction-builder.cjs +894 -0
- package/dist/builders/transaction-builder.cjs.map +1 -0
- package/dist/builders/transaction-builder.d.cts +511 -0
- package/dist/builders/transaction-builder.d.ts +511 -0
- package/dist/builders/transaction-builder.js +892 -0
- package/dist/builders/transaction-builder.js.map +1 -0
- package/dist/builders/update-builder.cjs +627 -0
- package/dist/builders/update-builder.cjs.map +1 -0
- package/dist/builders/update-builder.d.cts +365 -0
- package/dist/builders/update-builder.d.ts +365 -0
- package/dist/builders/update-builder.js +625 -0
- package/dist/builders/update-builder.js.map +1 -0
- package/dist/conditions--ld9a78i.d.ts +331 -0
- package/dist/conditions-ChhQWd6z.d.cts +331 -0
- package/dist/conditions.cjs +59 -0
- package/dist/conditions.cjs.map +1 -0
- package/dist/conditions.d.cts +3 -0
- package/dist/conditions.d.ts +3 -0
- package/dist/conditions.js +43 -0
- package/dist/conditions.js.map +1 -0
- package/dist/entity.cjs +228 -0
- package/dist/entity.cjs.map +1 -0
- package/dist/entity.d.cts +149 -0
- package/dist/entity.d.ts +149 -0
- package/dist/entity.js +224 -0
- package/dist/entity.js.map +1 -0
- package/dist/query-builder-Csror9Iu.d.ts +507 -0
- package/dist/query-builder-D2FM9rsu.d.cts +507 -0
- package/dist/standard-schema.cjs +4 -0
- package/dist/standard-schema.cjs.map +1 -0
- package/dist/standard-schema.d.cts +57 -0
- package/dist/standard-schema.d.ts +57 -0
- package/dist/standard-schema.js +3 -0
- package/dist/standard-schema.js.map +1 -0
- package/dist/table-BEhBPy2G.d.cts +364 -0
- package/dist/table-BW3cmUqr.d.ts +364 -0
- package/dist/{index.js → table.cjs} +330 -354
- package/dist/table.cjs.map +1 -0
- package/dist/table.d.cts +12 -0
- package/dist/table.d.ts +12 -0
- package/dist/table.js +3243 -0
- package/dist/table.js.map +1 -0
- package/dist/types.cjs +4 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +22 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/partition-key-template.cjs +19 -0
- package/dist/utils/partition-key-template.cjs.map +1 -0
- package/dist/utils/partition-key-template.d.cts +32 -0
- package/dist/utils/partition-key-template.d.ts +32 -0
- package/dist/utils/partition-key-template.js +17 -0
- package/dist/utils/partition-key-template.js.map +1 -0
- package/dist/utils/sort-key-template.cjs +19 -0
- package/dist/utils/sort-key-template.cjs.map +1 -0
- package/dist/utils/sort-key-template.d.cts +35 -0
- package/dist/utils/sort-key-template.d.ts +35 -0
- package/dist/utils/sort-key-template.js +17 -0
- package/dist/utils/sort-key-template.js.map +1 -0
- package/package.json +97 -22
- package/dist/index.d.ts +0 -2923
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
import { TransactWriteCommandInput } from '@aws-sdk/lib-dynamodb';
|
|
2
|
+
import { P as PrimaryKeyWithoutExpression, C as Condition } from '../conditions--ld9a78i.js';
|
|
3
|
+
import { a as PutCommandParams, D as DeleteCommandParams, U as UpdateCommandParams, C as ConditionCheckCommandParams } from '../builder-types-C_PDZhnP.js';
|
|
4
|
+
import { DynamoItem } from '../types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for DynamoDB transactions.
|
|
8
|
+
*/
|
|
9
|
+
interface TransactionOptions {
|
|
10
|
+
/** Unique identifier for the transaction request (idempotency token) */
|
|
11
|
+
clientRequestToken?: string;
|
|
12
|
+
/** Level of consumed capacity details to return */
|
|
13
|
+
returnConsumedCapacity?: "INDEXES" | "TOTAL" | "NONE";
|
|
14
|
+
/** Whether to return item collection metrics */
|
|
15
|
+
returnItemCollectionMetrics?: "SIZE" | "NONE";
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for table indexes used in duplicate detection.
|
|
19
|
+
* Defines the key structure for checking uniqueness constraints.
|
|
20
|
+
*/
|
|
21
|
+
interface IndexConfig {
|
|
22
|
+
/** The partition key attribute name */
|
|
23
|
+
partitionKey: string;
|
|
24
|
+
/** Optional sort key attribute name */
|
|
25
|
+
sortKey?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Function type for executing DynamoDB transaction operations.
|
|
29
|
+
* @param params - The complete transaction command input
|
|
30
|
+
* @returns A promise that resolves when the transaction completes
|
|
31
|
+
*/
|
|
32
|
+
type TransactionExecutor = (params: TransactWriteCommandInput) => Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Builder for creating and executing DynamoDB transactions.
|
|
35
|
+
* Use this builder when you need to:
|
|
36
|
+
* - Perform multiple operations atomically
|
|
37
|
+
* - Ensure data consistency across operations
|
|
38
|
+
* - Implement complex business logic that requires atomic updates
|
|
39
|
+
* - Prevent duplicate items across tables
|
|
40
|
+
*
|
|
41
|
+
* The builder supports:
|
|
42
|
+
* - Put operations (insert/replace items)
|
|
43
|
+
* - Delete operations
|
|
44
|
+
* - Update operations
|
|
45
|
+
* - Condition checks
|
|
46
|
+
* - Duplicate detection
|
|
47
|
+
* - Transaction-wide options
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* // Create a transaction with multiple operations
|
|
52
|
+
* const transaction = new TransactionBuilder(executor, {
|
|
53
|
+
* partitionKey: 'id',
|
|
54
|
+
* sortKey: 'type'
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* // Add a new order
|
|
58
|
+
* transaction.put('orders', {
|
|
59
|
+
* orderId: '123',
|
|
60
|
+
* status: 'PENDING'
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* // Update inventory with condition
|
|
64
|
+
* transaction.update(
|
|
65
|
+
* 'inventory',
|
|
66
|
+
* { productId: 'ABC' },
|
|
67
|
+
* 'set quantity = quantity - :amount',
|
|
68
|
+
* { ':amount': 1 },
|
|
69
|
+
* op => op.gte('quantity', 1)
|
|
70
|
+
* );
|
|
71
|
+
*
|
|
72
|
+
* // Execute the transaction atomically
|
|
73
|
+
* await transaction.execute();
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* Note: DynamoDB transactions have some limitations:
|
|
77
|
+
* - Maximum 25 operations per transaction
|
|
78
|
+
* - All operations must be in the same AWS region
|
|
79
|
+
* - Cannot include table scans or queries
|
|
80
|
+
*/
|
|
81
|
+
declare class TransactionBuilder {
|
|
82
|
+
private items;
|
|
83
|
+
private options;
|
|
84
|
+
private indexConfig;
|
|
85
|
+
private readonly executor;
|
|
86
|
+
constructor(executor: TransactionExecutor, indexConfig: IndexConfig);
|
|
87
|
+
/**
|
|
88
|
+
* Checks if an item with the same primary key already exists in the transaction
|
|
89
|
+
* @private
|
|
90
|
+
*/
|
|
91
|
+
private checkForDuplicateItem;
|
|
92
|
+
createKeyForPrimaryIndex(key: PrimaryKeyWithoutExpression): {
|
|
93
|
+
[x: string]: string;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Adds a put operation to the transaction.
|
|
97
|
+
* Use this method when you need to:
|
|
98
|
+
* - Insert new items as part of a transaction
|
|
99
|
+
* - Replace existing items atomically
|
|
100
|
+
* - Ensure items meet certain conditions before insertion
|
|
101
|
+
*
|
|
102
|
+
* The method automatically checks for duplicate items within the transaction
|
|
103
|
+
* to prevent multiple operations on the same item.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* // Simple put operation
|
|
108
|
+
* transaction.put('orders', {
|
|
109
|
+
* orderId: '123',
|
|
110
|
+
* status: 'PENDING',
|
|
111
|
+
* amount: 100
|
|
112
|
+
* });
|
|
113
|
+
*
|
|
114
|
+
* // Conditional put operation
|
|
115
|
+
* transaction.put(
|
|
116
|
+
* 'inventory',
|
|
117
|
+
* { productId: 'ABC', quantity: 50 },
|
|
118
|
+
* op => op.attributeNotExists('productId')
|
|
119
|
+
* );
|
|
120
|
+
*
|
|
121
|
+
* // Put with complex condition
|
|
122
|
+
* transaction.put(
|
|
123
|
+
* 'users',
|
|
124
|
+
* { userId: '123', status: 'ACTIVE' },
|
|
125
|
+
* op => op.and([
|
|
126
|
+
* op.attributeNotExists('userId'),
|
|
127
|
+
* op.beginsWith('status', 'ACTIVE')
|
|
128
|
+
* ])
|
|
129
|
+
* );
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @param tableName - The name of the DynamoDB table
|
|
133
|
+
* @param item - The item to put into the table
|
|
134
|
+
* @param condition - Optional condition that must be satisfied
|
|
135
|
+
* @returns The transaction builder for method chaining
|
|
136
|
+
* @throws {Error} If a duplicate item is detected in the transaction
|
|
137
|
+
*/
|
|
138
|
+
put<T extends DynamoItem>(tableName: string, item: T, condition?: Condition): this;
|
|
139
|
+
/**
|
|
140
|
+
* Adds a pre-configured put operation to the transaction.
|
|
141
|
+
* Use this method when you need to:
|
|
142
|
+
* - Reuse put commands from PutBuilder
|
|
143
|
+
* - Add complex put operations with pre-configured parameters
|
|
144
|
+
* - Integrate with existing put command configurations
|
|
145
|
+
*
|
|
146
|
+
* This method is particularly useful when working with PutBuilder
|
|
147
|
+
* to maintain consistency in put operations across your application.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* // Create a put command with PutBuilder
|
|
152
|
+
* const putCommand = new PutBuilder(executor, newItem, 'users')
|
|
153
|
+
* .condition(op => op.attributeNotExists('userId'))
|
|
154
|
+
* .toDynamoCommand();
|
|
155
|
+
*
|
|
156
|
+
* // Add the command to the transaction
|
|
157
|
+
* transaction.putWithCommand(putCommand);
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* @param command - The complete put command configuration
|
|
161
|
+
* @returns The transaction builder for method chaining
|
|
162
|
+
* @throws {Error} If a duplicate item is detected in the transaction
|
|
163
|
+
* @see PutBuilder for creating put commands
|
|
164
|
+
*/
|
|
165
|
+
putWithCommand(command: PutCommandParams): TransactionBuilder;
|
|
166
|
+
/**
|
|
167
|
+
* Adds a delete operation to the transaction.
|
|
168
|
+
* Use this method when you need to:
|
|
169
|
+
* - Remove items as part of a transaction
|
|
170
|
+
* - Conditionally delete items
|
|
171
|
+
* - Ensure items exist before deletion
|
|
172
|
+
*
|
|
173
|
+
* The method automatically checks for duplicate items within the transaction
|
|
174
|
+
* to prevent multiple operations on the same item.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* // Simple delete operation
|
|
179
|
+
* transaction.delete('orders', {
|
|
180
|
+
* pk: 'ORDER#123',
|
|
181
|
+
* sk: 'METADATA'
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* // Conditional delete operation
|
|
185
|
+
* transaction.delete(
|
|
186
|
+
* 'users',
|
|
187
|
+
* { pk: 'USER#123' },
|
|
188
|
+
* op => op.eq('status', 'INACTIVE')
|
|
189
|
+
* );
|
|
190
|
+
*
|
|
191
|
+
* // Delete with complex condition
|
|
192
|
+
* transaction.delete(
|
|
193
|
+
* 'products',
|
|
194
|
+
* { pk: 'PROD#ABC' },
|
|
195
|
+
* op => op.and([
|
|
196
|
+
* op.eq('status', 'DRAFT'),
|
|
197
|
+
* op.lt('version', 5)
|
|
198
|
+
* ])
|
|
199
|
+
* );
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* @param tableName - The name of the DynamoDB table
|
|
203
|
+
* @param key - The primary key of the item to delete
|
|
204
|
+
* @param condition - Optional condition that must be satisfied
|
|
205
|
+
* @returns The transaction builder for method chaining
|
|
206
|
+
* @throws {Error} If a duplicate item is detected in the transaction
|
|
207
|
+
*/
|
|
208
|
+
delete(tableName: string, key: PrimaryKeyWithoutExpression, condition?: Condition): TransactionBuilder;
|
|
209
|
+
/**
|
|
210
|
+
* Adds a pre-configured delete operation to the transaction.
|
|
211
|
+
* Use this method when you need to:
|
|
212
|
+
* - Reuse delete commands from DeleteBuilder
|
|
213
|
+
* - Add complex delete operations with pre-configured parameters
|
|
214
|
+
* - Integrate with existing delete command configurations
|
|
215
|
+
*
|
|
216
|
+
* This method is particularly useful when working with DeleteBuilder
|
|
217
|
+
* to maintain consistency in delete operations across your application.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* // Create a delete command with DeleteBuilder
|
|
222
|
+
* const deleteCommand = new DeleteBuilder(executor, 'users', { pk: 'USER#123' })
|
|
223
|
+
* .condition(op => op.and([
|
|
224
|
+
* op.attributeExists('pk'),
|
|
225
|
+
* op.eq('status', 'INACTIVE')
|
|
226
|
+
* ]))
|
|
227
|
+
* .toDynamoCommand();
|
|
228
|
+
*
|
|
229
|
+
* // Add the command to the transaction
|
|
230
|
+
* transaction.deleteWithCommand(deleteCommand);
|
|
231
|
+
* ```
|
|
232
|
+
*
|
|
233
|
+
* @param command - The complete delete command configuration
|
|
234
|
+
* @returns The transaction builder for method chaining
|
|
235
|
+
* @throws {Error} If a duplicate item is detected in the transaction
|
|
236
|
+
* @see DeleteBuilder for creating delete commands
|
|
237
|
+
*/
|
|
238
|
+
deleteWithCommand(command: DeleteCommandParams): this;
|
|
239
|
+
/**
|
|
240
|
+
* Adds an update operation to the transaction.
|
|
241
|
+
* Use this method when you need to:
|
|
242
|
+
* - Modify existing items as part of a transaction
|
|
243
|
+
* - Update multiple attributes atomically
|
|
244
|
+
* - Apply conditional updates
|
|
245
|
+
* - Perform complex attribute manipulations
|
|
246
|
+
*
|
|
247
|
+
* The method supports all DynamoDB update expressions:
|
|
248
|
+
* - SET: Modify or add attributes
|
|
249
|
+
* - REMOVE: Delete attributes
|
|
250
|
+
* - ADD: Update numbers and sets
|
|
251
|
+
* - DELETE: Remove elements from a set
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```typescript
|
|
255
|
+
* // Simple update
|
|
256
|
+
* transaction.update(
|
|
257
|
+
* 'orders',
|
|
258
|
+
* { pk: 'ORDER#123' },
|
|
259
|
+
* 'SET #status = :status',
|
|
260
|
+
* { '#status': 'status' },
|
|
261
|
+
* { ':status': 'PROCESSING' }
|
|
262
|
+
* );
|
|
263
|
+
*
|
|
264
|
+
* // Complex update with multiple operations
|
|
265
|
+
* transaction.update(
|
|
266
|
+
* 'products',
|
|
267
|
+
* { pk: 'PROD#ABC' },
|
|
268
|
+
* 'SET #qty = #qty - :amount, #status = :status REMOVE #oldAttr',
|
|
269
|
+
* { '#qty': 'quantity', '#status': 'status', '#oldAttr': 'deprecated_field' },
|
|
270
|
+
* { ':amount': 1, ':status': 'LOW_STOCK' }
|
|
271
|
+
* );
|
|
272
|
+
*
|
|
273
|
+
* // Conditional update
|
|
274
|
+
* transaction.update(
|
|
275
|
+
* 'users',
|
|
276
|
+
* { pk: 'USER#123' },
|
|
277
|
+
* 'SET #lastLogin = :now',
|
|
278
|
+
* { '#lastLogin': 'lastLoginDate' },
|
|
279
|
+
* { ':now': new Date().toISOString() },
|
|
280
|
+
* op => op.attributeExists('pk')
|
|
281
|
+
* );
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @param tableName - The name of the DynamoDB table
|
|
285
|
+
* @param key - The primary key of the item to update
|
|
286
|
+
* @param updateExpression - The update expression (SET, REMOVE, ADD, DELETE)
|
|
287
|
+
* @param expressionAttributeNames - Map of attribute name placeholders to actual names
|
|
288
|
+
* @param expressionAttributeValues - Map of value placeholders to actual values
|
|
289
|
+
* @param condition - Optional condition that must be satisfied
|
|
290
|
+
* @returns The transaction builder for method chaining
|
|
291
|
+
* @throws {Error} If a duplicate item is detected in the transaction
|
|
292
|
+
*/
|
|
293
|
+
update<T extends DynamoItem>(tableName: string, key: PrimaryKeyWithoutExpression, updateExpression: string, expressionAttributeNames?: Record<string, string>, expressionAttributeValues?: Record<string, unknown>, condition?: Condition): this;
|
|
294
|
+
/**
|
|
295
|
+
* Adds a pre-configured update operation to the transaction.
|
|
296
|
+
* Use this method when you need to:
|
|
297
|
+
* - Reuse update commands from UpdateBuilder
|
|
298
|
+
* - Add complex update operations with pre-configured parameters
|
|
299
|
+
* - Integrate with existing update command configurations
|
|
300
|
+
*
|
|
301
|
+
* This method is particularly useful when working with UpdateBuilder
|
|
302
|
+
* to maintain consistency in update operations across your application.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* // Create an update command with UpdateBuilder
|
|
307
|
+
* const updateCommand = new UpdateBuilder(executor, 'inventory', { pk: 'PROD#ABC' })
|
|
308
|
+
* .set('quantity', ':qty')
|
|
309
|
+
* .set('lastUpdated', ':now')
|
|
310
|
+
* .values({
|
|
311
|
+
* ':qty': 100,
|
|
312
|
+
* ':now': new Date().toISOString()
|
|
313
|
+
* })
|
|
314
|
+
* .condition(op => op.gt('quantity', 0))
|
|
315
|
+
* .toDynamoCommand();
|
|
316
|
+
*
|
|
317
|
+
* // Add the command to the transaction
|
|
318
|
+
* transaction.updateWithCommand(updateCommand);
|
|
319
|
+
* ```
|
|
320
|
+
*
|
|
321
|
+
* @param command - The complete update command configuration
|
|
322
|
+
* @returns The transaction builder for method chaining
|
|
323
|
+
* @throws {Error} If a duplicate item is detected in the transaction
|
|
324
|
+
* @see UpdateBuilder for creating update commands
|
|
325
|
+
*/
|
|
326
|
+
updateWithCommand(command: UpdateCommandParams): TransactionBuilder;
|
|
327
|
+
/**
|
|
328
|
+
* Adds a condition check operation to the transaction.
|
|
329
|
+
* Use this method when you need to:
|
|
330
|
+
* - Validate item state without modifying it
|
|
331
|
+
* - Ensure data consistency across tables
|
|
332
|
+
* - Implement complex business rules
|
|
333
|
+
* - Verify preconditions for other operations
|
|
334
|
+
*
|
|
335
|
+
* Condition checks are particularly useful for:
|
|
336
|
+
* - Implementing optimistic locking
|
|
337
|
+
* - Ensuring referential integrity
|
|
338
|
+
* - Validating business rules atomically
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```typescript
|
|
342
|
+
* // Check if order is in correct state
|
|
343
|
+
* transaction.conditionCheck(
|
|
344
|
+
* 'orders',
|
|
345
|
+
* { pk: 'ORDER#123' },
|
|
346
|
+
* op => op.eq('status', 'PENDING')
|
|
347
|
+
* );
|
|
348
|
+
*
|
|
349
|
+
* // Complex condition check
|
|
350
|
+
* transaction.conditionCheck(
|
|
351
|
+
* 'inventory',
|
|
352
|
+
* { pk: 'PROD#ABC' },
|
|
353
|
+
* op => op.and([
|
|
354
|
+
* op.gt('quantity', 0),
|
|
355
|
+
* op.eq('status', 'ACTIVE'),
|
|
356
|
+
* op.attributeExists('lastRestockDate')
|
|
357
|
+
* ])
|
|
358
|
+
* );
|
|
359
|
+
*
|
|
360
|
+
* // Check with multiple attributes
|
|
361
|
+
* transaction.conditionCheck(
|
|
362
|
+
* 'users',
|
|
363
|
+
* { pk: 'USER#123' },
|
|
364
|
+
* op => op.or([
|
|
365
|
+
* op.eq('status', 'PREMIUM'),
|
|
366
|
+
* op.gte('credits', 100)
|
|
367
|
+
* ])
|
|
368
|
+
* );
|
|
369
|
+
* ```
|
|
370
|
+
*
|
|
371
|
+
* @param tableName - The name of the DynamoDB table
|
|
372
|
+
* @param key - The primary key of the item to check
|
|
373
|
+
* @param condition - The condition that must be satisfied
|
|
374
|
+
* @returns The transaction builder for method chaining
|
|
375
|
+
* @throws {Error} If a duplicate item is detected in the transaction
|
|
376
|
+
* @throws {Error} If condition expression generation fails
|
|
377
|
+
*/
|
|
378
|
+
conditionCheck(tableName: string, key: PrimaryKeyWithoutExpression, condition: Condition): TransactionBuilder;
|
|
379
|
+
/**
|
|
380
|
+
* Adds a pre-configured condition check operation to the transaction.
|
|
381
|
+
* Use this method when you need to:
|
|
382
|
+
* - Reuse condition checks from ConditionCheckBuilder
|
|
383
|
+
* - Add complex condition checks with pre-configured parameters
|
|
384
|
+
* - Integrate with existing condition check configurations
|
|
385
|
+
*
|
|
386
|
+
* This method is particularly useful when working with ConditionCheckBuilder
|
|
387
|
+
* to maintain consistency in condition checks across your application.
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* ```typescript
|
|
391
|
+
* // Create a condition check with ConditionCheckBuilder
|
|
392
|
+
* const checkCommand = new ConditionCheckBuilder('inventory', { pk: 'PROD#ABC' })
|
|
393
|
+
* .condition(op => op.and([
|
|
394
|
+
* op.between('quantity', 10, 100),
|
|
395
|
+
* op.beginsWith('category', 'ELECTRONICS'),
|
|
396
|
+
* op.attributeExists('lastAuditDate')
|
|
397
|
+
* ]))
|
|
398
|
+
* .toDynamoCommand();
|
|
399
|
+
*
|
|
400
|
+
* // Add the command to the transaction
|
|
401
|
+
* transaction.conditionCheckWithCommand(checkCommand);
|
|
402
|
+
* ```
|
|
403
|
+
*
|
|
404
|
+
* @param command - The complete condition check command configuration
|
|
405
|
+
* @returns The transaction builder for method chaining
|
|
406
|
+
* @throws {Error} If a duplicate item is detected in the transaction
|
|
407
|
+
* @see ConditionCheckBuilder for creating condition check commands
|
|
408
|
+
*/
|
|
409
|
+
conditionCheckWithCommand(command: ConditionCheckCommandParams): TransactionBuilder;
|
|
410
|
+
/**
|
|
411
|
+
* Sets options for the transaction execution.
|
|
412
|
+
* Use this method when you need to:
|
|
413
|
+
* - Enable idempotent transactions
|
|
414
|
+
* - Track consumed capacity
|
|
415
|
+
* - Monitor item collection metrics
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* ```typescript
|
|
419
|
+
* // Enable idempotency and capacity tracking
|
|
420
|
+
* transaction.withOptions({
|
|
421
|
+
* clientRequestToken: 'unique-request-id-123',
|
|
422
|
+
* returnConsumedCapacity: 'TOTAL'
|
|
423
|
+
* });
|
|
424
|
+
*
|
|
425
|
+
* // Track item collection metrics
|
|
426
|
+
* transaction.withOptions({
|
|
427
|
+
* returnItemCollectionMetrics: 'SIZE'
|
|
428
|
+
* });
|
|
429
|
+
* ```
|
|
430
|
+
*
|
|
431
|
+
* Note: ClientRequestToken can be used to make transactions idempotent,
|
|
432
|
+
* ensuring the same transaction is not executed multiple times.
|
|
433
|
+
*
|
|
434
|
+
* @param options - Configuration options for the transaction
|
|
435
|
+
* @returns The transaction builder for method chaining
|
|
436
|
+
*/
|
|
437
|
+
withOptions(options: TransactionOptions): TransactionBuilder;
|
|
438
|
+
/**
|
|
439
|
+
* Gets a human-readable representation of the transaction items.
|
|
440
|
+
* Use this method when you need to:
|
|
441
|
+
* - Debug complex transactions
|
|
442
|
+
* - Verify operation parameters
|
|
443
|
+
* - Log transaction details
|
|
444
|
+
* - Troubleshoot condition expressions
|
|
445
|
+
*
|
|
446
|
+
* The method resolves all expression placeholders with their actual values,
|
|
447
|
+
* making it easier to understand the transaction's operations.
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* ```typescript
|
|
451
|
+
* // Add multiple operations
|
|
452
|
+
* transaction
|
|
453
|
+
* .put('orders', { orderId: '123', status: 'PENDING' })
|
|
454
|
+
* .update('inventory',
|
|
455
|
+
* { productId: 'ABC' },
|
|
456
|
+
* 'SET quantity = quantity - :amount',
|
|
457
|
+
* undefined,
|
|
458
|
+
* { ':amount': 1 }
|
|
459
|
+
* );
|
|
460
|
+
*
|
|
461
|
+
* // Debug the transaction
|
|
462
|
+
* const debugInfo = transaction.debug();
|
|
463
|
+
* console.log('Transaction operations:', debugInfo);
|
|
464
|
+
* ```
|
|
465
|
+
*
|
|
466
|
+
* @returns An array of readable representations of the transaction items
|
|
467
|
+
*/
|
|
468
|
+
debug(): DynamoItem[];
|
|
469
|
+
/**
|
|
470
|
+
* Executes all operations in the transaction atomically.
|
|
471
|
+
* Use this method when you need to:
|
|
472
|
+
* - Perform multiple operations atomically
|
|
473
|
+
* - Ensure all-or-nothing execution
|
|
474
|
+
* - Maintain data consistency across operations
|
|
475
|
+
*
|
|
476
|
+
* The transaction will only succeed if all operations succeed.
|
|
477
|
+
* If any operation fails, the entire transaction is rolled back.
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```typescript
|
|
481
|
+
* try {
|
|
482
|
+
* // Build and execute transaction
|
|
483
|
+
* await transaction
|
|
484
|
+
* .put('orders', newOrder)
|
|
485
|
+
* .update('inventory',
|
|
486
|
+
* { productId: 'ABC' },
|
|
487
|
+
* 'SET quantity = quantity - :qty',
|
|
488
|
+
* undefined,
|
|
489
|
+
* { ':qty': 1 }
|
|
490
|
+
* )
|
|
491
|
+
* .conditionCheck('products',
|
|
492
|
+
* { productId: 'ABC' },
|
|
493
|
+
* op => op.eq('status', 'ACTIVE')
|
|
494
|
+
* )
|
|
495
|
+
* .execute();
|
|
496
|
+
*
|
|
497
|
+
* console.log('Transaction completed successfully');
|
|
498
|
+
* } catch (error) {
|
|
499
|
+
* // Handle transaction failure
|
|
500
|
+
* console.error('Transaction failed:', error);
|
|
501
|
+
* }
|
|
502
|
+
* ```
|
|
503
|
+
*
|
|
504
|
+
* @throws {Error} If no transaction items are specified
|
|
505
|
+
* @throws {Error} If any operation in the transaction fails
|
|
506
|
+
* @returns A promise that resolves when the transaction completes
|
|
507
|
+
*/
|
|
508
|
+
execute(): Promise<void>;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export { TransactionBuilder, type TransactionExecutor, type TransactionOptions };
|