From 7e5474dcf2d3bce3d57312a0a4de4954eb5b2f8f Mon Sep 17 00:00:00 2001 From: Dries Hooghe Date: Thu, 24 Nov 2022 18:23:37 +0100 Subject: [PATCH] :bug: fix conditional imports of peer dependencies --- example/src/app.module.ts | 15 ++++++------ lib/event-sourcing.providers.ts | 24 ++++++++++++++----- lib/integration/event-store/index.ts | 2 -- lib/integration/snapshot-store/index.ts | 2 -- .../event-sourcing-options.interface.ts | 4 ++-- .../event-store/dynamodb.event-store.spec.ts | 2 +- .../event-store/mongodb.event-store.spec.ts | 2 +- .../dynamodb.snapshot-store.spec.ts | 2 +- .../mongodb.snapshot-store.spec.ts | 5 +++- 9 files changed, 35 insertions(+), 23 deletions(-) diff --git a/example/src/app.module.ts b/example/src/app.module.ts index de5638ef..86402f67 100644 --- a/example/src/app.module.ts +++ b/example/src/app.module.ts @@ -16,13 +16,14 @@ import { AccountController } from './application/account.controller'; EventSourcingModule.forRootAsync({ useFactory: () => ({ events: [...Events], - eventStore: { client: 'dynamodb', - options: { - region: 'us-east-1', - endpoint: 'http://127.0.0.1:8000', - credentials: { accessKeyId: 'foo', secretAccessKey: 'bar' }, - } - }, + eventStore: { + client: 'dynamodb', + options: { + region: 'us-east-1', + endpoint: 'http://127.0.0.1:8000', + credentials: { accessKeyId: 'foo', secretAccessKey: 'bar' }, + } + }, snapshotStore: { client: 'dynamodb', options: { diff --git a/lib/event-sourcing.providers.ts b/lib/event-sourcing.providers.ts index acc50944..75437578 100644 --- a/lib/event-sourcing.providers.ts +++ b/lib/event-sourcing.providers.ts @@ -3,8 +3,8 @@ import { EVENT_SOURCING_OPTIONS } from './constants'; import { EventMap } from './event-map'; import { EventStore } from './event-store'; import { MissingStoreConnectionOptionsException } from './exceptions'; -import { DynamoDBEventStore, InMemoryEventStore, MongoDBEventStore } from './integration/event-store'; -import { DynamoDBSnapshotStore, InMemorySnapshotStore, MongoDBSnapshotStore } from './integration/snapshot-store'; +import { InMemoryEventStore } from './integration/event-store'; +import { InMemorySnapshotStore } from './integration/snapshot-store'; import { EventSourcingModuleOptions } from './interfaces'; import { SnapshotStore } from './snapshot-store'; @@ -21,7 +21,10 @@ export const EventStoreProvider = { throw new MissingStoreConnectionOptionsException('eventStore', 'mongodb'); } const { url, ...clientOptions } = options.eventStore.options; - const { MongoClient } = await import('mongodb'); + const [{ MongoClient }, { MongoDBEventStore }] = await Promise.all([ + import('mongodb'), + import('./integration/event-store/mongodb.event-store'), + ]); const mongoClient = await new MongoClient(url, clientOptions).connect(); return new MongoDBEventStore(eventMap, mongoClient.db()); } @@ -29,7 +32,10 @@ export const EventStoreProvider = { if (!options.eventStore.options) { throw new MissingStoreConnectionOptionsException('eventStore', ' dynamodb'); } - const { DynamoDBClient } = await import('@aws-sdk/client-dynamodb'); + const [{ DynamoDBClient }, { DynamoDBEventStore }] = await Promise.all([ + import('@aws-sdk/client-dynamodb'), + import('./integration/event-store/dynamodb.event-store'), + ]); const dynamoClient = new DynamoDBClient(options.eventStore.options); return new DynamoDBEventStore(eventMap, dynamoClient); } @@ -50,7 +56,10 @@ export const SnapshotStoreProvider = { throw new MissingStoreConnectionOptionsException('snapshotStore', 'mongodb'); } const { url, ...clientOptions } = options.snapshotStore.options; - const { MongoClient } = await import('mongodb'); + const [{ MongoClient }, { MongoDBSnapshotStore }] = await Promise.all([ + import('mongodb'), + import('./integration/snapshot-store/mongodb.snapshot-store'), + ]); const mongoClient = await new MongoClient(url, clientOptions).connect(); return new MongoDBSnapshotStore(mongoClient.db()); } @@ -58,7 +67,10 @@ export const SnapshotStoreProvider = { if (!options.snapshotStore.options) { throw new MissingStoreConnectionOptionsException('snapshotStore', 'dynamodb'); } - const { DynamoDBClient } = await import('@aws-sdk/client-dynamodb'); + const [{ DynamoDBClient }, { DynamoDBSnapshotStore }] = await Promise.all([ + import('@aws-sdk/client-dynamodb'), + import('./integration/snapshot-store/dynamodb.snapshot-store'), + ]); const dynamoClient = new DynamoDBClient(options.snapshotStore.options); return new DynamoDBSnapshotStore(dynamoClient); } diff --git a/lib/integration/event-store/index.ts b/lib/integration/event-store/index.ts index 07821b69..68cae7ec 100644 --- a/lib/integration/event-store/index.ts +++ b/lib/integration/event-store/index.ts @@ -1,3 +1 @@ -export * from './dynamodb.event-store'; export * from './in-memory.event-store'; -export * from './mongodb.event-store'; diff --git a/lib/integration/snapshot-store/index.ts b/lib/integration/snapshot-store/index.ts index dd554734..f39ecc12 100644 --- a/lib/integration/snapshot-store/index.ts +++ b/lib/integration/snapshot-store/index.ts @@ -1,3 +1 @@ -export * from './dynamodb.snapshot-store'; export * from './in-memory.snapshot-store'; -export * from './mongodb.snapshot-store'; diff --git a/lib/interfaces/event-sourcing-options.interface.ts b/lib/interfaces/event-sourcing-options.interface.ts index ac95ba0d..ed1bb310 100644 --- a/lib/interfaces/event-sourcing-options.interface.ts +++ b/lib/interfaces/event-sourcing-options.interface.ts @@ -1,6 +1,6 @@ -import { DynamoDBClientConfig } from '@aws-sdk/client-dynamodb'; +import type { DynamoDBClientConfig } from '@aws-sdk/client-dynamodb'; import { ModuleMetadata, Type } from '@nestjs/common'; -import { MongoClientOptions } from 'mongodb'; +import type { MongoClientOptions } from 'mongodb'; import { IEvent } from './events'; type InMemoryStoreConfig = { client: 'in-memory' }; diff --git a/tests/unit/integration/event-store/dynamodb.event-store.spec.ts b/tests/unit/integration/event-store/dynamodb.event-store.spec.ts index ddadf89f..63c19ee0 100644 --- a/tests/unit/integration/event-store/dynamodb.event-store.spec.ts +++ b/tests/unit/integration/event-store/dynamodb.event-store.spec.ts @@ -14,7 +14,7 @@ import { StreamReadingDirection, } from '../../../../lib'; import { DefaultEventSerializer } from '../../../../lib/helpers'; -import { DynamoDBEventStore } from '../../../../lib/integration/event-store'; +import { DynamoDBEventStore } from '../../../../lib/integration/event-store/dynamodb.event-store'; class AccountId extends Id {} diff --git a/tests/unit/integration/event-store/mongodb.event-store.spec.ts b/tests/unit/integration/event-store/mongodb.event-store.spec.ts index d0b2611a..f4ab4411 100644 --- a/tests/unit/integration/event-store/mongodb.event-store.spec.ts +++ b/tests/unit/integration/event-store/mongodb.event-store.spec.ts @@ -13,7 +13,7 @@ import { StreamReadingDirection, } from '../../../../lib'; import { DefaultEventSerializer } from '../../../../lib/helpers'; -import { MongoDBEventStore, MongoEventEntity } from '../../../../lib/integration/event-store'; +import { MongoDBEventStore, MongoEventEntity } from '../../../../lib/integration/event-store/mongodb.event-store'; class AccountId extends Id {} diff --git a/tests/unit/integration/snapshot-store/dynamodb.snapshot-store.spec.ts b/tests/unit/integration/snapshot-store/dynamodb.snapshot-store.spec.ts index 9c4c0453..5f4bc727 100644 --- a/tests/unit/integration/snapshot-store/dynamodb.snapshot-store.spec.ts +++ b/tests/unit/integration/snapshot-store/dynamodb.snapshot-store.spec.ts @@ -12,7 +12,7 @@ import { SnapshotStream, StreamReadingDirection, } from '../../../../lib'; -import { DynamoDBSnapshotStore } from '../../../../lib/integration/snapshot-store'; +import { DynamoDBSnapshotStore } from '../../../../lib/integration/snapshot-store/dynamodb.snapshot-store'; class AccountId extends Id {} class CustomerId extends Id {} diff --git a/tests/unit/integration/snapshot-store/mongodb.snapshot-store.spec.ts b/tests/unit/integration/snapshot-store/mongodb.snapshot-store.spec.ts index d825128a..14729475 100644 --- a/tests/unit/integration/snapshot-store/mongodb.snapshot-store.spec.ts +++ b/tests/unit/integration/snapshot-store/mongodb.snapshot-store.spec.ts @@ -10,7 +10,10 @@ import { SnapshotStream, StreamReadingDirection, } from '../../../../lib'; -import { MongoDBSnapshotStore, MongoSnapshotEntity } from '../../../../lib/integration/snapshot-store'; +import { + MongoDBSnapshotStore, + MongoSnapshotEntity, +} from '../../../../lib/integration/snapshot-store/mongodb.snapshot-store'; class AccountId extends Id {} class CustomerId extends Id {}