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