dynamo-bao 0.7.1 → 0.7.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/package.json +9 -9
- package/src/mixins/batch-loading-mixin.js +1 -2
- package/src/mixins/mutation-mixin.js +65 -12
- package/src/model.js +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dynamo-bao",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"repository": {
|
|
34
34
|
"type": "git",
|
|
35
|
-
"url": "https://github.com/aag1024/dynamo-bao"
|
|
35
|
+
"url": "git+https://github.com/aag1024/dynamo-bao.git"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"aws4fetch": "^1.0.20",
|
|
@@ -53,12 +53,12 @@
|
|
|
53
53
|
"prettier": "3.4.2"
|
|
54
54
|
},
|
|
55
55
|
"bin": {
|
|
56
|
-
"bao-init": "
|
|
57
|
-
"bao-create-table": "
|
|
58
|
-
"bao-codegen": "
|
|
59
|
-
"bao-watch": "
|
|
60
|
-
"bao-update-table": "
|
|
61
|
-
"bao-delete": "
|
|
62
|
-
"bao-rebuild-search-text": "
|
|
56
|
+
"bao-init": "bin/dynamo-bao-create-table.js",
|
|
57
|
+
"bao-create-table": "bin/bao-create-table.js",
|
|
58
|
+
"bao-codegen": "bin/codegen.js",
|
|
59
|
+
"bao-watch": "bin/watch.js",
|
|
60
|
+
"bao-update-table": "bin/dynamo-bao-update-table.js",
|
|
61
|
+
"bao-delete": "bin/dynamo-bao-delete-table.js",
|
|
62
|
+
"bao-rebuild-search-text": "bin/bao-rebuild-search-text.js"
|
|
63
63
|
}
|
|
64
64
|
}
|
|
@@ -122,11 +122,10 @@ const BatchLoadingMethods = {
|
|
|
122
122
|
* @private
|
|
123
123
|
*/
|
|
124
124
|
_loaderKey(primaryId) {
|
|
125
|
-
const pkSk = this._parsePrimaryId(primaryId);
|
|
126
125
|
return JSON.stringify([
|
|
127
126
|
this.name,
|
|
128
127
|
this.manager.getTenantId() ?? null,
|
|
129
|
-
this.
|
|
128
|
+
this._canonicalPrimaryId(primaryId),
|
|
130
129
|
]);
|
|
131
130
|
},
|
|
132
131
|
|
|
@@ -283,7 +283,7 @@ const MutationMethods = {
|
|
|
283
283
|
* @param {string} primaryId - The record's logical primary id.
|
|
284
284
|
* @param {Object} [options]
|
|
285
285
|
* @param {boolean} [options.dryRun=false] - Inspect only; perform no writes.
|
|
286
|
-
* @returns {Promise<{relocated: boolean, from: ?string, to: string, reason
|
|
286
|
+
* @returns {Promise<{relocated: boolean, from: ?string, to: string, reason: (string|undefined)}>}
|
|
287
287
|
*/
|
|
288
288
|
async relocateToCanonicalKey(primaryId, options = {}) {
|
|
289
289
|
const { dryRun = false } = options;
|
|
@@ -421,6 +421,12 @@ const MutationMethods = {
|
|
|
421
421
|
forceReindex = false,
|
|
422
422
|
} = options;
|
|
423
423
|
|
|
424
|
+
// The id is used as a string below (iteration keys, the relatedId on
|
|
425
|
+
// unique constraint rows), so accept an object id by normalizing it.
|
|
426
|
+
if (!isNew) {
|
|
427
|
+
primaryId = this._canonicalPrimaryId(primaryId);
|
|
428
|
+
}
|
|
429
|
+
|
|
424
430
|
logger.debug("saveItem", primaryId, isNew, instanceObj);
|
|
425
431
|
let consumedCapacity = [];
|
|
426
432
|
|
|
@@ -477,6 +483,44 @@ const MutationMethods = {
|
|
|
477
483
|
}
|
|
478
484
|
}
|
|
479
485
|
|
|
486
|
+
// A relative counter ("+3") is written with ADD, which DynamoDB applies
|
|
487
|
+
// at write time, but an index key built from it needs the resulting
|
|
488
|
+
// number. On a new item ADD just stores the delta, so write that. On an
|
|
489
|
+
// existing item, a counter that's part of an index key is written as
|
|
490
|
+
// the loaded value plus the delta, guarded on the loaded value, so a
|
|
491
|
+
// concurrent change fails the update (ConditionalError) instead of
|
|
492
|
+
// leaving the index key stale. Other counters keep the atomic ADD.
|
|
493
|
+
const counterGuards = [];
|
|
494
|
+
const { CounterFieldClass } = require("../fields");
|
|
495
|
+
for (const [key, field] of Object.entries(this.fields)) {
|
|
496
|
+
const dyValue = dyUpdatesToSave[key];
|
|
497
|
+
if (
|
|
498
|
+
!(field instanceof CounterFieldClass) ||
|
|
499
|
+
typeof dyValue !== "string"
|
|
500
|
+
) {
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
const delta = parseInt(dyValue, 10);
|
|
504
|
+
let value;
|
|
505
|
+
if (isNew) {
|
|
506
|
+
value = delta;
|
|
507
|
+
} else if (
|
|
508
|
+
Object.values(this.indexes).some(
|
|
509
|
+
(index) =>
|
|
510
|
+
index.indexId !== undefined &&
|
|
511
|
+
(index.pk === key || index.sk === key),
|
|
512
|
+
)
|
|
513
|
+
) {
|
|
514
|
+
const loadedValue = currentItem._loadedDyData[key];
|
|
515
|
+
value = Number(loadedValue ?? 0) + delta;
|
|
516
|
+
counterGuards.push({ fieldName: key, loadedValue });
|
|
517
|
+
} else {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
dyUpdatesToSave[key] = value;
|
|
521
|
+
indexComputationData[key] = value;
|
|
522
|
+
}
|
|
523
|
+
|
|
480
524
|
// Backfill from currentItem for GSI consistency
|
|
481
525
|
if (!isNew && currentItem) {
|
|
482
526
|
if (forceReindex) {
|
|
@@ -725,6 +769,26 @@ const MutationMethods = {
|
|
|
725
769
|
: "attribute_exists(#__migExistsPk)";
|
|
726
770
|
}
|
|
727
771
|
|
|
772
|
+
for (const { fieldName, loadedValue } of counterGuards) {
|
|
773
|
+
const name = `#__counterGuard_${fieldName}`;
|
|
774
|
+
const value = `:__counterGuard_${fieldName}`;
|
|
775
|
+
updateParams.ExpressionAttributeNames = {
|
|
776
|
+
...updateParams.ExpressionAttributeNames,
|
|
777
|
+
[name]: fieldName,
|
|
778
|
+
};
|
|
779
|
+
let guard = `attribute_not_exists(${name})`;
|
|
780
|
+
if (loadedValue != null) {
|
|
781
|
+
guard = `${name} = ${value}`;
|
|
782
|
+
updateParams.ExpressionAttributeValues = {
|
|
783
|
+
...updateParams.ExpressionAttributeValues,
|
|
784
|
+
[value]: loadedValue,
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
updateParams.ConditionExpression = updateParams.ConditionExpression
|
|
788
|
+
? `(${updateParams.ConditionExpression}) AND ${guard}`
|
|
789
|
+
: guard;
|
|
790
|
+
}
|
|
791
|
+
|
|
728
792
|
const dyKey = writeKey;
|
|
729
793
|
logger.debug("dyKey", dyKey);
|
|
730
794
|
|
|
@@ -763,17 +827,6 @@ const MutationMethods = {
|
|
|
763
827
|
storedItem[key] = value;
|
|
764
828
|
}
|
|
765
829
|
}
|
|
766
|
-
// A relative counter ("+3") is written with ADD, which on a new
|
|
767
|
-
// item stores the number itself.
|
|
768
|
-
const { CounterFieldClass } = require("../fields");
|
|
769
|
-
for (const [key, field] of Object.entries(this.fields)) {
|
|
770
|
-
if (
|
|
771
|
-
field instanceof CounterFieldClass &&
|
|
772
|
-
typeof storedItem[key] === "string"
|
|
773
|
-
) {
|
|
774
|
-
storedItem[key] = parseInt(storedItem[key], 10);
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
830
|
savedItem = this._createFromDyItem(storedItem);
|
|
778
831
|
} else {
|
|
779
832
|
const readOptions = { batchDelay: 0, bypassCache: true };
|
package/src/model.js
CHANGED
|
@@ -1119,6 +1119,17 @@ class BaoModel {
|
|
|
1119
1119
|
return this.constructor.getPrimaryId(this._dyData);
|
|
1120
1120
|
}
|
|
1121
1121
|
|
|
1122
|
+
/**
|
|
1123
|
+
* The string form of a primary id given as a string or an object (either
|
|
1124
|
+
* `{ pk, sk }` or the key field values), as {@link BaoModel#getPrimaryId}
|
|
1125
|
+
* returns it.
|
|
1126
|
+
* @private
|
|
1127
|
+
*/
|
|
1128
|
+
static _canonicalPrimaryId(primaryId) {
|
|
1129
|
+
const pkSk = this._parsePrimaryId(primaryId);
|
|
1130
|
+
return this.makePrimaryId(pkSk.pk, pkSk.sk);
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1122
1133
|
static _parsePrimaryId(primaryId) {
|
|
1123
1134
|
if (typeof primaryId === "object" && primaryId !== null) {
|
|
1124
1135
|
if (primaryId.pk !== undefined) {
|