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