dynamo-bao 0.7.0 → 0.7.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.
- package/package.json +1 -1
- package/src/fields.js +3 -1
- package/src/mixins/batch-loading-mixin.js +62 -9
- package/src/mixins/mutation-mixin.js +70 -30
package/package.json
CHANGED
package/src/fields.js
CHANGED
|
@@ -179,11 +179,13 @@ class StringField extends BaoBaseField {
|
|
|
179
179
|
*/
|
|
180
180
|
class DateTimeField extends BaoBaseField {
|
|
181
181
|
validate(value) {
|
|
182
|
-
if (this.required && value === undefined) {
|
|
182
|
+
if (this.required && (value === null || value === undefined)) {
|
|
183
183
|
throw new ValidationError("Field is required", null, value);
|
|
184
184
|
}
|
|
185
|
+
// null clears the field (toDy maps it to a REMOVE)
|
|
185
186
|
if (
|
|
186
187
|
value !== undefined &&
|
|
188
|
+
value !== null &&
|
|
187
189
|
!(value instanceof Date) &&
|
|
188
190
|
typeof value !== "number" &&
|
|
189
191
|
typeof value !== "string"
|
|
@@ -115,6 +115,52 @@ const BatchLoadingMethods = {
|
|
|
115
115
|
return !!(context && context.batchRequests);
|
|
116
116
|
},
|
|
117
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Key for the request-scoped loader cache. The cache is one Map per request
|
|
120
|
+
* shared by every model and tenant, so the key carries both, plus the
|
|
121
|
+
* canonical string id so a string id and its object form share an entry.
|
|
122
|
+
* @private
|
|
123
|
+
*/
|
|
124
|
+
_loaderKey(primaryId) {
|
|
125
|
+
const pkSk = this._parsePrimaryId(primaryId);
|
|
126
|
+
return JSON.stringify([
|
|
127
|
+
this.name,
|
|
128
|
+
this.manager.getTenantId() ?? null,
|
|
129
|
+
this.makePrimaryId(pkSk.pk, pkSk.sk),
|
|
130
|
+
]);
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Store a just-written instance in the request-scoped loader cache so a
|
|
135
|
+
* later find() in the same request returns the written row instead of a
|
|
136
|
+
* stale one. Caches a copy with no consumed capacity, so later operations
|
|
137
|
+
* that load it don't report this write's capacity as their own. No-op
|
|
138
|
+
* outside runWithBatchContext().
|
|
139
|
+
* @private
|
|
140
|
+
*/
|
|
141
|
+
_primeLoaderContext(primaryId, instance) {
|
|
142
|
+
const loaderContext = batchContext?.getStore()?.loaderContext;
|
|
143
|
+
if (loaderContext) {
|
|
144
|
+
loaderContext.set(
|
|
145
|
+
this._loaderKey(primaryId),
|
|
146
|
+
this._createFromDyItem({ ...instance._dyData }),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Drop a just-deleted item from the request-scoped loader cache so a later
|
|
153
|
+
* find() in the same request doesn't return the deleted row. No-op outside
|
|
154
|
+
* runWithBatchContext().
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
_evictFromLoaderContext(primaryId) {
|
|
158
|
+
const loaderContext = batchContext?.getStore()?.loaderContext;
|
|
159
|
+
if (loaderContext) {
|
|
160
|
+
loaderContext.delete(this._loaderKey(primaryId));
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
|
|
118
164
|
/**
|
|
119
165
|
* @memberof BaoModel
|
|
120
166
|
* @description
|
|
@@ -163,9 +209,10 @@ const BatchLoadingMethods = {
|
|
|
163
209
|
// First check loaderContext for existing items
|
|
164
210
|
if (loaderContext) {
|
|
165
211
|
primaryIds.forEach((id) => {
|
|
166
|
-
|
|
212
|
+
const loaderKey = this._loaderKey(id);
|
|
213
|
+
if (loaderContext.has(loaderKey)) {
|
|
167
214
|
const instance = this._createFromDyItem(
|
|
168
|
-
loaderContext.get(
|
|
215
|
+
loaderContext.get(loaderKey)._dyData,
|
|
169
216
|
);
|
|
170
217
|
results[id] = instance;
|
|
171
218
|
} else {
|
|
@@ -222,7 +269,7 @@ const BatchLoadingMethods = {
|
|
|
222
269
|
|
|
223
270
|
// Add to loader context if provided
|
|
224
271
|
if (loaderContext) {
|
|
225
|
-
loaderContext.set(primaryId, instance);
|
|
272
|
+
loaderContext.set(this._loaderKey(primaryId), instance);
|
|
226
273
|
}
|
|
227
274
|
});
|
|
228
275
|
}
|
|
@@ -277,7 +324,7 @@ const BatchLoadingMethods = {
|
|
|
277
324
|
if (fb.item) {
|
|
278
325
|
const instance = this._createFromDyItem(fb.item);
|
|
279
326
|
results[instance.getPrimaryId()] = instance;
|
|
280
|
-
if (loaderContext) loaderContext.set(id, instance);
|
|
327
|
+
if (loaderContext) loaderContext.set(this._loaderKey(id), instance);
|
|
281
328
|
}
|
|
282
329
|
}
|
|
283
330
|
}
|
|
@@ -342,13 +389,16 @@ const BatchLoadingMethods = {
|
|
|
342
389
|
}
|
|
343
390
|
|
|
344
391
|
// Check loader context first (unless bypassing cache)
|
|
345
|
-
|
|
346
|
-
|
|
392
|
+
const loaderKey = loaderContext ? this._loaderKey(primaryId) : null;
|
|
393
|
+
if (!bypassCache && loaderContext && loaderContext.has(loaderKey)) {
|
|
394
|
+
const cachedItem = loaderContext.get(loaderKey);
|
|
347
395
|
// Return the exact same cached object instance for true caching
|
|
348
396
|
return cachedItem;
|
|
349
397
|
}
|
|
350
398
|
|
|
351
|
-
|
|
399
|
+
// bypassCache skips batching too: a batch resolves through batchFind,
|
|
400
|
+
// which reads the loader cache.
|
|
401
|
+
if (batchDelay === 0 || bypassCache) {
|
|
352
402
|
// Direct DynamoDB request logic
|
|
353
403
|
const pkSk = this._parsePrimaryId(primaryId);
|
|
354
404
|
const dyKey = this._getDyKeyForPkSk(pkSk);
|
|
@@ -392,7 +442,7 @@ const BatchLoadingMethods = {
|
|
|
392
442
|
|
|
393
443
|
// Add to loader context if provided (unless bypassing cache)
|
|
394
444
|
if (!bypassCache && loaderContext) {
|
|
395
|
-
loaderContext.set(
|
|
445
|
+
loaderContext.set(loaderKey, instance);
|
|
396
446
|
}
|
|
397
447
|
|
|
398
448
|
return instance;
|
|
@@ -400,7 +450,10 @@ const BatchLoadingMethods = {
|
|
|
400
450
|
|
|
401
451
|
// Batch request logic
|
|
402
452
|
return new Promise((resolve, reject) => {
|
|
403
|
-
|
|
453
|
+
// One batch per model, tenant and delay: the batch builds its keys in
|
|
454
|
+
// the first caller's async context, so it must not mix tenants.
|
|
455
|
+
const tenantId = this.manager.getTenantId() ?? "";
|
|
456
|
+
const batchKey = `${this.name}-${tenantId}-${batchDelay}`;
|
|
404
457
|
const batchRequests = this._getBatchRequests();
|
|
405
458
|
let batchRequest = batchRequests.get(batchKey);
|
|
406
459
|
|
|
@@ -225,6 +225,8 @@ const MutationMethods = {
|
|
|
225
225
|
);
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
this._evictFromLoaderContext(primaryId);
|
|
229
|
+
|
|
228
230
|
await pluginManager.executeHooks(
|
|
229
231
|
this.name,
|
|
230
232
|
"afterDelete",
|
|
@@ -543,11 +545,9 @@ const MutationMethods = {
|
|
|
543
545
|
}
|
|
544
546
|
});
|
|
545
547
|
|
|
546
|
-
//
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
isNew ? null : primaryId,
|
|
550
|
-
);
|
|
548
|
+
// No pre-save uniqueness read: each constraint Put in the transaction
|
|
549
|
+
// carries attribute_not_exists, and the catch block below runs
|
|
550
|
+
// _validateUniqueConstraints to name the colliding field.
|
|
551
551
|
|
|
552
552
|
// Handle unique constraints
|
|
553
553
|
for (const constraint of Object.values(this.uniqueConstraints || {})) {
|
|
@@ -689,12 +689,20 @@ const MutationMethods = {
|
|
|
689
689
|
...filterExpression.ExpressionAttributeValues,
|
|
690
690
|
};
|
|
691
691
|
}
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (isNew) {
|
|
695
|
+
// For new items, ensure they don't already exist, ANDed with any
|
|
696
|
+
// caller-supplied condition. _buildUpdateExpression maps each field
|
|
697
|
+
// to "#<fieldName>"; user field names can't start with "_" and no
|
|
698
|
+
// system field starts with "__", so a "#__" placeholder can't clobber
|
|
699
|
+
// one.
|
|
700
|
+
updateParams.ConditionExpression = updateParams.ConditionExpression
|
|
701
|
+
? `(${updateParams.ConditionExpression}) AND attribute_not_exists(#__newItemPk)`
|
|
702
|
+
: "attribute_not_exists(#__newItemPk)";
|
|
695
703
|
updateParams.ExpressionAttributeNames = {
|
|
696
704
|
...updateParams.ExpressionAttributeNames,
|
|
697
|
-
"#
|
|
705
|
+
"#__newItemPk": "_pk",
|
|
698
706
|
};
|
|
699
707
|
}
|
|
700
708
|
|
|
@@ -706,16 +714,15 @@ const MutationMethods = {
|
|
|
706
714
|
// will then find and update the relocated canonical row. ANDed with any
|
|
707
715
|
// caller-supplied condition.
|
|
708
716
|
//
|
|
709
|
-
// Use a
|
|
710
|
-
//
|
|
711
|
-
// "#pk".
|
|
717
|
+
// Use a "#__" placeholder so we never clobber a field placeholder
|
|
718
|
+
// (see the new-item guard above).
|
|
712
719
|
updateParams.ExpressionAttributeNames = {
|
|
713
720
|
...updateParams.ExpressionAttributeNames,
|
|
714
|
-
"#
|
|
721
|
+
"#__migExistsPk": "_pk",
|
|
715
722
|
};
|
|
716
723
|
updateParams.ConditionExpression = updateParams.ConditionExpression
|
|
717
|
-
? `(${updateParams.ConditionExpression}) AND attribute_exists(#
|
|
718
|
-
: "attribute_exists(#
|
|
724
|
+
? `(${updateParams.ConditionExpression}) AND attribute_exists(#__migExistsPk)`
|
|
725
|
+
: "attribute_exists(#__migExistsPk)";
|
|
719
726
|
}
|
|
720
727
|
|
|
721
728
|
const dyKey = writeKey;
|
|
@@ -744,13 +751,39 @@ const MutationMethods = {
|
|
|
744
751
|
logger.debug("transactItems response", response);
|
|
745
752
|
logger.debug("primaryId to load", primaryId);
|
|
746
753
|
|
|
747
|
-
//
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
//
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
+
// transactWrite doesn't return values. A new item is exactly its key
|
|
755
|
+
// plus what we wrote (REMOVEd nulls aside), so build it locally. An
|
|
756
|
+
// update must be read back, bypassing the loader cache, which still
|
|
757
|
+
// holds the pre-update row.
|
|
758
|
+
let savedItem;
|
|
759
|
+
if (isNew) {
|
|
760
|
+
const storedItem = { ...dyKey };
|
|
761
|
+
for (const [key, value] of Object.entries(dyUpdatesToSave)) {
|
|
762
|
+
if (value !== undefined && value !== null) {
|
|
763
|
+
storedItem[key] = value;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
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
|
+
savedItem = this._createFromDyItem(storedItem);
|
|
778
|
+
} else {
|
|
779
|
+
const readOptions = { batchDelay: 0, bypassCache: true };
|
|
780
|
+
savedItem = await this.find(primaryId, readOptions);
|
|
781
|
+
|
|
782
|
+
// if the item doesn't exist, sleep for 200ms and try again
|
|
783
|
+
if (!savedItem.exists()) {
|
|
784
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
785
|
+
savedItem = await this.find(primaryId, readOptions);
|
|
786
|
+
}
|
|
754
787
|
}
|
|
755
788
|
|
|
756
789
|
logger.debug("savedItem", savedItem);
|
|
@@ -766,8 +799,11 @@ const MutationMethods = {
|
|
|
766
799
|
"write",
|
|
767
800
|
false,
|
|
768
801
|
);
|
|
769
|
-
|
|
802
|
+
// The loaded row's capacity was counted in the request total when
|
|
803
|
+
// it was consumed, so don't count it again.
|
|
804
|
+
savedItem._addConsumedCapacity(consumedCapacity, "read", false, true);
|
|
770
805
|
|
|
806
|
+
this._primeLoaderContext(primaryId, savedItem);
|
|
771
807
|
return savedItem;
|
|
772
808
|
} else {
|
|
773
809
|
// Use simple update if no unique constraints are changing
|
|
@@ -795,12 +831,15 @@ const MutationMethods = {
|
|
|
795
831
|
"write",
|
|
796
832
|
false,
|
|
797
833
|
);
|
|
798
|
-
|
|
834
|
+
// The loaded row's capacity was counted in the request total when
|
|
835
|
+
// it was consumed, so don't count it again.
|
|
836
|
+
savedItem._addConsumedCapacity(consumedCapacity, "read", false, true);
|
|
799
837
|
|
|
800
838
|
if (!savedItem.exists()) {
|
|
801
839
|
throw new ConditionalError("Failed to fetch saved item", "update");
|
|
802
840
|
}
|
|
803
841
|
|
|
842
|
+
this._primeLoaderContext(primaryId, savedItem);
|
|
804
843
|
return savedItem;
|
|
805
844
|
}
|
|
806
845
|
} catch (error) {
|
|
@@ -810,6 +849,13 @@ const MutationMethods = {
|
|
|
810
849
|
}
|
|
811
850
|
|
|
812
851
|
if (error.name === "TransactionCanceledException") {
|
|
852
|
+
// A taken unique value cancels the transaction; this read throws a
|
|
853
|
+
// ConditionalError naming the field if that's the cause.
|
|
854
|
+
await this._validateUniqueConstraints(
|
|
855
|
+
jsUpdates,
|
|
856
|
+
isNew ? null : primaryId,
|
|
857
|
+
);
|
|
858
|
+
|
|
813
859
|
// Check if it's due to conditional check failure
|
|
814
860
|
if (
|
|
815
861
|
error.CancellationReasons?.some(
|
|
@@ -822,12 +868,6 @@ const MutationMethods = {
|
|
|
822
868
|
error,
|
|
823
869
|
);
|
|
824
870
|
}
|
|
825
|
-
// For other transaction cancellation reasons, try to validate unique constraints
|
|
826
|
-
// which will throw the appropriate ConditionalError if that's the issue
|
|
827
|
-
await this._validateUniqueConstraints(
|
|
828
|
-
jsUpdates,
|
|
829
|
-
isNew ? null : primaryId,
|
|
830
|
-
);
|
|
831
871
|
}
|
|
832
872
|
throw error;
|
|
833
873
|
}
|