-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
db.ts
70 lines (57 loc) · 2.13 KB
/
db.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
import { Request } from 'express';
import * as mongodb from 'mongodb';
import * as mongoose from 'mongoose';
import Config from './config';
import { InvalidSyncIdException } from './exception';
import { LogLevel } from './server';
// Handles database interaction
export default class DB {
public static idIsValid(id): void {
let binary: mongodb.Binary;
let base64Str: string;
if (!id) {
throw new InvalidSyncIdException();
}
try {
binary = new mongodb.Binary(Buffer.from(id, 'hex'), 4);
base64Str = binary.buffer.toString('base64');
}
catch (err) {
throw new InvalidSyncIdException();
}
if (!binary || !base64Str) {
throw new InvalidSyncIdException();
}
}
constructor(private log: (level: LogLevel, message: string, req?: Request, err?: Error) => void) { }
// Closes the database connection
public async closeConnection(): Promise<void> {
await mongoose.disconnect()
}
// Initialises the database connection using config settings
public async openConnection(): Promise<void> {
// Set the db connection options from config settings
const options: mongoose.ConnectionOptions = {
connectTimeoutMS: Config.get().db.connTimeout,
keepAlive: 1,
useNewUrlParser: true
};
// Get db username and password
const username = Config.get().db.username || process.env.XBROWSERSYNC_DB_USER;
const password = Config.get().db.password || process.env.XBROWSERSYNC_DB_PWD;
// Connect to the host and db name defined in config settings
const dbServerUrl = `mongodb://${username}:${password}@${Config.get().db.host}:${Config.get().db.port}/${Config.get().db.name}?authSource=${Config.get().db.authSource}`;
mongoose.connect(dbServerUrl, options);
const dbConn = mongoose.connection;
await new Promise((resolve, reject) => {
dbConn.on('close', () => {
dbConn.removeAllListeners();
});
dbConn.on('error', (err: mongoose.Error) => {
this.log(LogLevel.Error, 'Database error', null, err);
reject(new Error('Unable to connect to database.'));
});
dbConn.once('open', resolve);
});
}
}