From dcc68cd33abaf953f9d5741d91f68ef078229889 Mon Sep 17 00:00:00 2001 From: rsaladocid Date: Thu, 8 Oct 2020 21:58:03 +0200 Subject: [PATCH 1/2] Use different Mongo config by context --- .../persistence/mongo/MongoConfigFactory.ts | 10 +++++++++ .../persistence/mongo/MongoClientFactory.ts | 10 ++++----- .../persistence/mongo/MongoConfig.ts | 3 +++ .../Shared/application.yaml | 21 ++++++++++++------- .../Shared/application.yaml | 7 ++++++- 5 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 src/Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory.ts create mode 100644 src/Contexts/Shared/infrastructure/persistence/mongo/MongoConfig.ts diff --git a/src/Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory.ts b/src/Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory.ts new file mode 100644 index 0000000..60d48ca --- /dev/null +++ b/src/Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory.ts @@ -0,0 +1,10 @@ +import config from '../../../../../../apps/mooc_backend/config/config'; +import MongoConfig from '../../../../../Shared/infrastructure/persistence/mongo/MongoConfig'; + +export class MongoConfigFactory { + static createConfig(): MongoConfig { + return { + url: config.get('mongo.url') + }; + } +} diff --git a/src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts b/src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts index 1de5766..b93d08b 100644 --- a/src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts +++ b/src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts @@ -1,15 +1,15 @@ import { MongoClient } from 'mongodb'; -import config from '../../../../../apps/mooc_backend/config/config'; import { Nullable } from '../../../domain/Nullable'; +import MongoConfig from './MongoConfig'; export class MongoClientFactory { private static clients: { [key: string]: MongoClient } = {}; - static async createClient(contextName: string): Promise { + static async createClient(contextName: string, config: MongoConfig): Promise { let client = MongoClientFactory.getClient(contextName); if (!client) { - client = await MongoClientFactory.createAndConnectClient(); + client = await MongoClientFactory.createAndConnectClient(config); MongoClientFactory.registerClient(client, contextName); } @@ -21,8 +21,8 @@ export class MongoClientFactory { return MongoClientFactory.clients[contextName]; } - private static async createAndConnectClient(): Promise { - const client = new MongoClient(config.get('mongo.url'), { useUnifiedTopology: true, ignoreUndefined: true }); + private static async createAndConnectClient(config: MongoConfig): Promise { + const client = new MongoClient(config.url, { useUnifiedTopology: true, ignoreUndefined: true }); await client.connect(); diff --git a/src/Contexts/Shared/infrastructure/persistence/mongo/MongoConfig.ts b/src/Contexts/Shared/infrastructure/persistence/mongo/MongoConfig.ts new file mode 100644 index 0000000..ce64e4d --- /dev/null +++ b/src/Contexts/Shared/infrastructure/persistence/mongo/MongoConfig.ts @@ -0,0 +1,3 @@ +type MongoConfig = { url: string }; + +export default MongoConfig; diff --git a/src/apps/backoffice/frontend/config/dependency-injection/Shared/application.yaml b/src/apps/backoffice/frontend/config/dependency-injection/Shared/application.yaml index 813e2c5..0df3702 100644 --- a/src/apps/backoffice/frontend/config/dependency-injection/Shared/application.yaml +++ b/src/apps/backoffice/frontend/config/dependency-injection/Shared/application.yaml @@ -1,30 +1,35 @@ services: Shared.Logger: - class: ../../../../../../Contexts/Shared/infrastructure/WinstonLogger + class: ../../../../../../Contexts/Shared/infrastructure/WinstonLogger arguments: [] + Shared.MongoConfig: + factory: + class: ../../../../../../Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory + method: 'createConfig' + Shared.ConnectionManager: factory: - class: ../../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory + class: ../../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory method: 'createClient' - arguments: ['mooc'] + arguments: ['mooc', '@Shared.MongoConfig'] Shared.EventBus: - class: ../../../../../../Contexts/Shared/infrastructure/EventBus/InMemoryAsyncEventBus + class: ../../../../../../Contexts/Shared/infrastructure/EventBus/InMemoryAsyncEventBus arguments: [] Shared.QueryBus: - class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus + class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus arguments: ['@Shared.QueryHandlersInformation'] Shared.CommandBus: - class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus + class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus arguments: ['@Shared.CommandHandlersInformation'] Shared.QueryHandlersInformation: - class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation + class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation arguments: ['!tagged queryHandler'] Shared.CommandHandlersInformation: - class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation + class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation arguments: ['!tagged commandHandler'] diff --git a/src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml b/src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml index b3f8a05..ba5254a 100644 --- a/src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml +++ b/src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml @@ -1,9 +1,14 @@ services: + Shared.MongoConfig: + factory: + class: ../../../../../Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory + method: 'createConfig' + Shared.ConnectionManager: factory: class: ../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory method: 'createClient' - arguments: ['mooc'] + arguments: ['mooc', '@Shared.MongoConfig'] Shared.Logger: class: ../../../../../Contexts/Shared/infrastructure/WinstonLogger From cff62cbcce50ac5be394c2deab38a2bbb93603ec Mon Sep 17 00:00:00 2001 From: rsaladocid Date: Fri, 9 Oct 2020 11:03:11 +0200 Subject: [PATCH 2/2] Fix tests --- .../Shared/infrastructure/MongoClientFactory.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Contexts/Shared/infrastructure/MongoClientFactory.test.ts b/tests/Contexts/Shared/infrastructure/MongoClientFactory.test.ts index bb1de8b..6643ecb 100644 --- a/tests/Contexts/Shared/infrastructure/MongoClientFactory.test.ts +++ b/tests/Contexts/Shared/infrastructure/MongoClientFactory.test.ts @@ -6,7 +6,7 @@ describe('Create a client', () => { let client: MongoClient; beforeEach(async () => { - client = await factory.createClient('test'); + client = await factory.createClient('test', { url: 'mongodb://localhost:27017/mooc-backend-test' }); }); afterEach(async () => { @@ -19,7 +19,7 @@ describe('Create a client', () => { }); it('creates a new client if it does not exist a client with the given name', async () => { - const newClient = await factory.createClient('test2'); + const newClient = await factory.createClient('test2', { url: 'mongodb://localhost:27017/mooc-backend-test' }); expect(newClient).not.toBe(client); @@ -27,7 +27,7 @@ describe('Create a client', () => { }); it('returns a client if it already exists', async () => { - const newClient = await factory.createClient('test'); + const newClient = await factory.createClient('test', { url: 'mongodb://localhost:27017/mooc-backend-test' }); expect(newClient).toBe(client);