dyno-table 0.1.8 → 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/README.md +570 -147
- 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 +1 -1
- package/dist/builders/condition-check-builder.cjs.map +1 -1
- 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 +1 -1
- package/dist/builders/condition-check-builder.js.map +1 -1
- package/dist/builders/delete-builder.cjs +0 -17
- package/dist/builders/delete-builder.cjs.map +1 -1
- package/dist/builders/delete-builder.d.cts +166 -0
- package/dist/builders/delete-builder.d.ts +166 -0
- package/dist/builders/delete-builder.js +0 -17
- package/dist/builders/delete-builder.js.map +1 -1
- package/dist/builders/paginator.cjs.map +1 -1
- package/dist/builders/paginator.d.cts +179 -0
- package/dist/builders/paginator.d.ts +179 -0
- package/dist/builders/paginator.js.map +1 -1
- package/dist/builders/put-builder.cjs +8 -0
- package/dist/builders/put-builder.cjs.map +1 -1
- package/dist/builders/put-builder.d.cts +274 -0
- package/dist/builders/put-builder.d.ts +274 -0
- package/dist/builders/put-builder.js +8 -0
- package/dist/builders/put-builder.js.map +1 -1
- package/dist/builders/query-builder.cjs.map +1 -1
- package/dist/builders/query-builder.d.cts +6 -0
- package/dist/builders/query-builder.d.ts +6 -0
- package/dist/builders/query-builder.js.map +1 -1
- package/dist/builders/transaction-builder.cjs +40 -22
- package/dist/builders/transaction-builder.cjs.map +1 -1
- package/dist/builders/transaction-builder.d.cts +511 -0
- package/dist/builders/transaction-builder.d.ts +511 -0
- package/dist/builders/transaction-builder.js +40 -22
- package/dist/builders/transaction-builder.js.map +1 -1
- package/dist/builders/update-builder.cjs +3 -38
- package/dist/builders/update-builder.cjs.map +1 -1
- package/dist/builders/update-builder.d.cts +365 -0
- package/dist/builders/update-builder.d.ts +365 -0
- package/dist/builders/update-builder.js +3 -38
- package/dist/builders/update-builder.js.map +1 -1
- package/dist/conditions--ld9a78i.d.ts +331 -0
- package/dist/conditions-ChhQWd6z.d.cts +331 -0
- package/dist/conditions.cjs.map +1 -1
- package/dist/conditions.d.cts +3 -0
- package/dist/conditions.d.ts +3 -0
- package/dist/conditions.js.map +1 -1
- package/dist/entity.cjs +156 -97
- package/dist/entity.cjs.map +1 -1
- package/dist/entity.d.cts +149 -0
- package/dist/entity.d.ts +149 -0
- package/dist/entity.js +156 -97
- package/dist/entity.js.map +1 -1
- package/dist/query-builder-Csror9Iu.d.ts +507 -0
- package/dist/query-builder-D2FM9rsu.d.cts +507 -0
- package/dist/standard-schema.d.cts +57 -0
- package/dist/standard-schema.d.ts +57 -0
- package/dist/table-BEhBPy2G.d.cts +364 -0
- package/dist/table-BW3cmUqr.d.ts +364 -0
- package/dist/table.cjs +82 -102
- package/dist/table.cjs.map +1 -1
- package/dist/table.d.cts +12 -0
- package/dist/table.d.ts +12 -0
- package/dist/table.js +82 -102
- package/dist/table.js.map +1 -1
- package/dist/types.d.cts +22 -0
- package/dist/types.d.ts +22 -0
- package/dist/utils/{key-template.cjs → partition-key-template.cjs} +3 -3
- 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/{key-template.js → partition-key-template.js} +3 -3
- package/dist/utils/partition-key-template.js.map +1 -0
- package/dist/utils/sort-key-template.d.cts +35 -0
- package/dist/utils/sort-key-template.d.ts +35 -0
- package/package.json +86 -9
- package/dist/index.cjs +0 -3333
- package/dist/index.d.cts +0 -2971
- package/dist/index.d.ts +0 -2971
- package/dist/index.js +0 -3284
- package/dist/utils/key-template.cjs.map +0 -1
- package/dist/utils/key-template.js.map +0 -1
|
@@ -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 };
|
|
@@ -222,7 +222,7 @@ var TransactionBuilder = class {
|
|
|
222
222
|
*/
|
|
223
223
|
checkForDuplicateItem(tableName, newItem) {
|
|
224
224
|
const pkName = this.indexConfig.partitionKey;
|
|
225
|
-
const skName = this.indexConfig.sortKey
|
|
225
|
+
const skName = this.indexConfig.sortKey ?? "";
|
|
226
226
|
const pkValue = newItem[pkName];
|
|
227
227
|
const skValue = skName ? newItem[skName] : void 0;
|
|
228
228
|
if (!pkValue) {
|
|
@@ -263,6 +263,18 @@ var TransactionBuilder = class {
|
|
|
263
263
|
);
|
|
264
264
|
}
|
|
265
265
|
}
|
|
266
|
+
createKeyForPrimaryIndex(key) {
|
|
267
|
+
const keyCondition = {
|
|
268
|
+
[this.indexConfig.partitionKey]: key.pk
|
|
269
|
+
};
|
|
270
|
+
if (this.indexConfig.sortKey) {
|
|
271
|
+
if (key.sk === void 0) {
|
|
272
|
+
throw new Error("Sort key is required for delete operation");
|
|
273
|
+
}
|
|
274
|
+
keyCondition[this.indexConfig.sortKey] = key.sk;
|
|
275
|
+
}
|
|
276
|
+
return keyCondition;
|
|
277
|
+
}
|
|
266
278
|
/**
|
|
267
279
|
* Adds a put operation to the transaction.
|
|
268
280
|
* Use this method when you need to:
|
|
@@ -402,15 +414,13 @@ var TransactionBuilder = class {
|
|
|
402
414
|
* @throws {Error} If a duplicate item is detected in the transaction
|
|
403
415
|
*/
|
|
404
416
|
delete(tableName, key, condition) {
|
|
405
|
-
this.
|
|
417
|
+
const keyCondition = this.createKeyForPrimaryIndex(key);
|
|
418
|
+
this.checkForDuplicateItem(tableName, keyCondition);
|
|
406
419
|
const transactionItem = {
|
|
407
420
|
type: "Delete",
|
|
408
421
|
params: {
|
|
409
422
|
tableName,
|
|
410
|
-
key:
|
|
411
|
-
pk: key.pk,
|
|
412
|
-
sk: key.sk
|
|
413
|
-
}
|
|
423
|
+
key: keyCondition
|
|
414
424
|
}
|
|
415
425
|
};
|
|
416
426
|
if (condition) {
|
|
@@ -452,10 +462,14 @@ var TransactionBuilder = class {
|
|
|
452
462
|
* @see DeleteBuilder for creating delete commands
|
|
453
463
|
*/
|
|
454
464
|
deleteWithCommand(command) {
|
|
455
|
-
this.
|
|
465
|
+
const keyCondition = this.createKeyForPrimaryIndex(command.key);
|
|
466
|
+
this.checkForDuplicateItem(command.tableName, keyCondition);
|
|
456
467
|
const transactionItem = {
|
|
457
468
|
type: "Delete",
|
|
458
|
-
params:
|
|
469
|
+
params: {
|
|
470
|
+
...command,
|
|
471
|
+
key: keyCondition
|
|
472
|
+
}
|
|
459
473
|
};
|
|
460
474
|
this.items.push(transactionItem);
|
|
461
475
|
return this;
|
|
@@ -515,15 +529,13 @@ var TransactionBuilder = class {
|
|
|
515
529
|
* @throws {Error} If a duplicate item is detected in the transaction
|
|
516
530
|
*/
|
|
517
531
|
update(tableName, key, updateExpression, expressionAttributeNames, expressionAttributeValues, condition) {
|
|
518
|
-
this.
|
|
532
|
+
const keyCondition = this.createKeyForPrimaryIndex(key);
|
|
533
|
+
this.checkForDuplicateItem(tableName, keyCondition);
|
|
519
534
|
const transactionItem = {
|
|
520
535
|
type: "Update",
|
|
521
536
|
params: {
|
|
522
537
|
tableName,
|
|
523
|
-
key:
|
|
524
|
-
pk: key.pk,
|
|
525
|
-
sk: key.sk
|
|
526
|
-
},
|
|
538
|
+
key: keyCondition,
|
|
527
539
|
updateExpression,
|
|
528
540
|
expressionAttributeNames,
|
|
529
541
|
expressionAttributeValues
|
|
@@ -577,10 +589,14 @@ var TransactionBuilder = class {
|
|
|
577
589
|
* @see UpdateBuilder for creating update commands
|
|
578
590
|
*/
|
|
579
591
|
updateWithCommand(command) {
|
|
580
|
-
this.
|
|
592
|
+
const keyCondition = this.createKeyForPrimaryIndex(command.key);
|
|
593
|
+
this.checkForDuplicateItem(command.tableName, keyCondition);
|
|
581
594
|
const transactionItem = {
|
|
582
595
|
type: "Update",
|
|
583
|
-
params:
|
|
596
|
+
params: {
|
|
597
|
+
...command,
|
|
598
|
+
key: keyCondition
|
|
599
|
+
}
|
|
584
600
|
};
|
|
585
601
|
this.items.push(transactionItem);
|
|
586
602
|
return this;
|
|
@@ -637,7 +653,8 @@ var TransactionBuilder = class {
|
|
|
637
653
|
* @throws {Error} If condition expression generation fails
|
|
638
654
|
*/
|
|
639
655
|
conditionCheck(tableName, key, condition) {
|
|
640
|
-
this.
|
|
656
|
+
const keyCondition = this.createKeyForPrimaryIndex(key);
|
|
657
|
+
this.checkForDuplicateItem(tableName, keyCondition);
|
|
641
658
|
const { expression, names, values } = prepareExpressionParams(condition);
|
|
642
659
|
if (!expression) {
|
|
643
660
|
throw new Error("Failed to generate condition expression");
|
|
@@ -646,10 +663,7 @@ var TransactionBuilder = class {
|
|
|
646
663
|
type: "ConditionCheck",
|
|
647
664
|
params: {
|
|
648
665
|
tableName,
|
|
649
|
-
key:
|
|
650
|
-
pk: key.pk,
|
|
651
|
-
sk: key.sk
|
|
652
|
-
},
|
|
666
|
+
key: keyCondition,
|
|
653
667
|
conditionExpression: expression,
|
|
654
668
|
expressionAttributeNames: names,
|
|
655
669
|
expressionAttributeValues: values
|
|
@@ -689,10 +703,14 @@ var TransactionBuilder = class {
|
|
|
689
703
|
* @see ConditionCheckBuilder for creating condition check commands
|
|
690
704
|
*/
|
|
691
705
|
conditionCheckWithCommand(command) {
|
|
692
|
-
this.
|
|
706
|
+
const keyCondition = this.createKeyForPrimaryIndex(command.key);
|
|
707
|
+
this.checkForDuplicateItem(command.tableName, keyCondition);
|
|
693
708
|
const transactionItem = {
|
|
694
709
|
type: "ConditionCheck",
|
|
695
|
-
params:
|
|
710
|
+
params: {
|
|
711
|
+
...command,
|
|
712
|
+
key: keyCondition
|
|
713
|
+
}
|
|
696
714
|
};
|
|
697
715
|
this.items.push(transactionItem);
|
|
698
716
|
return this;
|