/
initMongo.ts
55 lines (50 loc) · 1.76 KB
/
initMongo.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
import { MongoClient, Db, Auth } from 'mongodb';
import { Logger } from '@lumieducation/h5p-server';
const log = new Logger('initMongo');
/**
* Creates a MongoDB client.
* You must either pass the configuration values through the parameters or set
* them as environment variables.
* @param url (optional) the connection URL for MongoDB (e.g. mongodb://localhost:27105)
* Can also be set through the environment variable MONGODB_URL.
* @param db (optional) the DB
* Can also be set through the environment variable MONGODB_DB.
* @param user (optional) a MongoDB user that can read and write the database
* Can also be set through the environment variable MONGODB_USER.
* @param password (optional) the password for the MongoDB user
* Can also be set through the environment variable MONGODB_PASSWORD.
* @returns the MongoDB client
*/
export default async (
url?: string,
db?: string,
username?: string,
password?: string
): Promise<Db> => {
try {
let auth: Auth;
if (process.env.MONGODB_USER) {
auth = {
password: process.env.MONGODB_PASSWORD,
username: process.env.MONGODB_USER
};
} else if (username) {
auth = {
username,
password
};
}
const client = await MongoClient.connect(
process.env.MONGODB_URL ?? url,
{
auth,
ignoreUndefined: true // this is important as otherwise MongoDB
// stores null for deliberately set undefined values!
}
);
return client.db(process.env.MONGODB_DB ?? db);
} catch (error) {
log.error(`Error while initializing MongoDB: ${error.message}`);
throw error;
}
};