s3db.js 10.0.13 → 10.0.14

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
@@ -5113,10 +5113,6 @@ class EventualConsistencyPlugin extends Plugin {
5113
5113
  return recordOk && record ? record[this.config.field] || 0 : 0;
5114
5114
  }
5115
5115
  try {
5116
- const [recordOk, recordErr, record] = await tryFn(
5117
- () => this.targetResource.get(originalId)
5118
- );
5119
- const currentValue = recordOk && record ? record[this.config.field] || 0 : 0;
5120
5116
  const [ok, err, transactions] = await tryFn(
5121
5117
  () => this.transactionResource.query({
5122
5118
  originalId,
@@ -5124,16 +5120,119 @@ class EventualConsistencyPlugin extends Plugin {
5124
5120
  })
5125
5121
  );
5126
5122
  if (!ok || !transactions || transactions.length === 0) {
5123
+ const [recordOk, recordErr, record] = await tryFn(
5124
+ () => this.targetResource.get(originalId)
5125
+ );
5126
+ const currentValue2 = recordOk && record ? record[this.config.field] || 0 : 0;
5127
5127
  if (this.config.verbose) {
5128
5128
  console.log(
5129
5129
  `[EventualConsistency] ${this.config.resource}.${this.config.field} - No pending transactions for ${originalId}, skipping`
5130
5130
  );
5131
5131
  }
5132
- return currentValue;
5132
+ return currentValue2;
5133
+ }
5134
+ const [appliedOk, appliedErr, appliedTransactions] = await tryFn(
5135
+ () => this.transactionResource.query({
5136
+ originalId,
5137
+ applied: true
5138
+ })
5139
+ );
5140
+ let currentValue = 0;
5141
+ if (appliedOk && appliedTransactions && appliedTransactions.length > 0) {
5142
+ const [recordExistsOk, recordExistsErr, recordExists] = await tryFn(
5143
+ () => this.targetResource.get(originalId)
5144
+ );
5145
+ if (!recordExistsOk || !recordExists) {
5146
+ if (this.config.verbose) {
5147
+ console.log(
5148
+ `[EventualConsistency] ${this.config.resource}.${this.config.field} - Record ${originalId} doesn't exist, deleting ${appliedTransactions.length} old applied transactions`
5149
+ );
5150
+ }
5151
+ const { results, errors } = await promisePool.PromisePool.for(appliedTransactions).withConcurrency(10).process(async (txn) => {
5152
+ const [deleted] = await tryFn(() => this.transactionResource.delete(txn.id));
5153
+ return deleted;
5154
+ });
5155
+ if (this.config.verbose && errors && errors.length > 0) {
5156
+ console.warn(
5157
+ `[EventualConsistency] ${this.config.resource}.${this.config.field} - Failed to delete ${errors.length} old applied transactions`
5158
+ );
5159
+ }
5160
+ currentValue = 0;
5161
+ } else {
5162
+ appliedTransactions.sort(
5163
+ (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime()
5164
+ );
5165
+ const hasSetInApplied = appliedTransactions.some((t) => t.operation === "set");
5166
+ if (!hasSetInApplied) {
5167
+ const recordValue = recordExists[this.config.field] || 0;
5168
+ let appliedDelta = 0;
5169
+ for (const t of appliedTransactions) {
5170
+ if (t.operation === "add") appliedDelta += t.value;
5171
+ else if (t.operation === "sub") appliedDelta -= t.value;
5172
+ }
5173
+ const baseValue = recordValue - appliedDelta;
5174
+ const hasExistingAnchor = appliedTransactions.some((t) => t.source === "anchor");
5175
+ if (baseValue !== 0 && !hasExistingAnchor) {
5176
+ const firstTransactionDate = new Date(appliedTransactions[0].timestamp);
5177
+ const cohortInfo = this.getCohortInfo(firstTransactionDate);
5178
+ const anchorTransaction = {
5179
+ id: idGenerator(),
5180
+ originalId,
5181
+ field: this.config.field,
5182
+ value: baseValue,
5183
+ operation: "set",
5184
+ timestamp: new Date(firstTransactionDate.getTime() - 1).toISOString(),
5185
+ // 1ms before first txn to ensure it's first
5186
+ cohortDate: cohortInfo.date,
5187
+ cohortHour: cohortInfo.hour,
5188
+ cohortMonth: cohortInfo.month,
5189
+ source: "anchor",
5190
+ applied: true
5191
+ };
5192
+ await this.transactionResource.insert(anchorTransaction);
5193
+ appliedTransactions.unshift(anchorTransaction);
5194
+ }
5195
+ }
5196
+ currentValue = this.config.reducer(appliedTransactions);
5197
+ }
5198
+ } else {
5199
+ const [recordOk, recordErr, record] = await tryFn(
5200
+ () => this.targetResource.get(originalId)
5201
+ );
5202
+ currentValue = recordOk && record ? record[this.config.field] || 0 : 0;
5203
+ if (currentValue !== 0) {
5204
+ let anchorTimestamp;
5205
+ if (transactions && transactions.length > 0) {
5206
+ const firstPendingDate = new Date(transactions[0].timestamp);
5207
+ anchorTimestamp = new Date(firstPendingDate.getTime() - 1).toISOString();
5208
+ } else {
5209
+ anchorTimestamp = (/* @__PURE__ */ new Date()).toISOString();
5210
+ }
5211
+ const cohortInfo = this.getCohortInfo(new Date(anchorTimestamp));
5212
+ const anchorTransaction = {
5213
+ id: idGenerator(),
5214
+ originalId,
5215
+ field: this.config.field,
5216
+ value: currentValue,
5217
+ operation: "set",
5218
+ timestamp: anchorTimestamp,
5219
+ cohortDate: cohortInfo.date,
5220
+ cohortHour: cohortInfo.hour,
5221
+ cohortMonth: cohortInfo.month,
5222
+ source: "anchor",
5223
+ applied: true
5224
+ };
5225
+ await this.transactionResource.insert(anchorTransaction);
5226
+ if (this.config.verbose) {
5227
+ console.log(
5228
+ `[EventualConsistency] ${this.config.resource}.${this.config.field} - Created anchor transaction for ${originalId} with base value ${currentValue}`
5229
+ );
5230
+ }
5231
+ }
5133
5232
  }
5134
5233
  if (this.config.verbose) {
5135
5234
  console.log(
5136
- `[EventualConsistency] ${this.config.resource}.${this.config.field} - Consolidating ${originalId}: ${transactions.length} pending transactions (current: ${currentValue})`
5235
+ `[EventualConsistency] ${this.config.resource}.${this.config.field} - Consolidating ${originalId}: ${transactions.length} pending transactions (current: ${currentValue} from ${appliedOk && appliedTransactions?.length > 0 ? "applied transactions" : "record"})`
5137
5236
  );
5138
5237
  }
5139
5238
  transactions.sort(
@@ -12053,7 +12152,7 @@ class Database extends EventEmitter {
12053
12152
  this.id = idGenerator(7);
12054
12153
  this.version = "1";
12055
12154
  this.s3dbVersion = (() => {
12056
- const [ok, err, version] = tryFn(() => true ? "10.0.13" : "latest");
12155
+ const [ok, err, version] = tryFn(() => true ? "10.0.14" : "latest");
12057
12156
  return ok ? version : "latest";
12058
12157
  })();
12059
12158
  this.resources = {};