dyno-table 2.2.1 → 2.3.1

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 (102) hide show
  1. package/README.md +187 -1865
  2. package/dist/builders.cjs +55 -0
  3. package/dist/builders.d.cts +4 -0
  4. package/dist/builders.d.ts +4 -0
  5. package/dist/builders.js +2 -0
  6. package/dist/chunk-2EWNZOUK.js +618 -0
  7. package/dist/chunk-2WIBY7PZ.js +46 -0
  8. package/dist/chunk-7UJJ7JXM.cjs +63 -0
  9. package/dist/chunk-DTFJJASK.js +3200 -0
  10. package/dist/chunk-EODPMYPE.js +558 -0
  11. package/dist/chunk-KA3VPIPS.cjs +560 -0
  12. package/dist/chunk-NTA6GDPP.cjs +622 -0
  13. package/dist/chunk-PB7BBCZO.cjs +32 -0
  14. package/dist/chunk-QVRMYGC4.js +29 -0
  15. package/dist/chunk-XYL43FDX.cjs +3217 -0
  16. package/dist/conditions.cjs +67 -62
  17. package/dist/conditions.js +1 -48
  18. package/dist/entity.cjs +14 -625
  19. package/dist/entity.d.cts +2 -10
  20. package/dist/entity.d.ts +2 -10
  21. package/dist/entity.js +2 -626
  22. package/dist/index-2cbm07Bi.d.ts +2797 -0
  23. package/dist/index-DlN8G9hd.d.cts +2797 -0
  24. package/dist/index.cjs +111 -4460
  25. package/dist/index.d.cts +2 -10
  26. package/dist/index.d.ts +2 -10
  27. package/dist/index.js +5 -4442
  28. package/dist/standard-schema.cjs +0 -2
  29. package/dist/standard-schema.js +0 -2
  30. package/dist/table.cjs +7 -3796
  31. package/dist/table.d.cts +163 -12
  32. package/dist/table.d.ts +163 -12
  33. package/dist/table.js +3 -3799
  34. package/dist/types.cjs +0 -2
  35. package/dist/types.js +0 -2
  36. package/dist/utils.cjs +10 -30
  37. package/dist/utils.js +1 -31
  38. package/package.json +6 -66
  39. package/dist/batch-builder-BiQDIZ7p.d.cts +0 -398
  40. package/dist/batch-builder-CNsLS6sR.d.ts +0 -398
  41. package/dist/builder-types-BTVhQSHI.d.cts +0 -169
  42. package/dist/builder-types-CzuLR4Th.d.ts +0 -169
  43. package/dist/builders/condition-check-builder.cjs +0 -422
  44. package/dist/builders/condition-check-builder.cjs.map +0 -1
  45. package/dist/builders/condition-check-builder.d.cts +0 -153
  46. package/dist/builders/condition-check-builder.d.ts +0 -153
  47. package/dist/builders/condition-check-builder.js +0 -420
  48. package/dist/builders/condition-check-builder.js.map +0 -1
  49. package/dist/builders/delete-builder.cjs +0 -484
  50. package/dist/builders/delete-builder.cjs.map +0 -1
  51. package/dist/builders/delete-builder.d.cts +0 -211
  52. package/dist/builders/delete-builder.d.ts +0 -211
  53. package/dist/builders/delete-builder.js +0 -482
  54. package/dist/builders/delete-builder.js.map +0 -1
  55. package/dist/builders/paginator.cjs +0 -193
  56. package/dist/builders/paginator.cjs.map +0 -1
  57. package/dist/builders/paginator.d.cts +0 -155
  58. package/dist/builders/paginator.d.ts +0 -155
  59. package/dist/builders/paginator.js +0 -191
  60. package/dist/builders/paginator.js.map +0 -1
  61. package/dist/builders/put-builder.cjs +0 -554
  62. package/dist/builders/put-builder.cjs.map +0 -1
  63. package/dist/builders/put-builder.d.cts +0 -319
  64. package/dist/builders/put-builder.d.ts +0 -319
  65. package/dist/builders/put-builder.js +0 -552
  66. package/dist/builders/put-builder.js.map +0 -1
  67. package/dist/builders/query-builder.cjs +0 -757
  68. package/dist/builders/query-builder.cjs.map +0 -1
  69. package/dist/builders/query-builder.d.cts +0 -6
  70. package/dist/builders/query-builder.d.ts +0 -6
  71. package/dist/builders/query-builder.js +0 -755
  72. package/dist/builders/query-builder.js.map +0 -1
  73. package/dist/builders/transaction-builder.cjs +0 -906
  74. package/dist/builders/transaction-builder.cjs.map +0 -1
  75. package/dist/builders/transaction-builder.d.cts +0 -464
  76. package/dist/builders/transaction-builder.d.ts +0 -464
  77. package/dist/builders/transaction-builder.js +0 -904
  78. package/dist/builders/transaction-builder.js.map +0 -1
  79. package/dist/builders/update-builder.cjs +0 -668
  80. package/dist/builders/update-builder.cjs.map +0 -1
  81. package/dist/builders/update-builder.d.cts +0 -374
  82. package/dist/builders/update-builder.d.ts +0 -374
  83. package/dist/builders/update-builder.js +0 -666
  84. package/dist/builders/update-builder.js.map +0 -1
  85. package/dist/conditions.cjs.map +0 -1
  86. package/dist/conditions.js.map +0 -1
  87. package/dist/entity.cjs.map +0 -1
  88. package/dist/entity.js.map +0 -1
  89. package/dist/index.cjs.map +0 -1
  90. package/dist/index.js.map +0 -1
  91. package/dist/query-builder-D3URwK9k.d.cts +0 -477
  92. package/dist/query-builder-cfEkU0_w.d.ts +0 -477
  93. package/dist/standard-schema.cjs.map +0 -1
  94. package/dist/standard-schema.js.map +0 -1
  95. package/dist/table-ClST8nkR.d.cts +0 -276
  96. package/dist/table-vE3cGoDy.d.ts +0 -276
  97. package/dist/table.cjs.map +0 -1
  98. package/dist/table.js.map +0 -1
  99. package/dist/types.cjs.map +0 -1
  100. package/dist/types.js.map +0 -1
  101. package/dist/utils.cjs.map +0 -1
  102. package/dist/utils.js.map +0 -1
@@ -1,668 +0,0 @@
1
- 'use strict';
2
-
3
- // src/conditions.ts
4
- var createComparisonCondition = (type) => (attr, value) => ({
5
- type,
6
- attr,
7
- value
8
- });
9
- var eq = createComparisonCondition("eq");
10
- var ne = createComparisonCondition("ne");
11
- var lt = createComparisonCondition("lt");
12
- var lte = createComparisonCondition("lte");
13
- var gt = createComparisonCondition("gt");
14
- var gte = createComparisonCondition("gte");
15
- var between = (attr, lower, upper) => ({
16
- type: "between",
17
- attr,
18
- value: [lower, upper]
19
- });
20
- var inArray = (attr, values) => ({
21
- type: "in",
22
- attr,
23
- value: values
24
- });
25
- var beginsWith = createComparisonCondition("beginsWith");
26
- var contains = createComparisonCondition("contains");
27
- var attributeExists = (attr) => ({
28
- type: "attributeExists",
29
- attr
30
- });
31
- var attributeNotExists = (attr) => ({
32
- type: "attributeNotExists",
33
- attr
34
- });
35
- var and = (...conditions) => ({
36
- type: "and",
37
- conditions
38
- });
39
- var or = (...conditions) => ({
40
- type: "or",
41
- conditions
42
- });
43
- var not = (condition) => ({
44
- type: "not",
45
- condition
46
- });
47
-
48
- // src/expression.ts
49
- var generateAttributeName = (params, attr) => {
50
- if (attr.includes(".")) {
51
- const pathSegments = attr.split(".");
52
- const segmentNames = [];
53
- for (const segment of pathSegments) {
54
- let segmentName;
55
- for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
56
- if (existingAttr === segment) {
57
- segmentName = existingName;
58
- break;
59
- }
60
- }
61
- if (!segmentName) {
62
- segmentName = `#${Object.keys(params.expressionAttributeNames).length}`;
63
- params.expressionAttributeNames[segmentName] = segment;
64
- }
65
- segmentNames.push(segmentName);
66
- }
67
- return segmentNames.join(".");
68
- }
69
- for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
70
- if (existingAttr === attr) {
71
- return existingName;
72
- }
73
- }
74
- const attrName = `#${Object.keys(params.expressionAttributeNames).length}`;
75
- params.expressionAttributeNames[attrName] = attr;
76
- return attrName;
77
- };
78
- var generateValueName = (params, value) => {
79
- const valueName = `:${params.valueCounter.count++}`;
80
- params.expressionAttributeValues[valueName] = value;
81
- return valueName;
82
- };
83
- var validateCondition = (condition, requiresAttr = true, requiresValue = true) => {
84
- if (requiresAttr && !condition.attr) {
85
- throw new Error(`Attribute is required for ${condition.type} condition`);
86
- }
87
- if (requiresValue && condition.value === void 0) {
88
- throw new Error(`Value is required for ${condition.type} condition`);
89
- }
90
- };
91
- var buildComparisonExpression = (condition, operator, params) => {
92
- validateCondition(condition);
93
- if (!condition.attr) {
94
- throw new Error(`Attribute is required for ${condition.type} condition`);
95
- }
96
- const attrName = generateAttributeName(params, condition.attr);
97
- const valueName = generateValueName(params, condition.value);
98
- return `${attrName} ${operator} ${valueName}`;
99
- };
100
- var buildBetweenExpression = (condition, params) => {
101
- validateCondition(condition);
102
- if (!condition.attr) {
103
- throw new Error(`Attribute is required for ${condition.type} condition`);
104
- }
105
- if (!Array.isArray(condition.value) || condition.value.length !== 2) {
106
- throw new Error("Between condition requires an array of two values");
107
- }
108
- const attrName = generateAttributeName(params, condition.attr);
109
- const lowerName = generateValueName(params, condition.value[0]);
110
- const upperName = generateValueName(params, condition.value[1]);
111
- return `${attrName} BETWEEN ${lowerName} AND ${upperName}`;
112
- };
113
- var buildInExpression = (condition, params) => {
114
- validateCondition(condition);
115
- if (!condition.attr) {
116
- throw new Error(`Attribute is required for ${condition.type} condition`);
117
- }
118
- if (!Array.isArray(condition.value) || condition.value.length === 0) {
119
- throw new Error("In condition requires a non-empty array of values");
120
- }
121
- if (condition.value.length > 100) {
122
- throw new Error("In condition supports a maximum of 100 values");
123
- }
124
- const attrName = generateAttributeName(params, condition.attr);
125
- const valueNames = condition.value.map((value) => generateValueName(params, value));
126
- return `${attrName} IN (${valueNames.join(", ")})`;
127
- };
128
- var buildFunctionExpression = (functionName, condition, params) => {
129
- validateCondition(condition);
130
- if (!condition.attr) {
131
- throw new Error(`Attribute is required for ${condition.type} condition`);
132
- }
133
- const attrName = generateAttributeName(params, condition.attr);
134
- const valueName = generateValueName(params, condition.value);
135
- return `${functionName}(${attrName}, ${valueName})`;
136
- };
137
- var buildAttributeFunction = (functionName, condition, params) => {
138
- validateCondition(condition, true, false);
139
- if (!condition.attr) {
140
- throw new Error(`Attribute is required for ${condition.type} condition`);
141
- }
142
- const attrName = generateAttributeName(params, condition.attr);
143
- return `${functionName}(${attrName})`;
144
- };
145
- var buildLogicalExpression = (operator, conditions, params) => {
146
- if (!conditions || conditions.length === 0) {
147
- throw new Error(`At least one condition is required for ${operator} expression`);
148
- }
149
- const expressions = conditions.map((c) => buildExpression(c, params));
150
- return `(${expressions.join(` ${operator} `)})`;
151
- };
152
- var buildExpression = (condition, params) => {
153
- if (!condition) return "";
154
- try {
155
- const expressionBuilders = {
156
- eq: () => buildComparisonExpression(condition, "=", params),
157
- ne: () => buildComparisonExpression(condition, "<>", params),
158
- lt: () => buildComparisonExpression(condition, "<", params),
159
- lte: () => buildComparisonExpression(condition, "<=", params),
160
- gt: () => buildComparisonExpression(condition, ">", params),
161
- gte: () => buildComparisonExpression(condition, ">=", params),
162
- between: () => buildBetweenExpression(condition, params),
163
- in: () => buildInExpression(condition, params),
164
- beginsWith: () => buildFunctionExpression("begins_with", condition, params),
165
- contains: () => buildFunctionExpression("contains", condition, params),
166
- attributeExists: () => buildAttributeFunction("attribute_exists", condition, params),
167
- attributeNotExists: () => buildAttributeFunction("attribute_not_exists", condition, params),
168
- and: () => {
169
- if (!condition.conditions) {
170
- throw new Error("Conditions array is required for AND operator");
171
- }
172
- return buildLogicalExpression("AND", condition.conditions, params);
173
- },
174
- or: () => {
175
- if (!condition.conditions) {
176
- throw new Error("Conditions array is required for OR operator");
177
- }
178
- return buildLogicalExpression("OR", condition.conditions, params);
179
- },
180
- not: () => {
181
- if (!condition.condition) {
182
- throw new Error("Condition is required for NOT operator");
183
- }
184
- return `NOT (${buildExpression(condition.condition, params)})`;
185
- }
186
- };
187
- const builder = expressionBuilders[condition.type];
188
- if (!builder) {
189
- throw new Error(`Unknown condition type: ${condition.type}`);
190
- }
191
- return builder();
192
- } catch (error) {
193
- if (error instanceof Error) {
194
- console.error(`Error building expression for condition type ${condition.type}:`, error.message);
195
- } else {
196
- console.error(`Error building expression for condition type ${condition.type}:`, error);
197
- }
198
- throw error;
199
- }
200
- };
201
-
202
- // src/utils/debug-expression.ts
203
- function debugCommand(command) {
204
- const result = {};
205
- function replaceAliases(expressionString) {
206
- if (!expressionString) {
207
- return expressionString;
208
- }
209
- let replacedString = expressionString;
210
- for (const alias in command.expressionAttributeNames) {
211
- const attributeName = command.expressionAttributeNames[alias];
212
- const regex = new RegExp(alias, "g");
213
- replacedString = replacedString.replace(regex, attributeName);
214
- }
215
- for (const alias in command.expressionAttributeValues) {
216
- let attributeValue = command.expressionAttributeValues[alias];
217
- if (attributeValue instanceof Set) {
218
- const array = Array.from(attributeValue);
219
- attributeValue = `Set(${array.length}){${array.map((v) => JSON.stringify(v)).join(", ")}}`;
220
- } else {
221
- attributeValue = JSON.stringify(attributeValue);
222
- }
223
- const regex = new RegExp(alias, "g");
224
- replacedString = replacedString.replace(regex, attributeValue);
225
- }
226
- return replacedString;
227
- }
228
- if (command.updateExpression) {
229
- result.updateExpression = replaceAliases(command.updateExpression);
230
- }
231
- if (command.conditionExpression) {
232
- result.conditionExpression = replaceAliases(command.conditionExpression);
233
- }
234
- if (command.filterExpression) {
235
- result.filterExpression = replaceAliases(command.filterExpression);
236
- }
237
- if (command.keyConditionExpression) {
238
- result.keyConditionExpression = replaceAliases(command.keyConditionExpression);
239
- }
240
- if (command.projectionExpression) {
241
- result.projectionExpression = replaceAliases(command.projectionExpression);
242
- }
243
- return {
244
- raw: command,
245
- readable: result
246
- };
247
- }
248
-
249
- // src/builders/update-builder.ts
250
- var UpdateBuilder = class {
251
- updates = [];
252
- options = {
253
- returnValues: "ALL_NEW"
254
- };
255
- executor;
256
- tableName;
257
- key;
258
- constructor(executor, tableName, key) {
259
- this.executor = executor;
260
- this.tableName = tableName;
261
- this.key = key;
262
- }
263
- set(valuesOrPath, value) {
264
- if (typeof valuesOrPath === "object") {
265
- for (const [key, value2] of Object.entries(valuesOrPath)) {
266
- this.updates.push({
267
- type: "SET",
268
- path: key,
269
- value: value2
270
- });
271
- }
272
- } else {
273
- this.updates.push({
274
- type: "SET",
275
- path: valuesOrPath,
276
- value
277
- });
278
- }
279
- return this;
280
- }
281
- /**
282
- * Removes an attribute from the item.
283
- *
284
- * @example
285
- * ```typescript
286
- * // Remove simple attributes
287
- * builder
288
- * .remove('temporaryTag')
289
- * .remove('previousLocation');
290
- *
291
- * // Remove nested attributes
292
- * builder
293
- * .remove('metadata.testData')
294
- * .remove('stats.experimentalMetrics');
295
- * ```
296
- *
297
- * @param path - The path to the attribute to remove
298
- * @returns The builder instance for method chaining
299
- */
300
- remove(path) {
301
- this.updates.push({
302
- type: "REMOVE",
303
- path
304
- });
305
- return this;
306
- }
307
- /**
308
- * Adds a value to a number attribute or adds elements to a set.
309
- *
310
- * @example
311
- * ```typescript
312
- * // Increment counters
313
- * builder
314
- * .add('escapeAttempts', 1)
315
- * .add('feedingCount', 1);
316
- *
317
- * // Add to sets
318
- * builder
319
- * .add('knownBehaviors', new Set(['PACK_HUNTING', 'AMBUSH_TACTICS']))
320
- * .add('visitedZones', new Set(['ZONE_A', 'ZONE_B']));
321
- * ```
322
- *
323
- * @param path - The path to the attribute to update
324
- * @param value - The value to add (number or set)
325
- * @returns The builder instance for method chaining
326
- */
327
- add(path, value) {
328
- this.updates.push({
329
- type: "ADD",
330
- path,
331
- value
332
- });
333
- return this;
334
- }
335
- /**
336
- * Removes elements from a set attribute.
337
- *
338
- * @example
339
- * ```typescript
340
- * // Remove from sets using arrays
341
- * builder.deleteElementsFromSet(
342
- * 'allowedHabitats',
343
- * ['JUNGLE', 'COASTAL']
344
- * );
345
- *
346
- * // Remove from sets using Set DynamoItems
347
- * builder.deleteElementsFromSet(
348
- * 'knownBehaviors',
349
- * new Set(['NOCTURNAL', 'TERRITORIAL'])
350
- * );
351
- *
352
- * // Remove from nested sets
353
- * builder.deleteElementsFromSet(
354
- * 'stats.compatibleSpecies',
355
- * ['VELOCIRAPTOR', 'DILOPHOSAURUS']
356
- * );
357
- * ```
358
- *
359
- * @param path - The path to the set attribute
360
- * @param value - Elements to remove (array or Set)
361
- * @returns The builder instance for method chaining
362
- */
363
- deleteElementsFromSet(path, value) {
364
- let valuesToDelete;
365
- if (Array.isArray(value)) {
366
- valuesToDelete = new Set(value);
367
- } else {
368
- valuesToDelete = value;
369
- }
370
- this.updates.push({
371
- type: "DELETE",
372
- path,
373
- value: valuesToDelete
374
- });
375
- return this;
376
- }
377
- /**
378
- * Adds a condition that must be satisfied for the update to succeed.
379
- *
380
- * @example
381
- * ```typescript
382
- * // Simple condition
383
- * builder.condition(op =>
384
- * op.eq('status', 'ACTIVE')
385
- * );
386
- *
387
- * // Health check condition
388
- * builder.condition(op =>
389
- * op.and([
390
- * op.gt('health', 50),
391
- * op.eq('status', 'HUNTING')
392
- * ])
393
- * );
394
- *
395
- * // Complex security condition
396
- * builder.condition(op =>
397
- * op.and([
398
- * op.attributeExists('securitySystem'),
399
- * op.eq('containmentStatus', 'SECURE'),
400
- * op.lt('aggressionLevel', 8)
401
- * ])
402
- * );
403
- *
404
- * // Version check (optimistic locking)
405
- * builder.condition(op =>
406
- * op.eq('version', currentVersion)
407
- * );
408
- * ```
409
- *
410
- * @param condition - Either a Condition DynamoItem or a callback function that builds the condition
411
- * @returns The builder instance for method chaining
412
- */
413
- condition(condition) {
414
- if (typeof condition === "function") {
415
- const conditionOperator = {
416
- eq,
417
- ne,
418
- lt,
419
- lte,
420
- gt,
421
- gte,
422
- between,
423
- inArray,
424
- beginsWith,
425
- contains,
426
- attributeExists,
427
- attributeNotExists,
428
- and,
429
- or,
430
- not
431
- };
432
- this.options.condition = condition(conditionOperator);
433
- } else {
434
- this.options.condition = condition;
435
- }
436
- return this;
437
- }
438
- /**
439
- * Sets which item attributes to include in the response.
440
- *
441
- * Available options:
442
- * - ALL_NEW: All attributes after the update (default)
443
- * - UPDATED_NEW: Only updated attributes, new values
444
- * - ALL_OLD: All attributes before the update
445
- * - UPDATED_OLD: Only updated attributes, old values
446
- * - NONE: No attributes returned
447
- *
448
- * @example
449
- * ```typescript
450
- * // Get complete updated dinosaur
451
- * const result = await builder
452
- * .set('status', 'SLEEPING')
453
- * .returnValues('ALL_NEW')
454
- * .execute();
455
- *
456
- * // Track specific attribute changes
457
- * const result = await builder
458
- * .set({
459
- * 'stats.health': 100,
460
- * 'stats.energy': 95
461
- * })
462
- * .returnValues('UPDATED_OLD')
463
- * .execute();
464
- *
465
- * if (result.item) {
466
- * console.log('Previous health:', result.item.stats?.health);
467
- * }
468
- * ```
469
- *
470
- * @param returnValues - Which attributes to return in the response
471
- * @returns The builder instance for method chaining
472
- */
473
- returnValues(returnValues) {
474
- this.options.returnValues = returnValues;
475
- return this;
476
- }
477
- /**
478
- * Generate the DynamoDB command parameters
479
- */
480
- toDynamoCommand() {
481
- if (this.updates.length === 0) {
482
- throw new Error("No update actions specified");
483
- }
484
- const expressionParams = {
485
- expressionAttributeNames: {},
486
- expressionAttributeValues: {},
487
- valueCounter: { count: 0 }
488
- };
489
- let updateExpression = "";
490
- const setUpdates = [];
491
- const removeUpdates = [];
492
- const addUpdates = [];
493
- const deleteUpdates = [];
494
- for (const update of this.updates) {
495
- switch (update.type) {
496
- case "SET":
497
- setUpdates.push(update);
498
- break;
499
- case "REMOVE":
500
- removeUpdates.push(update);
501
- break;
502
- case "ADD":
503
- addUpdates.push(update);
504
- break;
505
- case "DELETE":
506
- deleteUpdates.push(update);
507
- break;
508
- }
509
- }
510
- if (setUpdates.length > 0) {
511
- updateExpression += "SET ";
512
- updateExpression += setUpdates.map((update) => {
513
- const attrName = generateAttributeName(expressionParams, update.path);
514
- const valueName = generateValueName(expressionParams, update.value);
515
- expressionParams.expressionAttributeValues[valueName] = update.value;
516
- return `${attrName} = ${valueName}`;
517
- }).join(", ");
518
- }
519
- if (removeUpdates.length > 0) {
520
- if (updateExpression) {
521
- updateExpression += " ";
522
- }
523
- updateExpression += "REMOVE ";
524
- updateExpression += removeUpdates.map((update) => {
525
- return generateAttributeName(expressionParams, update.path);
526
- }).join(", ");
527
- }
528
- if (addUpdates.length > 0) {
529
- if (updateExpression) {
530
- updateExpression += " ";
531
- }
532
- updateExpression += "ADD ";
533
- updateExpression += addUpdates.map((update) => {
534
- const attrName = generateAttributeName(expressionParams, update.path);
535
- const valueName = generateValueName(expressionParams, update.value);
536
- return `${attrName} ${valueName}`;
537
- }).join(", ");
538
- }
539
- if (deleteUpdates.length > 0) {
540
- if (updateExpression) {
541
- updateExpression += " ";
542
- }
543
- updateExpression += "DELETE ";
544
- updateExpression += deleteUpdates.map((update) => {
545
- const attrName = generateAttributeName(expressionParams, update.path);
546
- const valueName = generateValueName(expressionParams, update.value);
547
- return `${attrName} ${valueName}`;
548
- }).join(", ");
549
- }
550
- let conditionExpression;
551
- if (this.options.condition) {
552
- conditionExpression = buildExpression(this.options.condition, expressionParams);
553
- }
554
- const { expressionAttributeNames, expressionAttributeValues } = expressionParams;
555
- return {
556
- tableName: this.tableName,
557
- key: this.key,
558
- updateExpression,
559
- conditionExpression,
560
- expressionAttributeNames: Object.keys(expressionAttributeNames).length > 0 ? expressionAttributeNames : void 0,
561
- expressionAttributeValues: Object.keys(expressionAttributeValues).length > 0 ? expressionAttributeValues : void 0,
562
- returnValues: this.options.returnValues
563
- };
564
- }
565
- /**
566
- * Adds this update operation to a transaction.
567
- *
568
- * @example
569
- * ```typescript
570
- * const transaction = new TransactionBuilder(executor);
571
- *
572
- * // Update dinosaur status and habitat occupancy atomically
573
- * new UpdateBuilder(executor, 'dinosaurs', { id: 'TREX-001' })
574
- * .set('location', 'PADDOCK_A')
575
- * .set('status', 'CONTAINED')
576
- * .withTransaction(transaction);
577
- *
578
- * new UpdateBuilder(executor, 'habitats', { id: 'PADDOCK-A' })
579
- * .add('occupants', 1)
580
- * .set('lastOccupied', new Date().toISOString())
581
- * .withTransaction(transaction);
582
- *
583
- * // Execute all operations atomically
584
- * await transaction.execute();
585
- * ```
586
- *
587
- * @param transaction - The transaction builder to add this operation to
588
- * @returns The builder instance for method chaining
589
- */
590
- withTransaction(transaction) {
591
- const command = this.toDynamoCommand();
592
- transaction.updateWithCommand(command);
593
- }
594
- /**
595
- * Gets a human-readable representation of the update command.
596
- *
597
- * @example
598
- * ```typescript
599
- * // Create complex update
600
- * const builder = new UpdateBuilder(executor, 'dinosaurs', { id: 'RAPTOR-001' })
601
- * .set({
602
- * status: 'HUNTING',
603
- * 'stats.health': 95,
604
- * 'behavior.lastObserved': new Date().toISOString()
605
- * })
606
- * .add('huntingSuccesses', 1)
607
- * .condition(op => op.gt('health', 50));
608
- *
609
- * // Debug the update
610
- * const debugInfo = builder.debug();
611
- * console.log('Update operation:', debugInfo);
612
- * ```
613
- *
614
- * @returns A readable representation of the update command with resolved expressions
615
- */
616
- debug() {
617
- const command = this.toDynamoCommand();
618
- return debugCommand(command);
619
- }
620
- /**
621
- * Executes the update operation against DynamoDB.
622
- *
623
- * @example
624
- * ```typescript
625
- * try {
626
- * // Update dinosaur status with conditions
627
- * const result = await new UpdateBuilder(executor, 'dinosaurs', { id: 'TREX-001' })
628
- * .set({
629
- * status: 'FEEDING',
630
- * lastMeal: new Date().toISOString(),
631
- * 'stats.hunger': 0
632
- * })
633
- * .add('feedingCount', 1)
634
- * .condition(op =>
635
- * op.and([
636
- * op.gt('stats.hunger', 80),
637
- * op.eq('status', 'HUNTING')
638
- * ])
639
- * )
640
- * .returnValues('ALL_NEW')
641
- * .execute();
642
- *
643
- * if (result.item) {
644
- * console.log('Updated dinosaur:', result.item);
645
- * }
646
- * } catch (error) {
647
- * // Handle condition check failure
648
- * console.error('Failed to update dinosaur:', error);
649
- * // Check if dinosaur wasn't hungry enough
650
- * if (error.name === 'ConditionalCheckFailedException') {
651
- * console.log('Dinosaur not ready for feeding');
652
- * }
653
- * }
654
- * ```
655
- *
656
- * @returns A promise that resolves to an DynamoItem containing the updated item (if returnValues is set)
657
- * @throws {ConditionalCheckFailedException} If the condition check fails
658
- * @throws {Error} If the update operation fails for other reasons
659
- */
660
- async execute() {
661
- const params = this.toDynamoCommand();
662
- return this.executor(params);
663
- }
664
- };
665
-
666
- exports.UpdateBuilder = UpdateBuilder;
667
- //# sourceMappingURL=update-builder.cjs.map
668
- //# sourceMappingURL=update-builder.cjs.map