Skip to content

Commit dcc68cd

Browse files
committed
Use different Mongo config by context
1 parent 972a618 commit dcc68cd

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import config from '../../../../../../apps/mooc_backend/config/config';
2+
import MongoConfig from '../../../../../Shared/infrastructure/persistence/mongo/MongoConfig';
3+
4+
export class MongoConfigFactory {
5+
static createConfig(): MongoConfig {
6+
return {
7+
url: config.get('mongo.url')
8+
};
9+
}
10+
}

src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { MongoClient } from 'mongodb';
2-
import config from '../../../../../apps/mooc_backend/config/config';
32
import { Nullable } from '../../../domain/Nullable';
3+
import MongoConfig from './MongoConfig';
44

55
export class MongoClientFactory {
66
private static clients: { [key: string]: MongoClient } = {};
77

8-
static async createClient(contextName: string): Promise<MongoClient> {
8+
static async createClient(contextName: string, config: MongoConfig): Promise<MongoClient> {
99
let client = MongoClientFactory.getClient(contextName);
1010

1111
if (!client) {
12-
client = await MongoClientFactory.createAndConnectClient();
12+
client = await MongoClientFactory.createAndConnectClient(config);
1313

1414
MongoClientFactory.registerClient(client, contextName);
1515
}
@@ -21,8 +21,8 @@ export class MongoClientFactory {
2121
return MongoClientFactory.clients[contextName];
2222
}
2323

24-
private static async createAndConnectClient(): Promise<MongoClient> {
25-
const client = new MongoClient(config.get('mongo.url'), { useUnifiedTopology: true, ignoreUndefined: true });
24+
private static async createAndConnectClient(config: MongoConfig): Promise<MongoClient> {
25+
const client = new MongoClient(config.url, { useUnifiedTopology: true, ignoreUndefined: true });
2626

2727
await client.connect();
2828

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type MongoConfig = { url: string };
2+
3+
export default MongoConfig;
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
services:
22
Shared.Logger:
3-
class: ../../../../../../Contexts/Shared/infrastructure/WinstonLogger
3+
class: ../../../../../../Contexts/Shared/infrastructure/WinstonLogger
44
arguments: []
55

6+
Shared.MongoConfig:
7+
factory:
8+
class: ../../../../../../Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory
9+
method: 'createConfig'
10+
611
Shared.ConnectionManager:
712
factory:
8-
class: ../../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory
13+
class: ../../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory
914
method: 'createClient'
10-
arguments: ['mooc']
15+
arguments: ['mooc', '@Shared.MongoConfig']
1116

1217
Shared.EventBus:
13-
class: ../../../../../../Contexts/Shared/infrastructure/EventBus/InMemoryAsyncEventBus
18+
class: ../../../../../../Contexts/Shared/infrastructure/EventBus/InMemoryAsyncEventBus
1419
arguments: []
1520

1621
Shared.QueryBus:
17-
class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus
22+
class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus
1823
arguments: ['@Shared.QueryHandlersInformation']
1924

2025
Shared.CommandBus:
21-
class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus
26+
class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus
2227
arguments: ['@Shared.CommandHandlersInformation']
2328

2429
Shared.QueryHandlersInformation:
25-
class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation
30+
class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation
2631
arguments: ['!tagged queryHandler']
2732

2833
Shared.CommandHandlersInformation:
29-
class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation
34+
class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation
3035
arguments: ['!tagged commandHandler']

src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
services:
2+
Shared.MongoConfig:
3+
factory:
4+
class: ../../../../../Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory
5+
method: 'createConfig'
6+
27
Shared.ConnectionManager:
38
factory:
49
class: ../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory
510
method: 'createClient'
6-
arguments: ['mooc']
11+
arguments: ['mooc', '@Shared.MongoConfig']
712

813
Shared.Logger:
914
class: ../../../../../Contexts/Shared/infrastructure/WinstonLogger

0 commit comments

Comments
 (0)