Skip to content

Commit

Permalink
Added guide on gracefuly stopping PostgreSQL connection
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Jan 26, 2025
1 parent e8b4d42 commit f904a85
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ $ yarn add @testcontainers/postgresql
$ bun add @testcontainers/postgresql
```

:::

::: info EventStoreDB testing

Emmett provides the package with additional test containers like the one for [EventStoreDB](https://developers.eventstore.com/). If you're using EventStoreDB, install [emmett-testcontainers](https://www.npmjs.com/package/@event-driven-io/emmett-testcontainers) and get the test container for it.
Expand All @@ -468,6 +466,14 @@ Having that, we can set our test container with:

<<< @/snippets/gettingStarted/webApi/apiBDDE2EGiven.ts#test-container

::: info Event store lifetime

The PostgreSQL event store creates an internal connection pool to use PostgreSQL efficiently. **Because of that, we recommend using a single event store instance per application.** The same advice applies to other storages.

After the application ends or tests are finished, we need to close it to gracefully release open connections in the connection pool.

:::

And create our test specification using the `ApiE2ESpecification` type:

<<< @/snippets/gettingStarted/webApi/apiBDDE2EGiven.ts#given
Expand Down
14 changes: 11 additions & 3 deletions src/docs/snippets/gettingStarted/webApi/apiBDD.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
getApplication,
type TestRequest,
} from '@event-driven-io/emmett-expressjs';
import { getPostgreSQLEventStore } from '@event-driven-io/emmett-postgresql';
import {
getPostgreSQLEventStore,
type PostgresEventStore,
} from '@event-driven-io/emmett-postgresql';
import {
PostgreSqlContainer,
type StartedPostgreSqlContainer,
Expand All @@ -22,13 +25,17 @@ void describe('ShoppingCart E2E', () => {
let clientId: string;
let shoppingCartId: string;
let postgreSQLContainer: StartedPostgreSqlContainer;
let eventStore: PostgresEventStore;
let given: ApiE2ESpecification;

before(async () => {
postgreSQLContainer = await new PostgreSqlContainer().start();
eventStore = getPostgreSQLEventStore(
postgreSQLContainer.getConnectionUri(),
);

given = ApiE2ESpecification.for(
() => getPostgreSQLEventStore(postgreSQLContainer.getConnectionUri()),
() => eventStore,
(eventStore) =>
getApplication({
apis: [
Expand All @@ -47,7 +54,8 @@ void describe('ShoppingCart E2E', () => {
shoppingCartId = `shopping_cart:${clientId}:current`;
});

after(() => {
after(async () => {
await eventStore.close();
return postgreSQLContainer.stop();
});

Expand Down
22 changes: 16 additions & 6 deletions src/docs/snippets/gettingStarted/webApi/apiBDDE2EGiven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,45 @@ const unitPrice = 100;
const now = new Date();

// #region test-container
import {
getPostgreSQLEventStore,
type PostgresEventStore,
} from '@event-driven-io/emmett-postgresql';
import {
PostgreSqlContainer,
StartedPostgreSqlContainer,
} from '@testcontainers/postgresql';

let postgreSQLContainer: StartedPostgreSqlContainer;
void describe('ShoppingCart E2E', () => {
// Set up a container before all tests
let postgreSQLContainer: StartedPostgreSqlContainer;
let eventStore: PostgresEventStore;

// Set up a container and event store before all tests
before(async () => {
postgreSQLContainer = await new PostgreSqlContainer().start();
eventStore = getPostgreSQLEventStore(
postgreSQLContainer.getConnectionUri(),
);
});

// Stop container once we finished testing
after(() => {
// Close PostgreSQL connection and stop container once we finished testing
after(async () => {
await eventStore.close();
return postgreSQLContainer.stop();
});
// (...) Tests will go here
});
// #endregion test-container

const eventStore: PostgresEventStore = undefined!;
// #region given
import {
ApiE2ESpecification,
getApplication,
} from '@event-driven-io/emmett-expressjs';
import { getPostgreSQLEventStore } from '@event-driven-io/emmett-postgresql';

const given = ApiE2ESpecification.for(
() => getPostgreSQLEventStore(postgreSQLContainer.getConnectionUri()),
() => eventStore,
(eventStore) =>
getApplication({
apis: [
Expand Down

0 comments on commit f904a85

Please sign in to comment.