event-store-adapter-js 2.0.25-snapshot.3 → 2.1.0

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.
@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.EventStoreForDynamoDB = void 0;
16
+ const types_1 = require("../types");
16
17
  const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
17
18
  const moment_1 = __importDefault(require("moment/moment"));
18
19
  const default_key_resolver_1 = require("./default-key-resolver");
@@ -152,7 +153,7 @@ class EventStoreForDynamoDB {
152
153
  return new EventStoreForDynamoDB(this.dynamodbClient, this.journalTableName, this.snapshotTableName, this.journalAidIndexName, this.snapshotAidIndexName, this.shardCount, this.eventConverter, this.snapshotConverter, this.keepSnapshotCount, this.deleteTtl, this.keyResolver, this.eventSerializer, snapshotSerializer);
153
154
  }
154
155
  createEventAndSnapshot(event, aggregate) {
155
- var _a, _b;
156
+ var _a, _b, _c;
156
157
  return __awaiter(this, void 0, void 0, function* () {
157
158
  (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`createEventAndSnapshot(${JSON.stringify(event)}, ${JSON.stringify(aggregate)}): start`);
158
159
  const putSnapshot = this.putSnapshot(event, 0, aggregate);
@@ -168,12 +169,23 @@ class EventStoreForDynamoDB {
168
169
  const input = {
169
170
  TransactItems: transactWriteItems,
170
171
  };
171
- yield this.dynamodbClient.send(new client_dynamodb_1.TransactWriteItemsCommand(input));
172
- (_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug(`createEventAndSnapshot(${JSON.stringify(event)}, ${JSON.stringify(aggregate)}): finished`);
172
+ try {
173
+ yield this.dynamodbClient.send(new client_dynamodb_1.TransactWriteItemsCommand(input));
174
+ }
175
+ catch (e) {
176
+ if (e instanceof client_dynamodb_1.TransactionCanceledException &&
177
+ ((_b = e.CancellationReasons) === null || _b === void 0 ? void 0 : _b.some((e) => e.Code == "ConditionalCheckFailed"))) {
178
+ throw new types_1.OptimisticLockError("Optimistic locking failed", e);
179
+ }
180
+ else {
181
+ throw e;
182
+ }
183
+ }
184
+ (_c = this.logger) === null || _c === void 0 ? void 0 : _c.debug(`createEventAndSnapshot(${JSON.stringify(event)}, ${JSON.stringify(aggregate)}): finished`);
173
185
  });
174
186
  }
175
187
  updateEventAndSnapshotOpt(event, version, aggregate) {
176
- var _a, _b;
188
+ var _a, _b, _c;
177
189
  return __awaiter(this, void 0, void 0, function* () {
178
190
  (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`updateEventAndSnapshotOpt(${JSON.stringify(event)}, ${version}, ${JSON.stringify(aggregate)}): start`);
179
191
  const update = this.updateSnapshot(event, 0, version, aggregate);
@@ -189,8 +201,19 @@ class EventStoreForDynamoDB {
189
201
  const input = {
190
202
  TransactItems: transactWriteItems,
191
203
  };
192
- yield this.dynamodbClient.send(new client_dynamodb_1.TransactWriteItemsCommand(input));
193
- (_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug(`updateEventAndSnapshotOpt(${JSON.stringify(event)}, ${version}, ${JSON.stringify(aggregate)}): finished`);
204
+ try {
205
+ yield this.dynamodbClient.send(new client_dynamodb_1.TransactWriteItemsCommand(input));
206
+ }
207
+ catch (e) {
208
+ if (e instanceof client_dynamodb_1.TransactionCanceledException &&
209
+ ((_b = e.CancellationReasons) === null || _b === void 0 ? void 0 : _b.some((e) => e.Code == "ConditionalCheckFailed"))) {
210
+ throw new types_1.OptimisticLockError("Optimistic locking failed", e);
211
+ }
212
+ else {
213
+ throw e;
214
+ }
215
+ }
216
+ (_c = this.logger) === null || _c === void 0 ? void 0 : _c.debug(`updateEventAndSnapshotOpt(${JSON.stringify(event)}, ${version}, ${JSON.stringify(aggregate)}): finished`);
194
217
  });
195
218
  }
196
219
  putJournal(event) {
package/dist/types.d.ts CHANGED
@@ -38,4 +38,7 @@ export interface Logger {
38
38
  warn: (...content: any[]) => void;
39
39
  error: (...content: any[]) => void;
40
40
  }
41
- export { AggregateId, Aggregate, Event, KeyResolver, EventSerializer, SnapshotSerializer, };
41
+ declare class OptimisticLockError extends Error {
42
+ constructor(message: string, cause?: Error);
43
+ }
44
+ export { AggregateId, Aggregate, Event, KeyResolver, EventSerializer, SnapshotSerializer, OptimisticLockError, };
package/dist/types.js CHANGED
@@ -1,2 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OptimisticLockError = void 0;
4
+ class OptimisticLockError extends Error {
5
+ constructor(message, cause) {
6
+ super(message);
7
+ this.name = "OptimisticLockError";
8
+ this.cause = cause;
9
+ if (cause) {
10
+ this.stack = `${this.stack}\nCaused by:\n${cause.stack}`;
11
+ }
12
+ }
13
+ }
14
+ exports.OptimisticLockError = OptimisticLockError;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "event-store-adapter-js",
3
- "version": "2.0.25-snapshot.3",
3
+ "version": "2.1.0",
4
4
  "description": "This library is designed to turn DynamoDB into an Event Store for Event Sourcing.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -5,6 +5,7 @@ import {
5
5
  EventSerializer,
6
6
  KeyResolver,
7
7
  Logger,
8
+ OptimisticLockError,
8
9
  SnapshotSerializer,
9
10
  } from "../types";
10
11
  import {
@@ -13,6 +14,7 @@ import {
13
14
  Put,
14
15
  QueryCommand,
15
16
  QueryCommandInput,
17
+ TransactionCanceledException,
16
18
  TransactWriteItemsCommand,
17
19
  TransactWriteItemsInput,
18
20
  Update,
@@ -309,7 +311,18 @@ class EventStoreForDynamoDB<
309
311
  const input: TransactWriteItemsInput = {
310
312
  TransactItems: transactWriteItems,
311
313
  };
312
- await this.dynamodbClient.send(new TransactWriteItemsCommand(input));
314
+ try {
315
+ await this.dynamodbClient.send(new TransactWriteItemsCommand(input));
316
+ } catch (e) {
317
+ if (
318
+ e instanceof TransactionCanceledException &&
319
+ e.CancellationReasons?.some((e) => e.Code == "ConditionalCheckFailed")
320
+ ) {
321
+ throw new OptimisticLockError("Optimistic locking failed", e);
322
+ } else {
323
+ throw e;
324
+ }
325
+ }
313
326
  this.logger?.debug(
314
327
  `createEventAndSnapshot(${JSON.stringify(event)}, ${JSON.stringify(
315
328
  aggregate,
@@ -340,7 +353,18 @@ class EventStoreForDynamoDB<
340
353
  const input: TransactWriteItemsInput = {
341
354
  TransactItems: transactWriteItems,
342
355
  };
343
- await this.dynamodbClient.send(new TransactWriteItemsCommand(input));
356
+ try {
357
+ await this.dynamodbClient.send(new TransactWriteItemsCommand(input));
358
+ } catch (e) {
359
+ if (
360
+ e instanceof TransactionCanceledException &&
361
+ e.CancellationReasons?.some((e) => e.Code == "ConditionalCheckFailed")
362
+ ) {
363
+ throw new OptimisticLockError("Optimistic locking failed", e);
364
+ } else {
365
+ throw e;
366
+ }
367
+ }
344
368
  this.logger?.debug(
345
369
  `updateEventAndSnapshotOpt(${JSON.stringify(
346
370
  event,
package/src/types.ts CHANGED
@@ -59,6 +59,17 @@ export interface Logger {
59
59
  error: (...content: any[]) => void;
60
60
  }
61
61
 
62
+ class OptimisticLockError extends Error {
63
+ constructor(message: string, cause?: Error) {
64
+ super(message);
65
+ this.name = "OptimisticLockError";
66
+ this.cause = cause;
67
+ if (cause) {
68
+ this.stack = `${this.stack}\nCaused by:\n${cause.stack}`;
69
+ }
70
+ }
71
+ }
72
+
62
73
  export {
63
74
  AggregateId,
64
75
  Aggregate,
@@ -66,4 +77,5 @@ export {
66
77
  KeyResolver,
67
78
  EventSerializer,
68
79
  SnapshotSerializer,
80
+ OptimisticLockError,
69
81
  };