From d7662bb563a0e4494294e8f9639b643761b7df72 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Thu, 4 Jan 2024 12:48:06 +0100 Subject: [PATCH] Renovated snapshots example to the latest ESDB client syntax --- samples/decider/docker-compose.yml | 4 +- .../eventStoreDB/eventStoreDBContainer.ts | 2 +- .../eventStoreDB/eventStoreDBContainer.ts | 2 +- .../endingShift/handler.int.test.ts | 2 +- .../endingShift/route.api.test.ts | 2 +- .../placeAtWorkStation/handler.int.test.ts | 2 +- .../placeAtWorkStation/route.api.test.ts | 2 +- .../registeringTransaction/route.api.test.ts | 2 +- .../startingShift/handler.int.test.ts | 2 +- .../startingShift/route.api.test.ts | 2 +- .../snapshots/src/cashRegisters/testFlow.ts | 72 +++++++++--------- .../core/eventStore/reading/readFromStream.ts | 51 +++++++------ .../eventStore/reading/readStreamMetadata.ts | 8 +- .../eventStore/snapshotting/snapshotEvent.ts | 10 +-- samples/snapshots/src/core/events/event.ts | 12 ++- .../src/testing/assertions/streams.ts | 27 +++++-- .../src/testing/builders/setupCashRegister.ts | 7 +- .../src/testing/builders/setupStartedShift.ts | 8 +- .../eventStoreDBContainer.int.test.ts | 2 +- .../eventStoreDB/eventStoreDBContainer.ts | 74 ++++++++++--------- .../eventStoreDB/eventStoreDBContainer.ts | 2 +- 21 files changed, 167 insertions(+), 128 deletions(-) diff --git a/samples/decider/docker-compose.yml b/samples/decider/docker-compose.yml index af465c4d..10d55b33 100644 --- a/samples/decider/docker-compose.yml +++ b/samples/decider/docker-compose.yml @@ -4,8 +4,8 @@ services: ####################################################### # EventStoreDB ####################################################### - eventstoredb: - image: eventstore/eventstore:22.10.0-buster-slim + eventstore.db: + image: eventstore/eventstore:23.10.0-bookworm-slim # use this image if you're running ARM-based proc like Apple M1 # image: eventstore/eventstore:23.10.0-alpha-arm64v8 environment: diff --git a/samples/eventsVersioning/src/core/testing/eventStoreDB/eventStoreDBContainer.ts b/samples/eventsVersioning/src/core/testing/eventStoreDB/eventStoreDBContainer.ts index 0885252c..b7001c5d 100644 --- a/samples/eventsVersioning/src/core/testing/eventStoreDB/eventStoreDBContainer.ts +++ b/samples/eventsVersioning/src/core/testing/eventStoreDB/eventStoreDBContainer.ts @@ -7,7 +7,7 @@ const EVENTSTOREDB_PORT = 2113; const EVENTSTOREDB_TCP_PORT = 1113; const EVENTSTOREDB_TCP_PORTS = [EVENTSTOREDB_TCP_PORT, EVENTSTOREDB_PORT]; const EVENTSTOREDB_IMAGE_NAME = 'eventstore/eventstore'; -const EVENTSTOREDB_IMAGE_TAG = '22.10.1-buster-slim'; +const EVENTSTOREDB_IMAGE_TAG = '23.10.0-bookworm-slim'; export class EventStoreDBContainer extends GenericContainer { private readonly tcpPorts = EVENTSTOREDB_TCP_PORTS; diff --git a/samples/hotelManagement/src/core/testing/eventStoreDB/eventStoreDBContainer.ts b/samples/hotelManagement/src/core/testing/eventStoreDB/eventStoreDBContainer.ts index 0885252c..b7001c5d 100644 --- a/samples/hotelManagement/src/core/testing/eventStoreDB/eventStoreDBContainer.ts +++ b/samples/hotelManagement/src/core/testing/eventStoreDB/eventStoreDBContainer.ts @@ -7,7 +7,7 @@ const EVENTSTOREDB_PORT = 2113; const EVENTSTOREDB_TCP_PORT = 1113; const EVENTSTOREDB_TCP_PORTS = [EVENTSTOREDB_TCP_PORT, EVENTSTOREDB_PORT]; const EVENTSTOREDB_IMAGE_NAME = 'eventstore/eventstore'; -const EVENTSTOREDB_IMAGE_TAG = '22.10.1-buster-slim'; +const EVENTSTOREDB_IMAGE_TAG = '23.10.0-bookworm-slim'; export class EventStoreDBContainer extends GenericContainer { private readonly tcpPorts = EVENTSTOREDB_TCP_PORTS; diff --git a/samples/snapshots/src/cashRegisters/endingShift/handler.int.test.ts b/samples/snapshots/src/cashRegisters/endingShift/handler.int.test.ts index 28f57abd..c1c10fe9 100644 --- a/samples/snapshots/src/cashRegisters/endingShift/handler.int.test.ts +++ b/samples/snapshots/src/cashRegisters/endingShift/handler.int.test.ts @@ -27,7 +27,7 @@ describe('EndShift command', () => { const streamName = getCashRegisterStreamName(cashRegisterId); beforeAll(async () => { - esdbContainer = await new EventStoreDBContainer().startContainer(); + esdbContainer = await new EventStoreDBContainer().start(); config.eventStoreDB.connectionString = esdbContainer.getConnectionString(); eventStore = esdbContainer.getClient(); diff --git a/samples/snapshots/src/cashRegisters/endingShift/route.api.test.ts b/samples/snapshots/src/cashRegisters/endingShift/route.api.test.ts index 3c6ca239..f578cb3b 100644 --- a/samples/snapshots/src/cashRegisters/endingShift/route.api.test.ts +++ b/samples/snapshots/src/cashRegisters/endingShift/route.api.test.ts @@ -12,7 +12,7 @@ describe('DELETE /cash-registers/:id/shifts', () => { let esdbContainer: StartedEventStoreDBContainer; beforeAll(async () => { - esdbContainer = await new EventStoreDBContainer().startContainer(); + esdbContainer = await new EventStoreDBContainer().start(); config.eventStoreDB.connectionString = esdbContainer.getConnectionString(); console.log(config.eventStoreDB.connectionString); }); diff --git a/samples/snapshots/src/cashRegisters/placeAtWorkStation/handler.int.test.ts b/samples/snapshots/src/cashRegisters/placeAtWorkStation/handler.int.test.ts index 7a382dd5..743cf5bb 100644 --- a/samples/snapshots/src/cashRegisters/placeAtWorkStation/handler.int.test.ts +++ b/samples/snapshots/src/cashRegisters/placeAtWorkStation/handler.int.test.ts @@ -19,7 +19,7 @@ describe('PlaceAtWorkStation command', () => { let eventStore: EventStoreDBClient; beforeAll(async () => { - esdbContainer = await new EventStoreDBContainer().startContainer(); + esdbContainer = await new EventStoreDBContainer().start(); config.eventStoreDB.connectionString = esdbContainer.getConnectionString(); eventStore = esdbContainer.getClient(); diff --git a/samples/snapshots/src/cashRegisters/placeAtWorkStation/route.api.test.ts b/samples/snapshots/src/cashRegisters/placeAtWorkStation/route.api.test.ts index 1cc90f98..1b99581e 100644 --- a/samples/snapshots/src/cashRegisters/placeAtWorkStation/route.api.test.ts +++ b/samples/snapshots/src/cashRegisters/placeAtWorkStation/route.api.test.ts @@ -11,7 +11,7 @@ describe('POST /cash-register/', () => { let esdbContainer: StartedEventStoreDBContainer; beforeAll(async () => { - esdbContainer = await new EventStoreDBContainer().startContainer(); + esdbContainer = await new EventStoreDBContainer().start(); config.eventStoreDB.connectionString = esdbContainer.getConnectionString(); }); diff --git a/samples/snapshots/src/cashRegisters/registeringTransaction/route.api.test.ts b/samples/snapshots/src/cashRegisters/registeringTransaction/route.api.test.ts index 258f29a0..86b0ffd3 100644 --- a/samples/snapshots/src/cashRegisters/registeringTransaction/route.api.test.ts +++ b/samples/snapshots/src/cashRegisters/registeringTransaction/route.api.test.ts @@ -12,7 +12,7 @@ describe('POST /cash-registers/:id/transactions', () => { let esdbContainer: StartedEventStoreDBContainer; beforeAll(async () => { - esdbContainer = await new EventStoreDBContainer().startContainer(); + esdbContainer = await new EventStoreDBContainer().start(); config.eventStoreDB.connectionString = esdbContainer.getConnectionString(); console.log(config.eventStoreDB.connectionString); }); diff --git a/samples/snapshots/src/cashRegisters/startingShift/handler.int.test.ts b/samples/snapshots/src/cashRegisters/startingShift/handler.int.test.ts index d4208484..dc77c42f 100644 --- a/samples/snapshots/src/cashRegisters/startingShift/handler.int.test.ts +++ b/samples/snapshots/src/cashRegisters/startingShift/handler.int.test.ts @@ -27,7 +27,7 @@ describe('EndShift command', () => { const streamName = getCashRegisterStreamName(cashRegisterId); beforeAll(async () => { - esdbContainer = await new EventStoreDBContainer().startContainer(); + esdbContainer = await new EventStoreDBContainer().start(); config.eventStoreDB.connectionString = esdbContainer.getConnectionString(); eventStore = esdbContainer.getClient(); diff --git a/samples/snapshots/src/cashRegisters/startingShift/route.api.test.ts b/samples/snapshots/src/cashRegisters/startingShift/route.api.test.ts index 6693ea85..d45e5c59 100644 --- a/samples/snapshots/src/cashRegisters/startingShift/route.api.test.ts +++ b/samples/snapshots/src/cashRegisters/startingShift/route.api.test.ts @@ -12,7 +12,7 @@ describe('POST /cash-registers/:id/shifts', () => { let esdbContainer: StartedEventStoreDBContainer; beforeAll(async () => { - esdbContainer = await new EventStoreDBContainer().startContainer(); + esdbContainer = await new EventStoreDBContainer().start(); config.eventStoreDB.connectionString = esdbContainer.getConnectionString(); console.log(config.eventStoreDB.connectionString); }); diff --git a/samples/snapshots/src/cashRegisters/testFlow.ts b/samples/snapshots/src/cashRegisters/testFlow.ts index 5afbb215..7ee8b56d 100644 --- a/samples/snapshots/src/cashRegisters/testFlow.ts +++ b/samples/snapshots/src/cashRegisters/testFlow.ts @@ -9,6 +9,7 @@ import { Position, ResolvedEvent, START, + StreamNotFoundError, } from '@eventstore/db-client'; import { AppendToStreamOptions, @@ -22,21 +23,21 @@ import { export type Event< EventType extends string = string, - EventData extends object = object, - EventMetadata extends object = object, -> = { - type: EventType; - data: EventData; - metadata?: EventMetadata; -}; - -type SnapshotMetadata = { - snapshottedStreamRevision: string; -}; + EventData extends Record = Record, + EventMetadata extends Record = Record, +> = Readonly<{ + type: Readonly; + data: Readonly; + metadata?: Readonly; +}>; + +type SnapshotMetadata = Readonly<{ + snapshottedStreamVersion: string; +}>; type SnapshotEvent< EventType extends string = string, - EventData extends object = object, + EventData extends Record = Record, EventMetadata extends SnapshotMetadata = SnapshotMetadata, > = Event & { metadata: Readonly; @@ -51,8 +52,8 @@ export type Command< CommandData extends Record = Record, CommandMetadata extends Record = Record, > = { - readonly type: CommandType; - readonly data: CommandData; + type: Readonly; + data: Readonly; metadata?: Readonly; }; @@ -67,22 +68,26 @@ async function readFromStream( streamName: string, options?: ReadStreamOptions, ): Promise { + const events = []; try { - const events = await eventStore.readStream(streamName, options); - - return events - .filter((resolvedEvent) => !!resolvedEvent.event) - .map((resolvedEvent) => { - return { - type: resolvedEvent.event!.type, - data: resolvedEvent.event!.data, - metadata: resolvedEvent.event!.metadata, - }; + for await (const { event } of eventStore.readStream( + streamName, + options, + )) { + if (!event) continue; + + events.push({ + type: event.type, + data: event.data, + metadata: event.metadata, }); + } + return events; } catch (error) { - if (error.type == ErrorType.STREAM_NOT_FOUND) { + if (error instanceof StreamNotFoundError) { return 'STREAM_NOT_FOUND'; } + throw error; } } @@ -126,7 +131,7 @@ async function readEventsFromExternalSnapshot< const snapshot = await getLastSnapshot(streamName); const lastSnapshotRevision = snapshot - ? BigInt(snapshot.metadata.snapshottedStreamRevision) + ? BigInt(snapshot.metadata.snapshottedStreamVersion) : undefined; const streamEvents = await readFromStream( @@ -194,7 +199,7 @@ async function getLastSnapshotRevisionFromStreamMetadata( ); return streamMetadata - ? BigInt(streamMetadata.snapshottedStreamRevision) + ? BigInt(streamMetadata.snapshottedStreamVersion) : undefined; } @@ -274,7 +279,7 @@ async function appendEventAndExternalSnapshot< return appendResult; } -function appendSnapshotToSeparateStream< +async function appendSnapshotToSeparateStream< SnapshotStreamEvent extends SnapshotEvent, >( eventStore: EventStoreDBClient, @@ -288,7 +293,7 @@ function appendSnapshotToSeparateStream< // set snapshot stream metadata $maxCount to 1. // This will make sure that there is only one snapshot event. if (lastSnapshotRevision === undefined) { - eventStore.setStreamMetadata(snapshotStreamName, { maxCount: 1 }); + await eventStore.setStreamMetadata(snapshotStreamName, { maxCount: 1 }); } return appendToStream(eventStore, snapshotStreamName, [snapshot]); @@ -317,11 +322,10 @@ async function appendEventAndSnapshotToTheSameStream< eventsToAppend, ); - const snapshottedStreamRevision = - appendResult.nextExpectedRevision.toString(); + const snapshottedStreamVersion = appendResult.nextExpectedRevision.toString(); await eventStore.setStreamMetadata(streamName, { - snapshottedStreamRevision, + snapshottedStreamVersion, }); return appendResult; @@ -497,7 +501,7 @@ function buildCashRegisterSnapshot( return { type: 'cash-register-snapshoted', data: currentState, - metadata: { snapshottedStreamRevision: newStreamRevision.toString() }, + metadata: { snapshottedStreamVersion: newStreamRevision.toString() }, }; } @@ -521,7 +525,7 @@ function tryBuildCashRegisterSnapshotNoMetadata( return { type: 'cash-register-snapshoted', data: currentState, - metadata: { snapshottedStreamRevision: undefined! }, + metadata: { snapshottedStreamVersion: undefined! }, }; } diff --git a/samples/snapshots/src/core/eventStore/reading/readFromStream.ts b/samples/snapshots/src/core/eventStore/reading/readFromStream.ts index 07b8ab85..4ffa3371 100644 --- a/samples/snapshots/src/core/eventStore/reading/readFromStream.ts +++ b/samples/snapshots/src/core/eventStore/reading/readFromStream.ts @@ -1,4 +1,8 @@ -import { ErrorType, EventStoreDBClient } from '@eventstore/db-client'; +import { + ErrorType, + EventStoreDBClient, + StreamNotFoundError, +} from '@eventstore/db-client'; import { ReadStreamOptions } from '@eventstore/db-client/dist/streams'; import { STREAM_NOT_FOUND } from '.'; @@ -14,29 +18,32 @@ export async function readFromStream( streamName: string, options?: ReadFromStreamOptions, ): Promise> { + const events = []; + + const toPosition = options?.toPosition; + try { - const events = await eventStore.readStream(streamName, options); - - const toPosition = options?.toPosition; - - return success( - events - .filter( - (resolvedEvent) => - !!resolvedEvent.event && - (toPosition === undefined || - (resolvedEvent.commitPosition ?? 0) < toPosition), - ) - .map((resolvedEvent) => { - return { - type: resolvedEvent.event!.type, - data: resolvedEvent.event!.data, - metadata: resolvedEvent.event?.metadata, - }; - }), - ); + for await (const { event } of eventStore.readStream( + streamName, + options, + )) { + if (!event) continue; + + if ( + toPosition != undefined && + (event.position?.commit ?? 0) >= toPosition + ) + break; + + events.push({ + type: event.type, + data: event.data, + metadata: event.metadata, + }); + } + return success(events); } catch (error) { - if (error.type == ErrorType.STREAM_NOT_FOUND) { + if (error instanceof StreamNotFoundError) { return failure('STREAM_NOT_FOUND'); } diff --git a/samples/snapshots/src/core/eventStore/reading/readStreamMetadata.ts b/samples/snapshots/src/core/eventStore/reading/readStreamMetadata.ts index ed3a273d..bfa531d4 100644 --- a/samples/snapshots/src/core/eventStore/reading/readStreamMetadata.ts +++ b/samples/snapshots/src/core/eventStore/reading/readStreamMetadata.ts @@ -1,4 +1,8 @@ -import { ErrorType, EventStoreDBClient } from '@eventstore/db-client'; +import { + ErrorType, + EventStoreDBClient, + StreamNotFoundError, +} from '@eventstore/db-client'; import { GetStreamMetadataOptions } from '@eventstore/db-client/dist/streams'; import { STREAM_NOT_FOUND, METADATA_NOT_FOUND } from './'; import { failure, Result, success } from '../../primitives'; @@ -20,7 +24,7 @@ export async function readStreamMetadata< return success(result.metadata); } catch (error) { - if (error.type == ErrorType.STREAM_NOT_FOUND) { + if (error instanceof StreamNotFoundError) { return failure('STREAM_NOT_FOUND'); } diff --git a/samples/snapshots/src/core/eventStore/snapshotting/snapshotEvent.ts b/samples/snapshots/src/core/eventStore/snapshotting/snapshotEvent.ts index 83962363..b5568abc 100644 --- a/samples/snapshots/src/core/eventStore/snapshotting/snapshotEvent.ts +++ b/samples/snapshots/src/core/eventStore/snapshotting/snapshotEvent.ts @@ -1,14 +1,14 @@ import { Event } from '../../events'; -export type SnapshotMetadata = { + +export type SnapshotMetadata = Readonly<{ snapshottedStreamVersion: string; -}; +}>; export type SnapshotEvent< EventType extends string = string, EventData extends Record = Record, - EventMetadata extends SnapshotMetadata & - Record = SnapshotMetadata & Record, + EventMetadata extends SnapshotMetadata = SnapshotMetadata, > = Event & Readonly<{ - metadata: Readonly; + metadata: EventMetadata; }>; diff --git a/samples/snapshots/src/core/events/event.ts b/samples/snapshots/src/core/events/event.ts index de114d1d..0b70d5f0 100644 --- a/samples/snapshots/src/core/events/event.ts +++ b/samples/snapshots/src/core/events/event.ts @@ -12,6 +12,14 @@ export function isEvent< EventType extends string = string, EventData extends Record = Record, EventMetadata extends Record = Record, ->(event: any): event is Event { - return typeof event.type !== 'undefined' && typeof event.data !== 'undefined'; +>(event: unknown): event is Event { + return ( + typeof event === 'object' && + event !== null && + 'type' in event && + typeof event.type === 'string' && + 'data' in event && + typeof event.data === 'object' && + event.data !== null + ); } diff --git a/samples/snapshots/src/testing/assertions/streams.ts b/samples/snapshots/src/testing/assertions/streams.ts index d44d83fc..ea60b8b9 100644 --- a/samples/snapshots/src/testing/assertions/streams.ts +++ b/samples/snapshots/src/testing/assertions/streams.ts @@ -1,14 +1,24 @@ -import { ErrorType, EventStoreDBClient } from '@eventstore/db-client'; +import { EventStoreDBClient, StreamNotFoundError } from '@eventstore/db-client'; export async function expectStreamToNotExist( eventStore: EventStoreDBClient, streamName: string, ) { try { - await eventStore.readStream(streamName); + const events = eventStore.readStream(streamName, { maxCount: 1 }); + + for await (const _ of events) { + /* empty */ + } } catch (error) { - expect(error?.type).toBe(ErrorType.STREAM_NOT_FOUND); + if (error instanceof StreamNotFoundError) { + return; + } + + expect(typeof error).toBe('StreamNotFoundError'); } + + expect(undefined).toBeDefined(); } export async function expectStreamToHaveNumberOfEvents( @@ -17,8 +27,15 @@ export async function expectStreamToHaveNumberOfEvents( expectedNumberOfEvents: number, ) { try { - const events = await eventStore.readStream(streamName); - expect(events).toHaveLength(expectedNumberOfEvents); + const eventStream = eventStore.readStream(streamName); + + let eventsCount = 0; + + for await (const _ of eventStream) { + eventsCount++; + } + + expect(eventsCount).toBe(expectedNumberOfEvents); } catch (error) { expect(error).toBeUndefined(); } diff --git a/samples/snapshots/src/testing/builders/setupCashRegister.ts b/samples/snapshots/src/testing/builders/setupCashRegister.ts index 2c66da31..702dfec7 100644 --- a/samples/snapshots/src/testing/builders/setupCashRegister.ts +++ b/samples/snapshots/src/testing/builders/setupCashRegister.ts @@ -1,14 +1,13 @@ import request from 'supertest'; +import { App } from 'supertest/types'; -export async function setupCashRegister( - app: Express.Application, -): Promise { +export async function setupCashRegister(app: App): Promise { let cashRegisterId: string = ''; await request(app) .post('/cash-registers/') .send({ workstation: 'WS#1' }) .expect(201) - .expect((res) => { + .expect((res: { body: { id: string } }) => { cashRegisterId = res.body.id; }); diff --git a/samples/snapshots/src/testing/builders/setupStartedShift.ts b/samples/snapshots/src/testing/builders/setupStartedShift.ts index 3bb3522b..2362987d 100644 --- a/samples/snapshots/src/testing/builders/setupStartedShift.ts +++ b/samples/snapshots/src/testing/builders/setupStartedShift.ts @@ -1,15 +1,13 @@ import request from 'supertest'; +import { App } from 'supertest/types'; import { v4 as uuid } from 'uuid'; export async function setupStartedShift( - app: Express.Application, + app: App, existingCashRegisterId: string, -): Promise { - let cashRegisterId: string = ''; +): Promise { await request(app) .post(`/cash-registers/${existingCashRegisterId}/shifts`) .send({ cashierId: uuid() }) .expect(200); - - return cashRegisterId; } diff --git a/samples/snapshots/src/testing/eventStoreDB/eventStoreDBContainer.int.test.ts b/samples/snapshots/src/testing/eventStoreDB/eventStoreDBContainer.int.test.ts index 813a005e..34f36586 100644 --- a/samples/snapshots/src/testing/eventStoreDB/eventStoreDBContainer.int.test.ts +++ b/samples/snapshots/src/testing/eventStoreDB/eventStoreDBContainer.int.test.ts @@ -11,7 +11,7 @@ describe('EventStoreDBContainer', () => { let container: StartedEventStoreDBContainer; beforeAll(async () => { - container = await new EventStoreDBContainer().startContainer(); + container = await new EventStoreDBContainer().start(); }); it('should connect to EventStoreDB and append new event', async () => { diff --git a/samples/snapshots/src/testing/eventStoreDB/eventStoreDBContainer.ts b/samples/snapshots/src/testing/eventStoreDB/eventStoreDBContainer.ts index 83604085..ad9d3e5d 100644 --- a/samples/snapshots/src/testing/eventStoreDB/eventStoreDBContainer.ts +++ b/samples/snapshots/src/testing/eventStoreDB/eventStoreDBContainer.ts @@ -1,13 +1,16 @@ import { EventStoreDBClient } from '@eventstore/db-client'; import { + AbstractStartedContainer, GenericContainer, StartedTestContainer, - StoppedTestContainer, } from 'testcontainers'; +import { Environment } from 'testcontainers/build/types'; -const EVENTSTOREDB_TCP_PORTS = [1113, 2113]; +const EVENTSTOREDB_PORT = 2113; +const EVENTSTOREDB_TCP_PORT = 1113; +const EVENTSTOREDB_TCP_PORTS = [EVENTSTOREDB_TCP_PORT, EVENTSTOREDB_PORT]; const EVENTSTOREDB_IMAGE_NAME = 'eventstore/eventstore'; -const EVENTSTOREDB_IMAGE_TAG = '20.10.2-buster-slim'; +const EVENTSTOREDB_IMAGE_TAG = '23.10.0-bookworm-slim'; export class EventStoreDBContainer extends GenericContainer { private readonly tcpPorts = EVENTSTOREDB_TCP_PORTS; @@ -17,53 +20,52 @@ export class EventStoreDBContainer extends GenericContainer { runProjections = true, isInsecure = true, emptyDatabase = true, + withoutReuse = false, ) { super(image); - if (runProjections) { - this.withEnv('EVENTSTORE_RUN_PROJECTIONS', 'ALL'); - } + const environment: Environment = { + ...(runProjections + ? { + EVENTSTORE_RUN_PROJECTIONS: 'ALL', + } + : {}), + ...(isInsecure + ? { + EVENTSTORE_INSECURE: 'true', + } + : {}), + ...(!emptyDatabase + ? { + EVENTSTORE_MEM_DB: 'false', + EVENTSTORE_DB: '/data/integration-tests', + } + : {}), + EVENTSTORE_CLUSTER_SIZE: '1', + EVENTSTORE_START_STANDARD_PROJECTIONS: 'true', + EVENTSTORE_EXT_TCP_PORT: `${EVENTSTOREDB_TCP_PORT}`, + EVENTSTORE_HTTP_PORT: `${EVENTSTOREDB_PORT}`, + EVENTSTORE_ENABLE_EXTERNAL_TCP: 'true', + EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP: 'true', + }; - if (isInsecure) { - this.withEnv('EVENTSTORE_INSECURE', 'true'); - } + this.withEnvironment(environment).withExposedPorts(...this.tcpPorts); - if (!emptyDatabase) { - this.withEnv('EVENTSTORE_MEM_DB', 'false').withEnv( - 'EVENTSTORE_DB', - '/data/integration-tests', - ); - } - - this.withEnv('EVENTSTORE_CLUSTER_SIZE', '1') - .withEnv('EVENTSTORE_RUN_PROJECTIONS', 'All') - .withEnv('EVENTSTORE_START_STANDARD_PROJECTIONS', 'true') - .withEnv('EVENTSTORE_EXT_TCP_PORT', '1113') - .withEnv('EVENTSTORE_EXT_HTTP_PORT', '2113') - .withEnv('EVENTSTORE_ENABLE_EXTERNAL_TCP', 'true') - .withEnv('EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP', 'true') - .withExposedPorts(...this.tcpPorts); + if (!withoutReuse) this.withReuse(); } - async startContainer(): Promise { + async start(): Promise { return new StartedEventStoreDBContainer(await super.start()); } } -export class StartedEventStoreDBContainer { - constructor(private container: StartedTestContainer) {} - - stop( - options?: Partial<{ - timeout: number; - removeVolumes: boolean; - }>, - ): Promise { - return this.container.stop(options); +export class StartedEventStoreDBContainer extends AbstractStartedContainer { + constructor(container: StartedTestContainer) { + super(container); } getConnectionString(): string { - return `esdb://${this.container.getHost()}:${this.container.getMappedPort( + return `esdb://${this.getHost()}:${this.getMappedPort( 2113, )}?tls=false&throwOnAppendFailure=false`; } diff --git a/workshops/introduction_to_event_sourcing/src/core/testing/eventStoreDB/eventStoreDBContainer.ts b/workshops/introduction_to_event_sourcing/src/core/testing/eventStoreDB/eventStoreDBContainer.ts index dd347e1d..ad9d3e5d 100644 --- a/workshops/introduction_to_event_sourcing/src/core/testing/eventStoreDB/eventStoreDBContainer.ts +++ b/workshops/introduction_to_event_sourcing/src/core/testing/eventStoreDB/eventStoreDBContainer.ts @@ -10,7 +10,7 @@ const EVENTSTOREDB_PORT = 2113; const EVENTSTOREDB_TCP_PORT = 1113; const EVENTSTOREDB_TCP_PORTS = [EVENTSTOREDB_TCP_PORT, EVENTSTOREDB_PORT]; const EVENTSTOREDB_IMAGE_NAME = 'eventstore/eventstore'; -const EVENTSTOREDB_IMAGE_TAG = '22.10.1-buster-slim'; +const EVENTSTOREDB_IMAGE_TAG = '23.10.0-bookworm-slim'; export class EventStoreDBContainer extends GenericContainer { private readonly tcpPorts = EVENTSTOREDB_TCP_PORTS;