@nimbus-cqrs/eventsourcingdb 2.4.0 → 3.0.0-beta.1
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/README.md +82 -46
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/{esm → dist}/lib/client.d.ts +28 -17
- package/{esm → dist}/lib/client.d.ts.map +1 -1
- package/{esm → dist}/lib/client.js +25 -19
- package/dist/lib/client.js.map +1 -0
- package/dist/lib/consumerRunner.d.ts +62 -0
- package/dist/lib/consumerRunner.d.ts.map +1 -0
- package/dist/lib/consumerRunner.js +386 -0
- package/dist/lib/consumerRunner.js.map +1 -0
- package/dist/lib/eventConsumer.d.ts +94 -0
- package/dist/lib/eventConsumer.d.ts.map +1 -0
- package/dist/lib/eventConsumer.js +2 -0
- package/dist/lib/eventConsumer.js.map +1 -0
- package/{esm → dist}/lib/eventMapping.d.ts +1 -1
- package/dist/lib/eventMapping.d.ts.map +1 -0
- package/{esm → dist}/lib/eventMapping.js +2 -1
- package/dist/lib/eventMapping.js.map +1 -0
- package/dist/lib/eventObserver.d.ts +158 -0
- package/dist/lib/eventObserver.d.ts.map +1 -0
- package/dist/lib/eventObserver.js +339 -0
- package/dist/lib/eventObserver.js.map +1 -0
- package/dist/lib/observerRegistry.d.ts +39 -0
- package/dist/lib/observerRegistry.d.ts.map +1 -0
- package/dist/lib/observerRegistry.js +56 -0
- package/dist/lib/observerRegistry.js.map +1 -0
- package/{esm → dist}/lib/readEvents.d.ts.map +1 -1
- package/{esm → dist}/lib/readEvents.js +3 -2
- package/dist/lib/readEvents.js.map +1 -0
- package/{esm → dist}/lib/tracing.d.ts +3 -0
- package/dist/lib/tracing.d.ts.map +1 -0
- package/{esm → dist}/lib/tracing.js +5 -1
- package/dist/lib/tracing.js.map +1 -0
- package/dist/lib/writeEvents.d.ts +29 -0
- package/dist/lib/writeEvents.d.ts.map +1 -0
- package/dist/lib/writeEvents.js +47 -0
- package/dist/lib/writeEvents.js.map +1 -0
- package/package.json +32 -13
- package/src/index.ts +7 -0
- package/src/lib/client.ts +166 -0
- package/src/lib/consumerRunner.ts +592 -0
- package/src/lib/eventConsumer.ts +95 -0
- package/src/lib/eventMapping.ts +115 -0
- package/src/lib/eventObserver.ts +620 -0
- package/src/lib/observerRegistry.ts +69 -0
- package/src/lib/readEvents.ts +28 -0
- package/src/lib/tracing.ts +295 -0
- package/src/lib/writeEvents.ts +72 -0
- package/esm/_dnt.polyfills.d.ts +0 -7
- package/esm/_dnt.polyfills.d.ts.map +0 -1
- package/esm/_dnt.polyfills.js +0 -1
- package/esm/_dnt.shims.d.ts +0 -2
- package/esm/_dnt.shims.d.ts.map +0 -1
- package/esm/_dnt.shims.js +0 -57
- package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.d.ts +0 -700
- package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.js +0 -903
- package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.d.ts +0 -14
- package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.js +0 -65
- package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.d.ts +0 -20
- package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.js +0 -43
- package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.d.ts +0 -44
- package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.js +0 -47
- package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.d.ts +0 -44
- package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.js +0 -51
- package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.d.ts +0 -31
- package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.js +0 -37
- package/esm/index.d.ts +0 -7
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -6
- package/esm/lib/eventMapping.d.ts.map +0 -1
- package/esm/lib/eventObserver.d.ts +0 -133
- package/esm/lib/eventObserver.d.ts.map +0 -1
- package/esm/lib/eventObserver.js +0 -291
- package/esm/lib/tracing.d.ts.map +0 -1
- package/esm/lib/writeEvents.d.ts +0 -11
- package/esm/lib/writeEvents.d.ts.map +0 -1
- package/esm/lib/writeEvents.js +0 -31
- package/esm/package.json +0 -3
- /package/{esm → dist}/lib/readEvents.d.ts +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { context as otelContext,
|
|
1
|
+
import { metrics, context as otelContext, propagation, SpanKind, SpanStatusCode, trace, } from '@opentelemetry/api';
|
|
2
2
|
export const tracer = trace.getTracer('nimbus');
|
|
3
3
|
export const DB_SYSTEM = 'eventsourcingdb';
|
|
4
4
|
const meter = metrics.getMeter('nimbus');
|
|
@@ -11,6 +11,9 @@ const operationDuration = meter.createHistogram('eventsourcingdb_operation_durat
|
|
|
11
11
|
});
|
|
12
12
|
/**
|
|
13
13
|
* Histogram of serialized Nimbus event sizes written via writeEvents.
|
|
14
|
+
*
|
|
15
|
+
* Recorded without attributes so the series count does not grow with
|
|
16
|
+
* streams or event types.
|
|
14
17
|
*/
|
|
15
18
|
export const eventSizeBytes = meter.createHistogram('eventsourcingdb_event_size_bytes', {
|
|
16
19
|
description: 'Size of events written to EventSourcingDB in bytes',
|
|
@@ -186,3 +189,4 @@ export async function* withAsyncGeneratorSpan(operation, fn) {
|
|
|
186
189
|
span.end();
|
|
187
190
|
}
|
|
188
191
|
}
|
|
192
|
+
//# sourceMappingURL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,EAAE,CAAC,CAAC;QAC9D,iBAAiB,CAAC,MAAM,CACpB,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,GAAG,IAAI,EACtC,YAAY,CACf,CAAC;QAEF,IAAI,CAAC,SAAS,CAAC;YACX,IAAI,EAAE,cAAc,CAAC,KAAK;YAC1B,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe;SACpE,CAAC,CAAC;QACH,IAAI,CAAC,eAAe,CAChB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAC9D,CAAC;QACF,MAAM,KAAK,CAAC;IAChB,CAAC;YAAS,CAAC;QACP,IAAI,CAAC,GAAG,EAAE,CAAC;IACf,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Event } from '@nimbus-cqrs/core';
|
|
2
|
+
import type { Event as EventSourcingDBEvent, Precondition } from 'eventsourcingdb';
|
|
3
|
+
/**
|
|
4
|
+
* Writes one or more Nimbus events to EventSourcingDB. Each event is
|
|
5
|
+
* converted into an EventSourcingDB event candidate before being persisted.
|
|
6
|
+
*
|
|
7
|
+
* Returns the written EventSourcingDB events including the fields assigned
|
|
8
|
+
* by the server (`id`, `time`, `hash`, ...). The `id` of the last written
|
|
9
|
+
* event is the new revision of the subject and can be used with the
|
|
10
|
+
* `isSubjectOnEventId` precondition for subsequent writes.
|
|
11
|
+
*
|
|
12
|
+
* The SDK never retries a write on its own. Errors are passed through:
|
|
13
|
+
* - A `DbApiError` (from `eventsourcingdb`) when the server rejects the
|
|
14
|
+
* write, e.g. with `code === ErrorCode.preconditionViolated` when a
|
|
15
|
+
* precondition does not hold.
|
|
16
|
+
* - An `OutcomeUnknownError` when the write may have been stored although
|
|
17
|
+
* the response was lost (stalled connection, abort after sending, ...).
|
|
18
|
+
* Its `cause` holds the original error. Read the subject back before
|
|
19
|
+
* writing again.
|
|
20
|
+
*
|
|
21
|
+
* @param events - The Nimbus events to write.
|
|
22
|
+
* @param preconditions - Optional preconditions that must be met for the write to succeed.
|
|
23
|
+
* @param signal - An optional abort signal to cancel the write. Aborting
|
|
24
|
+
* after the request has been sent does not undo the write; the call then
|
|
25
|
+
* throws an `OutcomeUnknownError`.
|
|
26
|
+
* @returns The written EventSourcingDB events.
|
|
27
|
+
*/
|
|
28
|
+
export declare const writeEvents: (events: Event[], preconditions?: Precondition[], signal?: AbortSignal) => Promise<EventSourcingDBEvent[]>;
|
|
29
|
+
//# sourceMappingURL=writeEvents.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writeEvents.d.ts","sourceRoot":"","sources":["../../src/lib/writeEvents.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE/C,OAAO,KAAK,EAER,KAAK,IAAI,oBAAoB,EAC7B,YAAY,EACf,MAAM,iBAAiB,CAAC;AAazB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,WAAW,GACpB,QAAQ,KAAK,EAAE,EACf,gBAAgB,YAAY,EAAE,EAC9B,SAAS,WAAW,KACrB,OAAO,CAAC,oBAAoB,EAAE,CAuBhC,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { context, propagation } from '@opentelemetry/api';
|
|
2
|
+
import { getEventSourcingDBClient } from "./client.js";
|
|
3
|
+
import { nimbusEventToEventSourcingDBEventCandidate } from "./eventMapping.js";
|
|
4
|
+
import { eventSizeBytes, withSpan } from "./tracing.js";
|
|
5
|
+
const textEncoder = new TextEncoder();
|
|
6
|
+
/**
|
|
7
|
+
* Returns the UTF-8 byte length of a serialized Nimbus event.
|
|
8
|
+
*/
|
|
9
|
+
const getEventSizeBytes = (event) => textEncoder.encode(JSON.stringify(event)).length;
|
|
10
|
+
/**
|
|
11
|
+
* Writes one or more Nimbus events to EventSourcingDB. Each event is
|
|
12
|
+
* converted into an EventSourcingDB event candidate before being persisted.
|
|
13
|
+
*
|
|
14
|
+
* Returns the written EventSourcingDB events including the fields assigned
|
|
15
|
+
* by the server (`id`, `time`, `hash`, ...). The `id` of the last written
|
|
16
|
+
* event is the new revision of the subject and can be used with the
|
|
17
|
+
* `isSubjectOnEventId` precondition for subsequent writes.
|
|
18
|
+
*
|
|
19
|
+
* The SDK never retries a write on its own. Errors are passed through:
|
|
20
|
+
* - A `DbApiError` (from `eventsourcingdb`) when the server rejects the
|
|
21
|
+
* write, e.g. with `code === ErrorCode.preconditionViolated` when a
|
|
22
|
+
* precondition does not hold.
|
|
23
|
+
* - An `OutcomeUnknownError` when the write may have been stored although
|
|
24
|
+
* the response was lost (stalled connection, abort after sending, ...).
|
|
25
|
+
* Its `cause` holds the original error. Read the subject back before
|
|
26
|
+
* writing again.
|
|
27
|
+
*
|
|
28
|
+
* @param events - The Nimbus events to write.
|
|
29
|
+
* @param preconditions - Optional preconditions that must be met for the write to succeed.
|
|
30
|
+
* @param signal - An optional abort signal to cancel the write. Aborting
|
|
31
|
+
* after the request has been sent does not undo the write; the call then
|
|
32
|
+
* throws an `OutcomeUnknownError`.
|
|
33
|
+
* @returns The written EventSourcingDB events.
|
|
34
|
+
*/
|
|
35
|
+
export const writeEvents = (events, preconditions, signal) => {
|
|
36
|
+
return withSpan('writeEvents', async () => {
|
|
37
|
+
const eventSourcingDBClient = getEventSourcingDBClient();
|
|
38
|
+
const carrier = {};
|
|
39
|
+
propagation.inject(context.active(), carrier);
|
|
40
|
+
const eventCandidates = events.map((event) => {
|
|
41
|
+
eventSizeBytes.record(getEventSizeBytes(event));
|
|
42
|
+
return nimbusEventToEventSourcingDBEventCandidate(event, carrier.traceparent, carrier.tracestate);
|
|
43
|
+
});
|
|
44
|
+
return await eventSourcingDBClient.writeEvents(eventCandidates, preconditions, signal);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47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,aAAa,EACb,MAAM,CACT,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nimbus-cqrs/eventsourcingdb",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta.1",
|
|
4
4
|
"description": "Simplify Event-Driven Applications - EventSourcingDB integration for the Nimbus framework.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nimbus",
|
|
@@ -13,34 +13,53 @@
|
|
|
13
13
|
"event-store",
|
|
14
14
|
"event-stream"
|
|
15
15
|
],
|
|
16
|
+
"license": "Apache-2.0",
|
|
16
17
|
"author": "Daniel Gördes <d.goerdes@overlap.at> (https://overlap.at)",
|
|
17
18
|
"homepage": "https://nimbus.overlap.at",
|
|
18
19
|
"repository": {
|
|
19
20
|
"type": "git",
|
|
20
|
-
"url": "https://github.com/overlap-dev/Nimbus"
|
|
21
|
+
"url": "https://github.com/overlap-dev/Nimbus",
|
|
22
|
+
"directory": "packages/eventsourcingdb"
|
|
21
23
|
},
|
|
22
|
-
"license": "Apache-2.0",
|
|
23
24
|
"bugs": {
|
|
24
25
|
"url": "https://github.com/overlap-dev/Nimbus/issues"
|
|
25
26
|
},
|
|
26
|
-
"
|
|
27
|
+
"type": "module",
|
|
27
28
|
"exports": {
|
|
28
29
|
".": {
|
|
29
|
-
"
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
30
32
|
}
|
|
31
33
|
},
|
|
32
|
-
"
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"src",
|
|
37
|
+
"!src/**/*.test.ts",
|
|
38
|
+
"!src/**/*.metrics_worker.ts"
|
|
39
|
+
],
|
|
33
40
|
"engines": {
|
|
34
|
-
"node": ">=
|
|
41
|
+
"node": ">=24"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
35
45
|
},
|
|
36
46
|
"dependencies": {
|
|
47
|
+
"@nimbus-cqrs/core": "^3.0.0-beta.1",
|
|
37
48
|
"@opentelemetry/api": "^1.9.1",
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
49
|
+
"ulid": "^3.0.2"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"eventsourcingdb": "^2.0.0"
|
|
41
53
|
},
|
|
42
54
|
"devDependencies": {
|
|
43
|
-
"@
|
|
55
|
+
"@opentelemetry/sdk-metrics": "^3.0.0",
|
|
56
|
+
"@types/node": "^26.6.5",
|
|
57
|
+
"eventsourcingdb": "^2.2.3",
|
|
58
|
+
"typescript": "^5.9.3"
|
|
44
59
|
},
|
|
45
|
-
"
|
|
46
|
-
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsc -p tsconfig.build.json",
|
|
62
|
+
"check": "tsc --noEmit",
|
|
63
|
+
"test": "node --test --test-force-exit"
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './lib/client.ts';
|
|
2
|
+
export * from './lib/eventConsumer.ts';
|
|
3
|
+
export * from './lib/eventMapping.ts';
|
|
4
|
+
export * from './lib/eventObserver.ts';
|
|
5
|
+
export * from './lib/observerRegistry.ts';
|
|
6
|
+
export * from './lib/readEvents.ts';
|
|
7
|
+
export * from './lib/writeEvents.ts';
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { GenericException, getLogger } from '@nimbus-cqrs/core';
|
|
2
|
+
import { Client } from 'eventsourcingdb';
|
|
3
|
+
import { type EventObserver, initEventObserver } from './eventObserver.ts';
|
|
4
|
+
|
|
5
|
+
let eventSourcingDBClient: Client | null = null;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Configuration options for setting up the EventSourcingDB client.
|
|
9
|
+
*/
|
|
10
|
+
export type SetupEventSourcingDBClientInput = {
|
|
11
|
+
/**
|
|
12
|
+
* The URL of the EventSourcingDB server.
|
|
13
|
+
*/
|
|
14
|
+
url: URL;
|
|
15
|
+
/**
|
|
16
|
+
* The API token for authenticating with EventSourcingDB.
|
|
17
|
+
*/
|
|
18
|
+
apiToken: string;
|
|
19
|
+
/**
|
|
20
|
+
* The stall timeout in milliseconds. A request fails with a
|
|
21
|
+
* `ConnectionStalledError` once nothing arrives from the server for
|
|
22
|
+
* this long. Every item received (events, rows, heartbeats) resets the
|
|
23
|
+
* timer, so long-running streams are never cut off while they are
|
|
24
|
+
* healthy. Defaults to 30000ms (the SDK default).
|
|
25
|
+
*/
|
|
26
|
+
stallTimeout?: number;
|
|
27
|
+
/**
|
|
28
|
+
* An optional array of event observers to observe events.
|
|
29
|
+
*/
|
|
30
|
+
eventObservers?: EventObserver[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Initialize and configure the EventSourcingDB client.
|
|
35
|
+
*
|
|
36
|
+
* This function creates a singleton client instance, verifies connectivity by pinging
|
|
37
|
+
* the server, and validates the provided API token. It should be called once at
|
|
38
|
+
* application startup before using {@link getEventSourcingDBClient}.
|
|
39
|
+
*
|
|
40
|
+
* Optionally, you can provide event observers. They are started one after
|
|
41
|
+
* another once the client is initialized. Each one loads its consumers'
|
|
42
|
+
* checkpoints before the function resolves, so a checkpoint store that
|
|
43
|
+
* cannot be reached fails the setup.
|
|
44
|
+
*
|
|
45
|
+
* @param {SetupEventSourcingDBClientInput} options - The configuration options
|
|
46
|
+
* @param {URL} options.url - The URL of the EventSourcingDB server
|
|
47
|
+
* @param {string} options.apiToken - The API token for authentication
|
|
48
|
+
* @param {number} [options.stallTimeout] - Optional stall timeout in milliseconds (defaults to 30000)
|
|
49
|
+
* @param {EventObserver[]} [options.eventObservers] - Optional array of event observers
|
|
50
|
+
*
|
|
51
|
+
* @throws {GenericException} If the connection to EventSourcingDB fails
|
|
52
|
+
* @throws {GenericException} If the API token is invalid
|
|
53
|
+
* @throws {GenericException} If an event observer cannot be started
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { InMemoryCheckpointStore } from '@nimbus-cqrs/core';
|
|
58
|
+
* import { setupEventSourcingDBClient } from '@nimbus-cqrs/eventsourcingdb';
|
|
59
|
+
*
|
|
60
|
+
* await setupEventSourcingDBClient({
|
|
61
|
+
* url: new URL(process.env.ESDB_URL ?? ''),
|
|
62
|
+
* apiToken: process.env.ESDB_API_TOKEN ?? '',
|
|
63
|
+
* eventObservers: [
|
|
64
|
+
* {
|
|
65
|
+
* name: 'users',
|
|
66
|
+
* subject: '/users',
|
|
67
|
+
* recursive: true,
|
|
68
|
+
* checkpointStore: new InMemoryCheckpointStore(),
|
|
69
|
+
* consumers: [
|
|
70
|
+
* {
|
|
71
|
+
* name: 'users.projection',
|
|
72
|
+
* handles: (event) => event.type.startsWith('io.nimbus.users.'),
|
|
73
|
+
* handle: async (event) => {
|
|
74
|
+
* console.log('Received event:', event);
|
|
75
|
+
* },
|
|
76
|
+
* },
|
|
77
|
+
* ],
|
|
78
|
+
* connectionRetryOptions: {
|
|
79
|
+
* maxRetries: 3,
|
|
80
|
+
* initialRetryDelayMs: 3000,
|
|
81
|
+
* },
|
|
82
|
+
* handlerRetryOptions: {
|
|
83
|
+
* maxRetries: 3,
|
|
84
|
+
* initialRetryDelayMs: 3000,
|
|
85
|
+
* },
|
|
86
|
+
* },
|
|
87
|
+
* ],
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export const setupEventSourcingDBClient = async ({
|
|
92
|
+
url,
|
|
93
|
+
apiToken,
|
|
94
|
+
stallTimeout,
|
|
95
|
+
eventObservers,
|
|
96
|
+
}: SetupEventSourcingDBClientInput): Promise<void> => {
|
|
97
|
+
eventSourcingDBClient = new Client(
|
|
98
|
+
url,
|
|
99
|
+
apiToken,
|
|
100
|
+
stallTimeout !== undefined ? { stallTimeout } : undefined,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
await eventSourcingDBClient.ping();
|
|
105
|
+
} catch (error) {
|
|
106
|
+
getLogger().critical({
|
|
107
|
+
category: 'Nimbus',
|
|
108
|
+
message: 'Could not connect to EventSourcingDB',
|
|
109
|
+
error: error as Error,
|
|
110
|
+
});
|
|
111
|
+
throw new GenericException('Could not connect to EventSourcingDB');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
await eventSourcingDBClient.verifyApiToken();
|
|
116
|
+
} catch (error) {
|
|
117
|
+
getLogger().error({
|
|
118
|
+
category: 'Nimbus',
|
|
119
|
+
message: 'Invalid API token. Please check your API token.',
|
|
120
|
+
error: error as Error,
|
|
121
|
+
});
|
|
122
|
+
throw new GenericException(
|
|
123
|
+
'Invalid API token. Please check your API token.',
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getLogger().info({
|
|
128
|
+
category: 'Nimbus',
|
|
129
|
+
message: 'EventSourcingDB client initialized successfully',
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (eventObservers?.length) {
|
|
133
|
+
// Sequential on purpose: observers register in declaration order
|
|
134
|
+
// and the first one that cannot start fails the setup.
|
|
135
|
+
for (const eventObserver of eventObservers) {
|
|
136
|
+
await initEventObserver(eventObserver); // NOSONAR
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Get the EventSourcingDB client instance.
|
|
143
|
+
*
|
|
144
|
+
* Returns the singleton client instance that was created by {@link setupEventSourcingDBClient}.
|
|
145
|
+
* This function must be called after the client has been initialized.
|
|
146
|
+
*
|
|
147
|
+
* @returns {Client} The EventSourcingDB client instance
|
|
148
|
+
*
|
|
149
|
+
* @throws {GenericException} If the client has not been initialized via setupEventSourcingDBClient
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { getEventSourcingDBClient } from '@nimbus-cqrs/eventsourcingdb';
|
|
154
|
+
*
|
|
155
|
+
* const client = getEventSourcingDBClient();
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export const getEventSourcingDBClient = (): Client => {
|
|
159
|
+
if (!eventSourcingDBClient) {
|
|
160
|
+
throw new GenericException(
|
|
161
|
+
'EventSourcingDB client not yet initialized. Please call setupEventSourcingDBClient() first.',
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return eventSourcingDBClient;
|
|
166
|
+
};
|