dynamo-bao 0.3.4 → 0.3.6
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/README.md +3 -0
- package/package.json +1 -2
- package/src/mixins/mutation-mixin.js +35 -5
- package/src/model.js +10 -3
package/README.md
CHANGED
|
@@ -548,6 +548,9 @@ async function testUserModel() {
|
|
|
548
548
|
const foundUser = await User.findByEmail("test@example.com");
|
|
549
549
|
console.log("Found user by email:", foundUser.getPrimaryId());
|
|
550
550
|
|
|
551
|
+
// Force reindex on save (useful after manual data fixes)
|
|
552
|
+
await foundUser.save({ forceReindex: true });
|
|
553
|
+
|
|
551
554
|
// Create some test posts for the user
|
|
552
555
|
const post1 = new Post({
|
|
553
556
|
userId: user.userId,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dynamo-bao",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -49,7 +49,6 @@
|
|
|
49
49
|
"jest": "^29.7.0",
|
|
50
50
|
"jsdoc": "^4.0.4",
|
|
51
51
|
"prettier": "3.4.2",
|
|
52
|
-
"serverless": "^3.38.0",
|
|
53
52
|
"eslint-plugin-promise": "^6.1.1"
|
|
54
53
|
},
|
|
55
54
|
"bin": {
|
|
@@ -49,6 +49,7 @@ const MutationMethods = {
|
|
|
49
49
|
* - String operators: { fieldName: { $beginsWith: value, $contains: value } }
|
|
50
50
|
* - Existence check: { fieldName: { $exists: true|false } }
|
|
51
51
|
* - Logical operators: $and, $or, $not
|
|
52
|
+
* @param {boolean} [options.forceReindex=false] - When true, repopulates all index attributes even if the source fields are unchanged.
|
|
52
53
|
* @returns {Promise<Object>} Returns a promise that resolves to the updated item.
|
|
53
54
|
* @throws {Error} "Item not found" if the item doesn't exist
|
|
54
55
|
* @throws {Error} "Condition check failed" if the condition isn't satisfied
|
|
@@ -285,7 +286,11 @@ const MutationMethods = {
|
|
|
285
286
|
*/
|
|
286
287
|
async _saveItem(primaryId, jsUpdates, options = {}) {
|
|
287
288
|
try {
|
|
288
|
-
const {
|
|
289
|
+
const {
|
|
290
|
+
isNew = false,
|
|
291
|
+
instanceObj = null,
|
|
292
|
+
forceReindex = false,
|
|
293
|
+
} = options;
|
|
289
294
|
|
|
290
295
|
logger.debug("saveItem", primaryId, isNew, instanceObj);
|
|
291
296
|
let consumedCapacity = [];
|
|
@@ -311,6 +316,7 @@ const MutationMethods = {
|
|
|
311
316
|
|
|
312
317
|
const transactItems = [];
|
|
313
318
|
const dyUpdatesToSave = {};
|
|
319
|
+
const indexComputationData = {};
|
|
314
320
|
let hasUniqueConstraintChanges = false;
|
|
315
321
|
|
|
316
322
|
logger.debug("jsUpdates", jsUpdates);
|
|
@@ -327,17 +333,30 @@ const MutationMethods = {
|
|
|
327
333
|
|
|
328
334
|
if (jsUpdates[key] !== undefined) {
|
|
329
335
|
field.validate(jsUpdates[key]);
|
|
330
|
-
|
|
336
|
+
const dyValue = field.toDy(jsUpdates[key]);
|
|
337
|
+
dyUpdatesToSave[key] = dyValue;
|
|
338
|
+
indexComputationData[key] = dyValue;
|
|
331
339
|
} else {
|
|
332
340
|
if (typeof field.updateBeforeSave === "function") {
|
|
333
341
|
const newValue = field.updateBeforeSave(jsUpdates[key]);
|
|
334
342
|
if (newValue !== jsUpdates[key]) {
|
|
335
|
-
|
|
343
|
+
const dyValue = field.toDy(newValue);
|
|
344
|
+
dyUpdatesToSave[key] = dyValue;
|
|
345
|
+
indexComputationData[key] = dyValue;
|
|
336
346
|
}
|
|
337
347
|
}
|
|
338
348
|
}
|
|
339
349
|
}
|
|
340
350
|
|
|
351
|
+
if (forceReindex && currentItem) {
|
|
352
|
+
Object.keys(this.fields).forEach((fieldName) => {
|
|
353
|
+
const currentValue = currentItem._dyData[fieldName];
|
|
354
|
+
if (currentValue !== undefined) {
|
|
355
|
+
indexComputationData[fieldName] = currentValue;
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
341
360
|
if (isNew) {
|
|
342
361
|
primaryId = this._createNewPrimaryId(jsUpdates);
|
|
343
362
|
|
|
@@ -407,7 +426,12 @@ const MutationMethods = {
|
|
|
407
426
|
}
|
|
408
427
|
|
|
409
428
|
// Add GSI keys
|
|
410
|
-
const
|
|
429
|
+
const indexSourceData =
|
|
430
|
+
forceReindex && Object.keys(indexComputationData).length
|
|
431
|
+
? indexComputationData
|
|
432
|
+
: dyUpdatesToSave;
|
|
433
|
+
|
|
434
|
+
const indexKeys = this._getIndexKeys(indexSourceData);
|
|
411
435
|
logger.debug("indexKeys", indexKeys);
|
|
412
436
|
Object.assign(dyUpdatesToSave, indexKeys);
|
|
413
437
|
|
|
@@ -504,12 +528,18 @@ const MutationMethods = {
|
|
|
504
528
|
// Fetch the item since transactWrite doesn't return values
|
|
505
529
|
const savedItem = await this.find(primaryId, { batchDelay: 0 });
|
|
506
530
|
|
|
531
|
+
// if the item doesn't exist, sleep for 200ms and try again
|
|
532
|
+
if (!savedItem.exists()) {
|
|
533
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
534
|
+
savedItem = await this.find(primaryId, { batchDelay: 0 });
|
|
535
|
+
}
|
|
536
|
+
|
|
507
537
|
logger.debug("savedItem", savedItem);
|
|
508
|
-
logger.debug("savedItem.getPrimaryId()", savedItem.getPrimaryId());
|
|
509
538
|
|
|
510
539
|
if (!savedItem.exists()) {
|
|
511
540
|
throw new ConditionalError("Failed to fetch saved item", "update");
|
|
512
541
|
}
|
|
542
|
+
logger.debug("savedItem.getPrimaryId()", savedItem.getPrimaryId());
|
|
513
543
|
|
|
514
544
|
// Set the consumed capacity from the transaction
|
|
515
545
|
savedItem._addConsumedCapacity(
|
package/src/model.js
CHANGED
|
@@ -585,17 +585,24 @@ class BaoModel {
|
|
|
585
585
|
* @param {string[]} [options.constraints.fieldMatches=[]] - An array of field names that must match
|
|
586
586
|
* the current item's loaded state. This is often used for optimistic locking in conjunction
|
|
587
587
|
* with a {@link BaoFields.VersionField} field.
|
|
588
|
+
* @param {boolean} [options.forceReindex=false] - When true, repopulates all index attributes even if no tracked changes exist.
|
|
588
589
|
* @returns {Promise<Object>} Returns a promise that resolves to the updated item.
|
|
589
590
|
*/
|
|
590
591
|
async save(options = {}) {
|
|
591
|
-
|
|
592
|
+
const { forceReindex = false, ...otherOptions } = options;
|
|
593
|
+
|
|
594
|
+
if (!forceReindex && !this.hasChanges() && this.isLoaded()) {
|
|
592
595
|
logger.debug("save() - no changes to save");
|
|
593
596
|
return this; // No changes to save
|
|
594
597
|
}
|
|
595
598
|
|
|
596
599
|
let changes = null;
|
|
600
|
+
const updateOptions = { ...otherOptions, instanceObj: this };
|
|
601
|
+
|
|
597
602
|
if (!this.isLoaded()) {
|
|
598
|
-
|
|
603
|
+
updateOptions.isNew = true;
|
|
604
|
+
changes = this._getAllData();
|
|
605
|
+
} else if (forceReindex) {
|
|
599
606
|
changes = this._getAllData();
|
|
600
607
|
} else {
|
|
601
608
|
changes = this._getChanges();
|
|
@@ -605,7 +612,7 @@ class BaoModel {
|
|
|
605
612
|
const updatedObj = await this.constructor.update(
|
|
606
613
|
this.getPrimaryId(),
|
|
607
614
|
changes,
|
|
608
|
-
{
|
|
615
|
+
{ ...updateOptions, forceReindex },
|
|
609
616
|
);
|
|
610
617
|
|
|
611
618
|
logger.debug("save() - updatedObj", updatedObj);
|