dynamo-bao 0.8.1 → 0.8.2

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/CHANGELOG.md CHANGED
@@ -9,6 +9,26 @@ Each version heading links to the tag that was published to npm. Entries before
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.8.2] - 2026-10-27
13
+
14
+ ### Fixed
15
+
16
+ - A conditional `update()` or `delete()` of a row that did not exist was
17
+ reported as `ConditionalError` instead of `ItemNotFoundError` when another
18
+ writer created the row right after the failed write. Since 0.8.0 the
19
+ library read the row after a failed condition check to tell the two apart,
20
+ and that read described a later state. Guarded writes now send
21
+ `ReturnValuesOnConditionCheckFailure`, so the answer comes from the state
22
+ DynamoDB evaluated the condition against. The follow-up read is gone, which
23
+ makes the failure path one call cheaper. It remains only as a fallback for
24
+ injected clients that do not surface the returned item.
25
+
26
+ ### Changed
27
+
28
+ - The bundled DynamoDB client attaches `Item` to a
29
+ `ConditionalCheckFailedException` when the request asked for
30
+ `ReturnValuesOnConditionCheckFailure` (`null` when the row did not exist).
31
+
12
32
  ## [0.8.1] - 2026-10-27
13
33
 
14
34
  ### Fixed
@@ -467,7 +487,8 @@ Initial release: single-table models with fields, GSIs, unique constraints,
467
487
  related fields, counters, TTL, filters, pagination, batch loading, hooks,
468
488
  YAML codegen and a table creation tool.
469
489
 
470
- [Unreleased]: https://github.com/aag1024/dynamo-bao/compare/v0.8.1...HEAD
490
+ [Unreleased]: https://github.com/aag1024/dynamo-bao/compare/v0.8.2...HEAD
491
+ [0.8.2]: https://github.com/aag1024/dynamo-bao/compare/v0.8.1...v0.8.2
471
492
  [0.8.1]: https://github.com/aag1024/dynamo-bao/compare/v0.8.0...v0.8.1
472
493
  [0.8.0]: https://github.com/aag1024/dynamo-bao/compare/v0.7.3...v0.8.0
473
494
  [0.7.3]: https://github.com/aag1024/dynamo-bao/compare/v0.7.2...v0.7.3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamo-bao",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
5
5
  "main": "src/index.js",
6
6
  "exports": {
@@ -240,7 +240,16 @@ class DynamoDBClient {
240
240
  if (errorName === 'TransactionCanceledException' && errorBody.CancellationReasons) {
241
241
  error.CancellationReasons = errorBody.CancellationReasons;
242
242
  }
243
-
243
+
244
+ // With ReturnValuesOnConditionCheckFailure, a failed condition check
245
+ // carries the item as it was when the condition was evaluated, or no
246
+ // Item at all when the row did not exist. Surface that as error.Item
247
+ // (null for "did not exist") so callers can tell the two apart
248
+ // without a follow-up read.
249
+ if (command.input.ReturnValuesOnConditionCheckFailure) {
250
+ error.Item = errorBody.Item ? unmarshallItem(errorBody.Item) : null;
251
+ }
252
+
244
253
  throw error;
245
254
  }
246
255
 
@@ -522,6 +531,14 @@ class DynamoDBDocumentClient {
522
531
  }
523
532
 
524
533
  // Marshall JavaScript values to DynamoDB JSON
534
+ function unmarshallItem(item) {
535
+ const out = {};
536
+ Object.keys(item).forEach(key => {
537
+ out[key] = unmarshall(item[key]);
538
+ });
539
+ return out;
540
+ }
541
+
525
542
  function marshall(obj) {
526
543
  if (obj === null) return { NULL: true };
527
544
  if (obj === undefined) return { NULL: true };
@@ -294,7 +294,7 @@ const MutationMethods = {
294
294
  ) {
295
295
  if (
296
296
  !item &&
297
- !(await this._existsAfterGuardFailure(primaryId, options))
297
+ !(await this._existsAfterGuardFailure(primaryId, options, error))
298
298
  ) {
299
299
  throw new ItemNotFoundError("Item not found", primaryId);
300
300
  }
@@ -336,18 +336,26 @@ const MutationMethods = {
336
336
  params.ConditionExpression,
337
337
  "attribute_exists(#__existsPk)",
338
338
  );
339
+ // Ask DynamoDB to return the row the condition was evaluated against,
340
+ // so a failed check can be attributed without a follow-up read (see
341
+ // _existsAfterGuardFailure).
342
+ params.ReturnValuesOnConditionCheckFailure = "ALL_OLD";
339
343
  },
340
344
 
341
345
  /**
342
346
  * After a guarded write fails its condition check, decide whether the row
343
- * exists. With no caller condition the guard is the only thing that could
344
- * have failed, so no read is needed; otherwise one GetItem (cache
345
- * bypassed) tells a missing row apart from an unmet condition. Only the
346
- * failure path pays for this read.
347
+ * existed when the condition was evaluated. With no caller condition the
348
+ * guard is the only thing that could have failed, so the row was missing.
349
+ * Otherwise the error's `Item` (from ReturnValuesOnConditionCheckFailure)
350
+ * is exact: present means the caller's condition failed, null means the
351
+ * row did not exist. A read after the fact is only a fallback for clients
352
+ * that do not surface `Item`; it can be wrong when another writer creates
353
+ * the row between the failed write and the read.
347
354
  * @private
348
355
  */
349
- async _existsAfterGuardFailure(primaryId, options) {
356
+ async _existsAfterGuardFailure(primaryId, options, error) {
350
357
  if (!options.condition) return false;
358
+ if (error && error.Item !== undefined) return error.Item !== null;
351
359
  const existing = await this.find(primaryId, {
352
360
  batchDelay: 0,
353
361
  bypassCache: true,
@@ -1020,7 +1028,7 @@ const MutationMethods = {
1020
1028
  if (error.name === "ConditionalCheckFailedException") {
1021
1029
  if (
1022
1030
  existsGuard &&
1023
- !(await this._existsAfterGuardFailure(primaryId, options))
1031
+ !(await this._existsAfterGuardFailure(primaryId, options, error))
1024
1032
  ) {
1025
1033
  throw new ItemNotFoundError("Item not found", primaryId);
1026
1034
  }