Skip to content

Commit fd11ba1

Browse files
committed
✨ add mongodb connection
1 parent 9c2c9ef commit fd11ba1

File tree

5 files changed

+190
-1
lines changed

5 files changed

+190
-1
lines changed

.env.example

+6
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ DB_USER=user
44
DB_PASSWORD=password
55
DB_DATABASE=subway
66
DB_DATABASE_TEST=subway_test
7+
MONGO_URL=mongodb://localhost:27017/subway
8+
MONGO_URL_TEST=mongodb://localhost:27017/subway_test
9+
10+
# InMemory | MYSQL | MONGO
11+
12+
REPOSITORY_VENDOR=MYSQL

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"dependencies": {
4343
"dotenv": "^16.0.3",
4444
"fastify": "^4.17.0",
45+
"mongodb": "^5.5.0",
4546
"mysql2": "^3.3.0",
4647
"yup": "^1.1.1"
4748
}

pnpm-lock.yaml

+106-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Collection } from 'mongodb';
2+
3+
import { MongoHelper } from './mongo-helper';
4+
5+
describe('MongoHelper', () => {
6+
afterAll(async () => {
7+
await MongoHelper.disconnect();
8+
});
9+
10+
it('should be able to create a connection', async () => {
11+
await MongoHelper.connect();
12+
13+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
14+
// @ts-ignore private property
15+
expect(MongoHelper._client).toBeTruthy();
16+
});
17+
18+
it('should be able to disconnect from the database', async () => {
19+
await MongoHelper.disconnect();
20+
21+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
22+
// @ts-ignore private property
23+
expect(MongoHelper._client).toBeNull();
24+
});
25+
26+
it('should be able to get a collection', async () => {
27+
const collection = await MongoHelper.getCollection('subway_test');
28+
29+
expect(collection).toBeInstanceOf(Collection);
30+
31+
const result = await collection.find({}).toArray();
32+
33+
expect(result).toEqual([]);
34+
});
35+
36+
it('should be able to reconnect to get a collection if there is not connection', async () => {
37+
await MongoHelper.disconnect();
38+
39+
const collection = await MongoHelper.getCollection('subway_test');
40+
41+
expect(collection).toBeInstanceOf(Collection);
42+
43+
const result = await collection.find({}).toArray();
44+
45+
expect(result).toEqual([]);
46+
});
47+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Collection, MongoClient } from 'mongodb';
2+
import { config } from 'dotenv';
3+
4+
config();
5+
6+
const connectionURI =
7+
process.env.NODE_ENV === 'test'
8+
? process.env.MONGO_URI_TEST
9+
: process.env.MONGO_URI;
10+
11+
export class MongoHelper {
12+
private static _client: MongoClient | null;
13+
14+
public static async connect(): Promise<void> {
15+
this._client = await MongoClient.connect(connectionURI);
16+
}
17+
18+
public static async getCollection(name: string): Promise<Collection> {
19+
if (!this._client) {
20+
await this.connect();
21+
}
22+
23+
return this._client.db().collection(name);
24+
}
25+
26+
public static async disconnect(): Promise<void> {
27+
await this._client.close();
28+
this._client = null;
29+
}
30+
}

0 commit comments

Comments
 (0)