Skip to content

Commit

Permalink
Merge pull request #60 from ocoda/fix-conditional-imports-of-peer-dep…
Browse files Browse the repository at this point in the history
…endencies

🐛 fix conditional imports of peer dependencies
  • Loading branch information
drieshooghe committed Nov 24, 2022
2 parents 13ba8e2 + 7e5474d commit e676910
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 23 deletions.
15 changes: 8 additions & 7 deletions example/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
24 changes: 18 additions & 6 deletions lib/event-sourcing.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -21,15 +21,21 @@ 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());
}
case 'dynamodb': {
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);
}
Expand All @@ -50,15 +56,21 @@ 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());
}
case 'dynamodb': {
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);
}
Expand Down
2 changes: 0 additions & 2 deletions lib/integration/event-store/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './dynamodb.event-store';
export * from './in-memory.event-store';
export * from './mongodb.event-store';
2 changes: 0 additions & 2 deletions lib/integration/snapshot-store/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './dynamodb.snapshot-store';
export * from './in-memory.snapshot-store';
export * from './mongodb.snapshot-store';
4 changes: 2 additions & 2 deletions lib/interfaces/event-sourcing-options.interface.ts
Original file line number Diff line number Diff line change
@@ -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' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down

0 comments on commit e676910

Please sign in to comment.