s3db.js 10.0.13 → 10.0.15

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