Skip to content

Commit 0f585e7

Browse files
committed
Create Mongo client factory
1 parent 6e0fc9b commit 0f585e7

File tree

5 files changed

+157
-2
lines changed

5 files changed

+157
-2
lines changed

package-lock.json

Lines changed: 84 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@types/express": "^4.17.2",
2626
"@types/glob": "^7.1.1",
2727
"@types/helmet": "0.0.44",
28+
"@types/mongodb": "^3.3.15",
2829
"@types/node": "~13.1.1",
2930
"@types/uuid": "^3.4.6",
3031
"@types/uuid-validate": "0.0.1",
@@ -39,6 +40,7 @@
3940
"helmet": "^3.21.2",
4041
"http-status": "^1.4.2",
4142
"mandrill-api": "^1.0.45",
43+
"mongodb": "^3.5.2",
4244
"node-dependency-injection": "^2.4.2",
4345
"ts-node": "^8.3.0",
4446
"typescript": "^3.7.2",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { MongoClient } from 'mongodb';
2+
import config from '../../../../../apps/mooc_backend/config/config';
3+
import { Nullable } from '../../../domain/Nullable';
4+
5+
export class MongoClientFactory {
6+
private static clients: { [key: string]: MongoClient } = {};
7+
8+
static async createClient(contextName: string): Promise<MongoClient> {
9+
let client = MongoClientFactory.getClient(contextName);
10+
11+
if (!client) {
12+
client = await MongoClientFactory.createAndConnectClient();
13+
14+
MongoClientFactory.registerClient(client, contextName);
15+
}
16+
17+
return client;
18+
}
19+
20+
private static getClient(contextName: string): Nullable<MongoClient> {
21+
return MongoClientFactory.clients[contextName];
22+
}
23+
24+
private static async createAndConnectClient(): Promise<MongoClient> {
25+
const client = new MongoClient(config.get('mongo.url'));
26+
27+
await client.connect();
28+
29+
return client;
30+
}
31+
32+
private static registerClient(client: MongoClient, contextName: string): void {
33+
MongoClientFactory.clients[contextName] = client;
34+
}
35+
}

src/apps/mooc_backend/config/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ const convictConfig = convict({
66
format: ['production', 'development', 'staging', 'test'],
77
default: 'default',
88
env: 'NODE_ENV'
9+
},
10+
mongo: {
11+
url: {
12+
doc: 'The Mongo connection URL',
13+
format: String,
14+
env: 'MONGO_URL'
15+
},
16+
name: {
17+
doc: 'The Mongo database name',
18+
format: String,
19+
env: 'MONGO_DB_NAME'
20+
}
921
}
1022
});
1123

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { MongoClientFactory } from '../../../../src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory';
2+
import { MongoClient } from 'mongodb';
3+
4+
describe('MongoClientFactory', () => {
5+
const factory = MongoClientFactory;
6+
let client: MongoClient;
7+
8+
beforeEach(async () => {
9+
client = await factory.createClient('test');
10+
});
11+
12+
afterEach(async () => {
13+
await client.close();
14+
});
15+
16+
it('creates a new client if it does not exist', () => {
17+
expect(client).toBeInstanceOf(MongoClient);
18+
});
19+
20+
it('creates a new client with existing name should return existing client', async () => {
21+
const newClient = await factory.createClient('test');
22+
expect(newClient).toBe(client);
23+
});
24+
});

0 commit comments

Comments
 (0)