s3db.js 10.0.8 → 10.0.9

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/dist/s3db.cjs.js CHANGED
@@ -4958,11 +4958,35 @@ class EventualConsistencyPlugin extends Plugin {
4958
4958
  `[EventualConsistency] ${this.config.resource}.${this.config.field} - ${originalId}: ${currentValue} \u2192 ${consolidatedValue} (${consolidatedValue > currentValue ? "+" : ""}${consolidatedValue - currentValue})`
4959
4959
  );
4960
4960
  }
4961
- const [updateOk, updateErr] = await tryFn(
4962
- () => this.targetResource.update(originalId, {
4963
- [this.config.field]: consolidatedValue
4964
- })
4965
- );
4961
+ const [updateOk, updateErr] = await tryFn(async () => {
4962
+ const [ok2, err2] = await tryFn(
4963
+ () => this.targetResource.update(originalId, {
4964
+ [this.config.field]: consolidatedValue
4965
+ })
4966
+ );
4967
+ if (!ok2 && (err2?.code === "NoSuchKey" || err2?.code === "NotFound")) {
4968
+ if (this.config.verbose) {
4969
+ console.log(
4970
+ `[EventualConsistency] ${this.config.resource}.${this.config.field} - Record ${originalId} doesn't exist, creating with ${this.config.field}=${consolidatedValue}`
4971
+ );
4972
+ }
4973
+ return await this.targetResource.insert({
4974
+ id: originalId,
4975
+ [this.config.field]: consolidatedValue
4976
+ });
4977
+ }
4978
+ if (!ok2) {
4979
+ throw err2;
4980
+ }
4981
+ return ok2;
4982
+ });
4983
+ if (!updateOk) {
4984
+ console.error(
4985
+ `[EventualConsistency] ${this.config.resource}.${this.config.field} - FAILED to update ${originalId}: ${updateErr?.message || updateErr}`,
4986
+ { error: updateErr, consolidatedValue, currentValue }
4987
+ );
4988
+ throw updateErr;
4989
+ }
4966
4990
  if (updateOk) {
4967
4991
  const transactionsToUpdate = transactions.filter((txn) => txn.id !== "__synthetic__");
4968
4992
  const { results, errors } = await promisePool.PromisePool.for(transactionsToUpdate).withConcurrency(10).process(async (txn) => {
@@ -9444,11 +9468,11 @@ ${errorDetails}`,
9444
9468
  if (Array.isArray(listeners)) {
9445
9469
  for (const listener of listeners) {
9446
9470
  if (typeof listener === "function") {
9447
- this.on(eventName, listener);
9471
+ this.on(eventName, listener.bind(this));
9448
9472
  }
9449
9473
  }
9450
9474
  } else if (typeof listeners === "function") {
9451
- this.on(eventName, listeners);
9475
+ this.on(eventName, listeners.bind(this));
9452
9476
  }
9453
9477
  }
9454
9478
  }
@@ -11779,7 +11803,7 @@ class Database extends EventEmitter {
11779
11803
  this.id = idGenerator(7);
11780
11804
  this.version = "1";
11781
11805
  this.s3dbVersion = (() => {
11782
- const [ok, err, version] = tryFn(() => true ? "10.0.8" : "latest");
11806
+ const [ok, err, version] = tryFn(() => true ? "10.0.9" : "latest");
11783
11807
  return ok ? version : "latest";
11784
11808
  })();
11785
11809
  this.resources = {};