monorise 2.0.0-dev.2 → 2.0.0-dev.3

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.
@@ -624,6 +624,7 @@ var StandardErrorCode = {
624
624
  REPLICATION_ERROR: "REPLICATION_ERROR",
625
625
  RETRYABLE_MUTUAL_LOCK_CONFLICT: "RETRYABLE_MUTUAL_LOCK_CONFLICT",
626
626
  TAG_IS_UNDEFINED: "TAG_IS_UNDEFINED",
627
+ TRANSACTION_CONFLICT: "TRANSACTION_CONFLICT",
627
628
  TRANSACTION_EMPTY: "TRANSACTION_EMPTY",
628
629
  TRANSACTION_FAILED: "TRANSACTION_FAILED",
629
630
  TRANSACTION_INVALID_OPERATION: "TRANSACTION_INVALID_OPERATION",
@@ -752,6 +753,16 @@ var Repository = class {
752
753
  };
753
754
 
754
755
  // data/Entity.ts
756
+ var transactionConflict = (err) => {
757
+ var _a;
758
+ return ((_a = err.CancellationReasons) == null ? void 0 : _a.some(
759
+ (reason) => reason.Code === "TransactionConflict"
760
+ )) ? new StandardError(
761
+ StandardErrorCode.TRANSACTION_CONFLICT,
762
+ "Another request wrote the same data at the same time; retry",
763
+ err
764
+ ) : void 0;
765
+ };
755
766
  var Entity = class _Entity extends Item {
756
767
  constructor(entityType, entityId, data = {}, _createdAt, _updatedAt, _expiresAt) {
757
768
  super();
@@ -1149,6 +1160,8 @@ var EntityRepository = class extends Repository {
1149
1160
  );
1150
1161
  }
1151
1162
  }
1163
+ const conflict = transactionConflict(err);
1164
+ if (conflict) throw conflict;
1152
1165
  }
1153
1166
  throw err;
1154
1167
  }
@@ -1353,6 +1366,8 @@ var EntityRepository = class extends Repository {
1353
1366
  );
1354
1367
  }
1355
1368
  }
1369
+ const conflict = transactionConflict(err);
1370
+ if (conflict) throw conflict;
1356
1371
  }
1357
1372
  throw err;
1358
1373
  }
@@ -8328,6 +8343,7 @@ function resolveAsEntity(mutualConfig, callOptions = {}) {
8328
8343
  var _a, _b;
8329
8344
  return (_b = callOptions.asEntity) != null ? _b : (_a = mutualConfig == null ? void 0 : mutualConfig.asEntity) == null ? void 0 : _a.name;
8330
8345
  }
8346
+ var entityInTransactionKey = (entityType, entityId) => `${entityType}#${entityId}`;
8331
8347
  var MutualService = class {
8332
8348
  constructor(EntityConfig, entityRepository, mutualRepository, publishEvent2, ddbUtils, entityServiceLifeCycle) {
8333
8349
  this.EntityConfig = EntityConfig;
@@ -8359,7 +8375,8 @@ var MutualService = class {
8359
8375
  skipMutualCreation = false,
8360
8376
  ConditionExpression,
8361
8377
  ExpressionAttributeNames,
8362
- ExpressionAttributeValues
8378
+ ExpressionAttributeValues,
8379
+ entitiesInTransaction
8363
8380
  } = options;
8364
8381
  const mutualFieldConfig = this.getMutualFieldConfig(byEntityType, entityType);
8365
8382
  const asEntity = resolveAsEntity(mutualFieldConfig, options);
@@ -8388,9 +8405,13 @@ var MutualService = class {
8388
8405
  const parsedMutualPayload = schema.parse(mutualPayload);
8389
8406
  debugLog("[MONORISE_DEBUG] createMutual payload parsed:", parsedMutualPayload);
8390
8407
  debugLog("[MONORISE_DEBUG] createMutual fetching entities...");
8408
+ const readEntity = (type, id) => __async(this, null, function* () {
8409
+ const data = entitiesInTransaction == null ? void 0 : entitiesInTransaction.get(entityInTransactionKey(type, id));
8410
+ return data ? { data } : this.entityRepository.getEntity(type, id);
8411
+ });
8391
8412
  const [{ data: byEntityData }, { data: entityData }] = yield Promise.all([
8392
- this.entityRepository.getEntity(byEntityType, byEntityId),
8393
- this.entityRepository.getEntity(entityType, entityId)
8413
+ readEntity(byEntityType, byEntityId),
8414
+ readEntity(entityType, entityId)
8394
8415
  ]);
8395
8416
  debugLog("[MONORISE_DEBUG] createMutual entities fetched:", {
8396
8417
  hasByEntityData: !!byEntityData,
@@ -10082,7 +10103,7 @@ var DbUtils = class {
10082
10103
  }
10083
10104
  executeTransactWrite(params) {
10084
10105
  return __async(this, null, function* () {
10085
- var _a;
10106
+ var _a, _b;
10086
10107
  try {
10087
10108
  yield this.ddbClient.transactWriteItems(params);
10088
10109
  } catch (err) {
@@ -10098,6 +10119,16 @@ var DbUtils = class {
10098
10119
  { params }
10099
10120
  );
10100
10121
  }
10122
+ if ((_b = err.CancellationReasons) == null ? void 0 : _b.some(
10123
+ (reason) => reason.Code === "TransactionConflict"
10124
+ )) {
10125
+ throw new StandardError(
10126
+ StandardErrorCode.TRANSACTION_CONFLICT,
10127
+ "Another request wrote the same data at the same time; retry",
10128
+ err,
10129
+ { params }
10130
+ );
10131
+ }
10101
10132
  }
10102
10133
  throw new StandardError(
10103
10134
  StandardErrorCode.TRANSACTION_FAILED,
@@ -10181,8 +10212,8 @@ var CreateEntityController = class {
10181
10212
  c.status(httpStatus6.BAD_REQUEST);
10182
10213
  return c.json(__spreadValues({}, err.toJSON()));
10183
10214
  }
10184
- if (err instanceof StandardError && err.code === StandardErrorCode.UNIQUE_VALUE_EXISTS) {
10185
- c.status(httpStatus6.BAD_REQUEST);
10215
+ if (err instanceof StandardError && (err.code === StandardErrorCode.UNIQUE_VALUE_EXISTS || err.code === StandardErrorCode.TRANSACTION_CONFLICT)) {
10216
+ c.status(httpStatus6.CONFLICT);
10186
10217
  return c.json(__spreadValues({}, err.toJSON()));
10187
10218
  }
10188
10219
  console.log("===create-entity error:", {
@@ -10382,8 +10413,8 @@ var UpdateEntityController = class {
10382
10413
  c.status(httpStatus10.BAD_REQUEST);
10383
10414
  return c.json(__spreadValues({}, err.toJSON()));
10384
10415
  }
10385
- if (err instanceof StandardError && err.code === StandardErrorCode.UNIQUE_VALUE_EXISTS) {
10386
- c.status(httpStatus10.BAD_REQUEST);
10416
+ if (err instanceof StandardError && (err.code === StandardErrorCode.UNIQUE_VALUE_EXISTS || err.code === StandardErrorCode.TRANSACTION_CONFLICT)) {
10417
+ c.status(httpStatus10.CONFLICT);
10387
10418
  return c.json(__spreadValues({}, err.toJSON()));
10388
10419
  }
10389
10420
  if (err instanceof StandardError && err.code === StandardErrorCode.CONDITIONAL_CHECK_FAILED) {
@@ -10544,6 +10575,10 @@ var UpsertEntityController = class {
10544
10575
  c.status(httpStatus12.BAD_REQUEST);
10545
10576
  return c.json(__spreadValues({}, err.toJSON()));
10546
10577
  }
10578
+ if (err instanceof StandardError && (err.code === StandardErrorCode.UNIQUE_VALUE_EXISTS || err.code === StandardErrorCode.TRANSACTION_CONFLICT)) {
10579
+ c.status(httpStatus12.CONFLICT);
10580
+ return c.json(__spreadValues({}, err.toJSON()));
10581
+ }
10547
10582
  throw err;
10548
10583
  }
10549
10584
  }));
@@ -10589,6 +10624,10 @@ var CreateMutualController = class {
10589
10624
  c.status(httpStatus13.BAD_REQUEST);
10590
10625
  return c.json(__spreadValues({}, err.toJSON()));
10591
10626
  }
10627
+ if (err instanceof StandardError && err.code === StandardErrorCode.TRANSACTION_CONFLICT) {
10628
+ c.status(httpStatus13.CONFLICT);
10629
+ return c.json(__spreadValues({}, err.toJSON()));
10630
+ }
10592
10631
  throw err;
10593
10632
  }
10594
10633
  }));
@@ -10616,6 +10655,10 @@ var DeleteMutualController = class {
10616
10655
  c.status(httpStatus14.BAD_REQUEST);
10617
10656
  return c.json(__spreadValues({}, err.toJSON()));
10618
10657
  }
10658
+ if (err instanceof StandardError && err.code === StandardErrorCode.TRANSACTION_CONFLICT) {
10659
+ c.status(httpStatus14.CONFLICT);
10660
+ return c.json(__spreadValues({}, err.toJSON()));
10661
+ }
10619
10662
  throw err;
10620
10663
  }
10621
10664
  }));
@@ -10748,6 +10791,10 @@ var UpdateMutualController = class {
10748
10791
  c.status(httpStatus16.BAD_REQUEST);
10749
10792
  return c.json(__spreadValues({}, err.toJSON()));
10750
10793
  }
10794
+ if (err instanceof StandardError && err.code === StandardErrorCode.TRANSACTION_CONFLICT) {
10795
+ c.status(httpStatus16.CONFLICT);
10796
+ return c.json(__spreadValues({}, err.toJSON()));
10797
+ }
10751
10798
  throw err;
10752
10799
  }
10753
10800
  }));
@@ -11406,8 +11453,31 @@ var TransactionService = class {
11406
11453
  const allTransactItems = [];
11407
11454
  const pendingEvents = [];
11408
11455
  const resultEntries = [];
11409
- const processed = yield Promise.all(
11410
- operations.map((op) => this.processOperation(op))
11456
+ const processed = new Array(operations.length);
11457
+ const isCreate = (op) => op.operation === "createEntity";
11458
+ yield Promise.all(
11459
+ operations.map((op, index) => __async(this, null, function* () {
11460
+ if (isCreate(op)) processed[index] = yield this.processOperation(op);
11461
+ }))
11462
+ );
11463
+ const entitiesInTransaction = /* @__PURE__ */ new Map();
11464
+ for (const { resultEntry } of processed.filter(Boolean)) {
11465
+ if (resultEntry.operation === "createEntity" && resultEntry.data) {
11466
+ entitiesInTransaction.set(
11467
+ entityInTransactionKey(resultEntry.entityType, resultEntry.entityId),
11468
+ resultEntry.data
11469
+ );
11470
+ }
11471
+ }
11472
+ yield Promise.all(
11473
+ operations.map((op, index) => __async(this, null, function* () {
11474
+ if (!isCreate(op)) {
11475
+ processed[index] = yield this.processOperation(
11476
+ op,
11477
+ entitiesInTransaction
11478
+ );
11479
+ }
11480
+ }))
11411
11481
  );
11412
11482
  const afterCommits = [];
11413
11483
  for (const { items, events, afterCommit, resultEntry } of processed) {
@@ -11526,7 +11596,7 @@ var TransactionService = class {
11526
11596
  return { results: resultEntries };
11527
11597
  });
11528
11598
  }
11529
- processOperation(op) {
11599
+ processOperation(op, entitiesInTransaction) {
11530
11600
  return __async(this, null, function* () {
11531
11601
  switch (op.operation) {
11532
11602
  case "createEntity": {
@@ -11600,7 +11670,7 @@ var TransactionService = class {
11600
11670
  };
11601
11671
  }
11602
11672
  case "createMutual":
11603
- return this.processCreateMutual(op);
11673
+ return this.processCreateMutual(op, entitiesInTransaction);
11604
11674
  case "updateMutual":
11605
11675
  return this.processUpdateMutual(op);
11606
11676
  case "deleteMutual":
@@ -11617,7 +11687,7 @@ var TransactionService = class {
11617
11687
  // the write is deferred into this transaction. A mutual created here
11618
11688
  // publishes the same MUTUAL_CREATED event, and an asEntity mutual's
11619
11689
  // projection is written in the same transaction.
11620
- processCreateMutual(op) {
11690
+ processCreateMutual(op, entitiesInTransaction) {
11621
11691
  return __async(this, null, function* () {
11622
11692
  var _a, _b;
11623
11693
  this.assertMutualOperation(op);
@@ -11627,7 +11697,8 @@ var TransactionService = class {
11627
11697
  byEntityId: op.byEntityId,
11628
11698
  entityType: op.entityType,
11629
11699
  entityId: op.entityId,
11630
- mutualPayload
11700
+ mutualPayload,
11701
+ options: { entitiesInTransaction }
11631
11702
  });
11632
11703
  return {
11633
11704
  items: transactItems,