-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
124 lines (102 loc) · 3.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Collection, Db as DbConnection, MongoClient, WithoutId } from 'mongodb';
import dotenv from 'dotenv';
import { MongoConnectionStatus } from '../types';
import { MongoDbCollectionName } from '../types/mongo-db-models/mongo-db-collection-name';
import { DbModel } from '../types/mongo-db-models/db-model';
dotenv.config();
const { MONGODB_URI, MONGO_INITDB_DATABASE } = process.env;
/**
* Class representing a MongoDB client.
*/
export class MongoDbClient {
/**
* MongoDB connection string.
*/
private uri: string = MONGODB_URI as string;
/**
* MongoDB database name.
*/
private name: string = MONGO_INITDB_DATABASE as string;
/**
* Client connection status.
*/
private clientConnection: MongoConnectionStatus = {
isInitialised: false,
};
/**
* Gets the initialised client. If the client is not yet initialised,
* initialises the client and then returns it.
*
* @returns {Promise<MongoConnectionStatus>} The initialised client.
*/
private async getInitialisedClient(): Promise<MongoConnectionStatus> {
if (this.clientConnection.isInitialised) {
return this.clientConnection;
}
const client = await MongoClient.connect(this.uri);
const connection = client.db(this.name);
this.clientConnection = {
isInitialised: true,
client,
connection,
};
return this.clientConnection;
}
/**
* Gets the initialised connection.
*
* @returns {Promise<DbConnection>} The initialised connection.
* @throws {Error} If the client connection is not initialised.
*/
public async getConnection(): Promise<DbConnection> {
const client = await this.getInitialisedClient();
if (!client.isInitialised) {
throw new Error('Client connection is not initialised');
}
const { connection } = client;
return connection;
}
/**
* Gets a specific collection.
*
* @param {CollectionName} collectionName - The collection name to get.
* @returns {Promise<Collection<WithoutId<DbModel<CollectionName>>>>} The collection.
* @throws {Error} If the client connection is not initialised.
*/
public async getCollection<CollectionName extends MongoDbCollectionName>(
collectionName: CollectionName,
): Promise<Collection<WithoutId<DbModel<CollectionName>>>> {
const client = await this.getInitialisedClient();
if (!client.isInitialised) {
throw new Error('Client connection is not initialised');
}
return client.connection.collection(collectionName);
}
/**
* Gets the initialised client.
*
* @returns {Promise<MongoClient>} The initialised client.
* @throws {Error} If the client connection is not initialised.
*/
public async getClient(): Promise<MongoClient> {
const client = await this.getInitialisedClient();
if (!client.isInitialised) {
throw new Error('Client connection is not initialised');
}
return client.client;
}
/**
* Closes the connection or, if the connection is not initialised, does nothing.
*
* @returns {Promise<void>}
*/
public async close(): Promise<void> {
if (!this.clientConnection.isInitialised) {
return;
}
await this.clientConnection.client.close();
this.clientConnection = {
isInitialised: false,
};
}
}