dynamo-bao 0.11.0 → 0.12.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/CHANGELOG.md CHANGED
@@ -9,6 +9,37 @@ Each version heading links to the tag that was published to npm. Entries before
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.12.0] - 2026-12-03
13
+
14
+ Semver minor (0.12.0). Adds the `quiet: true` option on `Model.updateOp()` and
15
+ `Model.update()` for bookkeeping updates that write only the named plain
16
+ attributes; nothing existing changes behavior.
17
+
18
+ **Key format: no key code changed.** Primary keys, GSI keys, unique
19
+ constraint keys, iteration keys, `_searchText`, and tenant prefixes are built
20
+ identically to 0.11.0.
21
+
22
+ ### Added
23
+
24
+ - **`quiet: true` option on `Model.updateOp()` and `Model.update()`**:
25
+ - Writes only the named plain attributes (`ADD`, `SET`, `REMOVE`), skipping
26
+ `updateBeforeSave` field stamps (`ModifiedDateField`, `VersionField`, etc.)
27
+ and `_iter_*` rewrites — avoiding unwanted `modifiedAt` bumps and
28
+ unnecessary GSI pre-reads when `modifiedAt` is indexed.
29
+ - Skips `beforeSave` and `afterSave` hooks (and any `afterSave` read-back in
30
+ `transact()`) for that op while leaving other ops in the same transaction
31
+ unaffected.
32
+ - Preserves `condition`, the `attribute_exists` existence guard on missing
33
+ items, and request batch context cache eviction/priming.
34
+ - Refuses with `ValidationError` before sending if any named field is a
35
+ primary key, index key, unique-constraint field, search-text source,
36
+ server-generated (`VersionField`), immutable (`CreateDateField`), unknown,
37
+ empty, or combined with `forceReindex: true`. Also refused on `create()`,
38
+ `createOp()`, and `save()`.
39
+ - Handles no-op `StringSetField` quiet updates cleanly via `ConditionCheck`
40
+ without emitting an empty `UpdateExpression`.
41
+ - Updated `tutorials/12-transactions.md`, `README.md`, and generated JSDoc.
42
+
12
43
  ## [0.11.0] - 2026-12-03
13
44
 
14
45
  Semver minor (0.11.0). Adds multi-model atomic transactions via
@@ -699,7 +730,8 @@ Initial release: single-table models with fields, GSIs, unique constraints,
699
730
  related fields, counters, TTL, filters, pagination, batch loading, hooks,
700
731
  YAML codegen and a table creation tool.
701
732
 
702
- [Unreleased]: https://github.com/aag1024/dynamo-bao/compare/v0.11.0...HEAD
733
+ [Unreleased]: https://github.com/aag1024/dynamo-bao/compare/v0.12.0...HEAD
734
+ [0.12.0]: https://github.com/aag1024/dynamo-bao/compare/v0.11.0...v0.12.0
703
735
  [0.11.0]: https://github.com/aag1024/dynamo-bao/compare/v0.10.0...v0.11.0
704
736
  [0.10.0]: https://github.com/aag1024/dynamo-bao/compare/v0.9.0...v0.10.0
705
737
  [0.9.0]: https://github.com/aag1024/dynamo-bao/compare/v0.8.2...v0.9.0
package/README.md CHANGED
@@ -233,7 +233,7 @@ try {
233
233
  Document.updateOp(
234
234
  docId,
235
235
  { directShareCount: "+1" },
236
- { condition: { directShareCount: { $lt: 1000 } } },
236
+ { condition: { directShareCount: { $lt: 1000 } }, quiet: true },
237
237
  ),
238
238
  DocumentPermission.createOp({ docId, userId, role: "editor" }),
239
239
  ]);
@@ -247,6 +247,7 @@ try {
247
247
  - All-or-nothing commit with unique-constraint items, GSI keys, iteration keys, generated fields, and tenant prefixes built identically to single-item writes.
248
248
  - Maps `TransactionCanceledException` reasons back to logical op indexes (including `ReturnValuesOnConditionCheckFailure` items) and retries `TransactionConflict` with jittered backoff.
249
249
  - Runs `afterSave`/`afterDelete` hooks once per op in order only after commit, invalidates the request batch context cache, and refuses mixed tenants, duplicate keys, or more than 100 items.
250
+ - Pass `quiet: true` on `updateOp` or `update` to write only named plain attributes without touching `modifiedAt`/`version` or firing `beforeSave`/`afterSave` hooks.
250
251
 
251
252
  See [tutorial 12](tutorials/12-transactions.md) for full details, options, error shapes, and limits.
252
253
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamo-bao",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
5
5
  "main": "src/index.js",
6
6
  "exports": {
@@ -67,6 +67,11 @@ const MutationMethods = {
67
67
  * @returns {Promise<Object>} Returns a promise that resolves to the created item.
68
68
  */
69
69
  async create(jsUpdates, options = {}) {
70
+ if (options.quiet) {
71
+ throw new ValidationError(
72
+ "quiet: true is only supported on update and updateOp",
73
+ );
74
+ }
70
75
  const createOptions = {
71
76
  isNew: true,
72
77
  ...options,
@@ -104,33 +109,47 @@ const MutationMethods = {
104
109
  * - Existence check: { fieldName: { $exists: true|false } }
105
110
  * - Logical operators: $and, $or, $not
106
111
  * @param {boolean} [options.forceReindex=false] - When true, repopulates all index attributes even if the source fields are unchanged.
112
+ * @param {boolean} [options.quiet=false] - When true, writes only the named plain attributes: skips `beforeSave`/`afterSave` hooks, `updateBeforeSave` field stamps (`ModifiedDateField`, `VersionField`, ...), and `_iter_*` rewrites. Refused for primary key, index key, unique-constraint, search-text source, server-generated, or immutable fields.
107
113
  * @returns {Promise<Object>} Returns a promise that resolves to the updated item.
108
114
  * @throws {Error} "Item not found" if the item doesn't exist
109
115
  * @throws {Error} "Condition check failed" if the condition isn't satisfied
110
116
  */
111
117
  async update(primaryId, jsUpdates, options = {}) {
118
+ if (options.quiet) {
119
+ if (options.isNew) {
120
+ throw new ValidationError(
121
+ "quiet: true is only supported on update and updateOp",
122
+ );
123
+ }
124
+ this._assertQuietUpdateAllowed(jsUpdates, options);
125
+ }
126
+
112
127
  const updateOptions = {
113
128
  isNew: false,
114
129
  ...options,
115
130
  };
116
131
 
117
- await pluginManager.executeHooks(
118
- this.name,
119
- "beforeSave",
120
- jsUpdates,
121
- updateOptions,
122
- );
132
+ if (!updateOptions.quiet) {
133
+ await pluginManager.executeHooks(
134
+ this.name,
135
+ "beforeSave",
136
+ jsUpdates,
137
+ updateOptions,
138
+ );
139
+ }
123
140
 
124
141
  const result = await this._saveItem(primaryId, jsUpdates, updateOptions);
125
142
 
126
143
  logger.debug("update() - result", result);
127
144
 
128
- await pluginManager.executeHooks(
129
- this.name,
130
- "afterSave",
131
- result,
132
- updateOptions,
133
- );
145
+ if (!updateOptions.quiet) {
146
+ await pluginManager.executeHooks(
147
+ this.name,
148
+ "afterSave",
149
+ result,
150
+ updateOptions,
151
+ );
152
+ }
134
153
 
135
154
  return result;
136
155
  },
@@ -584,6 +603,11 @@ const MutationMethods = {
584
603
  if (!jsUpdates || typeof jsUpdates !== "object" || Array.isArray(jsUpdates)) {
585
604
  throw new ValidationError("createOp() requires a data object");
586
605
  }
606
+ if (options.quiet) {
607
+ throw new ValidationError(
608
+ "quiet: true is only supported on update and updateOp",
609
+ );
610
+ }
587
611
  const tenantId = this.manager.getTenantId();
588
612
  const model = this;
589
613
  const table = this.table;
@@ -653,6 +677,9 @@ const MutationMethods = {
653
677
  if (!jsUpdates || typeof jsUpdates !== "object" || Array.isArray(jsUpdates)) {
654
678
  throw new ValidationError("updateOp() requires an updates object");
655
679
  }
680
+ if (options.quiet) {
681
+ this._assertQuietUpdateAllowed(jsUpdates, options);
682
+ }
656
683
  const tenantId = this.manager.getTenantId();
657
684
  const model = this;
658
685
  const table = this.table;
@@ -670,24 +697,39 @@ const MutationMethods = {
670
697
  async _prepare() {
671
698
  const hookOptions = { isNew: false, ...opOptions };
672
699
  const working = { ...updatesCopy };
673
- await pluginManager.executeHooks(
674
- model.name,
675
- "beforeSave",
676
- working,
677
- hookOptions,
678
- );
700
+ if (!opOptions.quiet) {
701
+ await pluginManager.executeHooks(
702
+ model.name,
703
+ "beforeSave",
704
+ working,
705
+ hookOptions,
706
+ );
707
+ }
679
708
  const prepared = await model._prepareSave(
680
709
  primaryId,
681
710
  working,
682
711
  hookOptions,
683
712
  );
713
+ const mainTransactItem = prepared.updateParams.UpdateExpression
714
+ ? withReturnValuesOnFailure({ Update: prepared.updateParams })
715
+ : {
716
+ ConditionCheck: model._updateParamsToConditionCheck(
717
+ prepared.updateParams,
718
+ ),
719
+ };
684
720
  const transactItems = [
685
721
  ...prepared.uniqueTransactItems.map(withReturnValuesOnFailure),
686
- withReturnValuesOnFailure({ Update: prepared.updateParams }),
722
+ mainTransactItem,
687
723
  ];
688
724
  const itemMeta = [
689
725
  ...prepared.uniqueItemMeta,
690
- { role: "main", hasExistsGuard: prepared.existsGuard },
726
+ {
727
+ role: "main",
728
+ hasExistsGuard:
729
+ prepared.existsGuard ||
730
+ (!prepared.updateParams.UpdateExpression &&
731
+ !prepared.updateParams.ConditionExpression),
732
+ },
691
733
  ];
692
734
  const localResult =
693
735
  prepared.currentItem && !prepared.hasAtomicOps
@@ -708,6 +750,7 @@ const MutationMethods = {
708
750
  localResult,
709
751
  hookOptions,
710
752
  hasAtomicOps: prepared.hasAtomicOps,
753
+ quiet: Boolean(opOptions.quiet),
711
754
  };
712
755
  },
713
756
  };
@@ -856,6 +899,41 @@ const MutationMethods = {
856
899
  };
857
900
  },
858
901
 
902
+ /**
903
+ * Convert an updateParams with an empty UpdateExpression (e.g. a quiet
904
+ * update on an unchanged StringSetField) into a ConditionCheck that enforces
905
+ * the caller's condition and/or item existence without writing attributes.
906
+ * @private
907
+ */
908
+ _updateParamsToConditionCheck(updateParams) {
909
+ const checkParams = {
910
+ TableName: updateParams.TableName,
911
+ Key: updateParams.Key,
912
+ };
913
+ if (updateParams.ConditionExpression) {
914
+ checkParams.ConditionExpression = updateParams.ConditionExpression;
915
+ }
916
+ if (updateParams.ExpressionAttributeNames) {
917
+ checkParams.ExpressionAttributeNames = {
918
+ ...updateParams.ExpressionAttributeNames,
919
+ };
920
+ }
921
+ if (updateParams.ExpressionAttributeValues) {
922
+ checkParams.ExpressionAttributeValues = {
923
+ ...updateParams.ExpressionAttributeValues,
924
+ };
925
+ }
926
+ if (!checkParams.ConditionExpression) {
927
+ this._addExistsGuard(checkParams);
928
+ } else {
929
+ checkParams.ReturnValuesOnConditionCheckFailure = "ALL_OLD";
930
+ }
931
+ return checkParams;
932
+ },
933
+
934
+ /**
935
+ * @private
936
+ */
859
937
  _buildLocalSavedItem(isNew, currentItem, dyKey, dyUpdatesToSave) {
860
938
  const base = isNew
861
939
  ? {}
@@ -874,8 +952,119 @@ const MutationMethods = {
874
952
  return this._createFromDyItem(storedItem);
875
953
  },
876
954
 
955
+ /**
956
+ * Validate that a `quiet: true` update touches only plain writable
957
+ * attributes — refusing primary keys, index keys, unique-constraint fields,
958
+ * search-text source fields, server-generated/immutable fields, unknown
959
+ * fields, empty updates, and `forceReindex`.
960
+ * @private
961
+ */
962
+ _assertQuietUpdateAllowed(jsUpdates, options = {}) {
963
+ if (options.forceReindex) {
964
+ throw new ValidationError(
965
+ "quiet: true cannot be combined with forceReindex: true",
966
+ );
967
+ }
968
+
969
+ if (!jsUpdates || typeof jsUpdates !== "object" || Array.isArray(jsUpdates)) {
970
+ throw new ValidationError("quiet: true requires an updates object");
971
+ }
972
+
973
+ const fieldNames = Object.keys(jsUpdates).filter(
974
+ (k) => jsUpdates[k] !== undefined,
975
+ );
976
+ if (fieldNames.length === 0) {
977
+ throw new ValidationError(
978
+ "quiet: true requires at least one named field to update",
979
+ );
980
+ }
981
+
982
+ const pkFields = new Set(
983
+ [this.primaryKey?.pk, this.primaryKey?.sk].filter(
984
+ (f) => f && f !== "modelPrefix",
985
+ ),
986
+ );
987
+ const indexFields = new Set();
988
+ for (const index of Object.values(this.indexes || {})) {
989
+ if (index.indexId === undefined) continue;
990
+ if (index.pk && index.pk !== "modelPrefix") indexFields.add(index.pk);
991
+ if (index.sk && index.sk !== "modelPrefix") indexFields.add(index.sk);
992
+ }
993
+ const uniqueFields = new Set(
994
+ Object.values(this.uniqueConstraints || {}).map((c) => c.field),
995
+ );
996
+ const searchFields = new Set(
997
+ this.searchable && this.searchConfig?.fields
998
+ ? this.searchConfig.fields
999
+ : [],
1000
+ );
1001
+
1002
+ for (const fieldName of fieldNames) {
1003
+ if (!Object.prototype.hasOwnProperty.call(this.fields, fieldName)) {
1004
+ throw new ValidationError(
1005
+ `Unknown field "${fieldName}" on ${this.name} cannot be updated with quiet: true`,
1006
+ fieldName,
1007
+ );
1008
+ }
1009
+ const field = this.fields[fieldName];
1010
+ if (pkFields.has(fieldName)) {
1011
+ throw new ValidationError(
1012
+ `Primary key field "${fieldName}" cannot be updated with quiet: true`,
1013
+ fieldName,
1014
+ );
1015
+ }
1016
+ if (indexFields.has(fieldName)) {
1017
+ throw new ValidationError(
1018
+ `Index key field "${fieldName}" cannot be updated with quiet: true`,
1019
+ fieldName,
1020
+ );
1021
+ }
1022
+ if (uniqueFields.has(fieldName)) {
1023
+ throw new ValidationError(
1024
+ `Unique constraint field "${fieldName}" cannot be updated with quiet: true`,
1025
+ fieldName,
1026
+ );
1027
+ }
1028
+ if (searchFields.has(fieldName)) {
1029
+ throw new ValidationError(
1030
+ `Search-text source field "${fieldName}" cannot be updated with quiet: true`,
1031
+ fieldName,
1032
+ );
1033
+ }
1034
+ if (field.serverGenerated) {
1035
+ throw new ValidationError(
1036
+ `Server-generated field "${fieldName}" cannot be updated with quiet: true`,
1037
+ fieldName,
1038
+ );
1039
+ }
1040
+ if (field.immutable) {
1041
+ throw new ValidationError(
1042
+ `Immutable field "${fieldName}" cannot be updated with quiet: true`,
1043
+ fieldName,
1044
+ );
1045
+ }
1046
+ }
1047
+ },
1048
+
1049
+ /**
1050
+ * @private
1051
+ */
877
1052
  async _prepareSave(primaryId, jsUpdates, options = {}) {
878
- const { isNew = false, instanceObj = null, forceReindex = false } = options;
1053
+ const {
1054
+ isNew = false,
1055
+ instanceObj = null,
1056
+ forceReindex = false,
1057
+ quiet = false,
1058
+ } = options;
1059
+
1060
+ if (quiet) {
1061
+ if (isNew) {
1062
+ throw new ValidationError(
1063
+ "quiet: true is only supported on update and updateOp",
1064
+ );
1065
+ }
1066
+ this._assertQuietUpdateAllowed(jsUpdates, options);
1067
+ }
879
1068
 
880
1069
  // The id is used as a string below (iteration keys, the relatedId on
881
1070
  // unique constraint rows), so accept an object id by normalizing it.
@@ -919,7 +1108,7 @@ const MutationMethods = {
919
1108
  const dyValue = field.toDy(jsUpdates[key]);
920
1109
  dyUpdatesToSave[key] = dyValue;
921
1110
  indexComputationData[key] = dyValue;
922
- } else {
1111
+ } else if (!quiet) {
923
1112
  if (typeof field.updateBeforeSave === "function") {
924
1113
  const newValue = field.updateBeforeSave(jsUpdates[key]);
925
1114
  if (newValue !== jsUpdates[key]) {
@@ -1122,35 +1311,41 @@ const MutationMethods = {
1122
1311
  }
1123
1312
  }
1124
1313
 
1125
- // Add GSI keys
1126
- const indexSourceData = Object.keys(indexComputationData).length
1127
- ? indexComputationData
1128
- : dyUpdatesToSave;
1314
+ // Add GSI keys, iteration keys, and _searchText (skipped on quiet updates,
1315
+ // which are validated to touch only plain non-indexed, non-searchable fields).
1316
+ if (!quiet) {
1317
+ const indexSourceData = Object.keys(indexComputationData).length
1318
+ ? indexComputationData
1319
+ : dyUpdatesToSave;
1129
1320
 
1130
- const indexKeys = this._getIndexKeys(indexSourceData, {
1131
- isUpdate: !isNew,
1132
- });
1133
- logger.debug("indexKeys", indexKeys);
1134
- Object.assign(dyUpdatesToSave, indexKeys);
1321
+ const indexKeys = this._getIndexKeys(indexSourceData, {
1322
+ isUpdate: !isNew,
1323
+ });
1324
+ logger.debug("indexKeys", indexKeys);
1325
+ Object.assign(dyUpdatesToSave, indexKeys);
1135
1326
 
1136
- // Add iteration keys if model is iterable
1137
- if (this.iterable) {
1138
- const iterationKeys = this._getIterationKeys(primaryId, dyUpdatesToSave);
1139
- Object.assign(dyUpdatesToSave, iterationKeys);
1140
- }
1327
+ // Add iteration keys if model is iterable
1328
+ if (this.iterable) {
1329
+ const iterationKeys = this._getIterationKeys(
1330
+ primaryId,
1331
+ dyUpdatesToSave,
1332
+ );
1333
+ Object.assign(dyUpdatesToSave, iterationKeys);
1334
+ }
1141
1335
 
1142
- // Compute _searchText if the model is searchable. Returns undefined to
1143
- // skip the write entirely, null to REMOVE, or a string to SET.
1144
- if (this.searchable && this.searchConfig) {
1145
- const searchTextValue = computeSearchTextUpdate({
1146
- searchConfig: this.searchConfig,
1147
- dyUpdatesToSave,
1148
- currentItem,
1149
- isNew,
1150
- forceReindex,
1151
- });
1152
- if (searchTextValue !== undefined) {
1153
- dyUpdatesToSave[SEARCH_TEXT_FIELD] = searchTextValue;
1336
+ // Compute _searchText if the model is searchable. Returns undefined to
1337
+ // skip the write entirely, null to REMOVE, or a string to SET.
1338
+ if (this.searchable && this.searchConfig) {
1339
+ const searchTextValue = computeSearchTextUpdate({
1340
+ searchConfig: this.searchConfig,
1341
+ dyUpdatesToSave,
1342
+ currentItem,
1343
+ isNew,
1344
+ forceReindex,
1345
+ });
1346
+ if (searchTextValue !== undefined) {
1347
+ dyUpdatesToSave[SEARCH_TEXT_FIELD] = searchTextValue;
1348
+ }
1154
1349
  }
1155
1350
  }
1156
1351
 
@@ -1330,6 +1525,32 @@ const MutationMethods = {
1330
1525
  try {
1331
1526
  let response;
1332
1527
 
1528
+ if (!updateParams.UpdateExpression) {
1529
+ // A quiet update whose diff produced no write expression (for example,
1530
+ // an unchanged StringSetField). Evaluate any caller condition via
1531
+ // ConditionCheck without mutating the item.
1532
+ if (updateParams.ConditionExpression) {
1533
+ const checkParams = this._updateParamsToConditionCheck(updateParams);
1534
+ const clientRequestToken = ulid();
1535
+ const checkResp = await retryOperation(() =>
1536
+ this.documentClient.send(
1537
+ new TransactWriteCommand({
1538
+ TransactItems: [{ ConditionCheck: checkParams }],
1539
+ ClientRequestToken: clientRequestToken,
1540
+ ReturnConsumedCapacity: "TOTAL",
1541
+ }),
1542
+ ),
1543
+ );
1544
+ currentItem._addConsumedCapacity(
1545
+ checkResp.ConsumedCapacity,
1546
+ "write",
1547
+ false,
1548
+ );
1549
+ }
1550
+ this._primeLoaderContext(primaryId, currentItem);
1551
+ return currentItem;
1552
+ }
1553
+
1333
1554
  if (hasUniqueConstraintChanges) {
1334
1555
  // Use transaction if we have unique constraint changes
1335
1556
  const transactItems = [
package/src/model.js CHANGED
@@ -1449,6 +1449,11 @@ class BaoModel {
1449
1449
  */
1450
1450
  async save(options = {}) {
1451
1451
  assertAttached(this, "save");
1452
+ if (options.quiet) {
1453
+ throw new ValidationError(
1454
+ "quiet: true is only supported on update and updateOp",
1455
+ );
1456
+ }
1452
1457
  const { forceReindex = false, ...otherOptions } = options;
1453
1458
 
1454
1459
  if (!forceReindex && !this.hasChanges() && this.isLoaded()) {
package/src/transact.js CHANGED
@@ -323,7 +323,10 @@ async function transact(ops, options = {}) {
323
323
  results.push(prepared.localResult);
324
324
  } else if (prepared.type === "update") {
325
325
  let result = prepared.localResult;
326
- if (pluginManager.hasHook(prepared.model.name, "afterSave")) {
326
+ if (
327
+ !prepared.quiet &&
328
+ pluginManager.hasHook(prepared.model.name, "afterSave")
329
+ ) {
327
330
  if (!result) {
328
331
  result = await runInCapturedTenant(prepared.tenantId, () =>
329
332
  prepared.model.find(prepared.primaryId, {