-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
38 lines (35 loc) · 1.06 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
import { Db } from 'mongodb';
import { LoggerInstance } from 'winston';
import Config from './Config';
import Connection from './Connection';
import getConnection from './getConnection';
export interface Opts {
readonly maxRetries?: number;
readonly retryGapMS?: number;
readonly logger: LoggerInstance;
readonly url: string;
readonly dbName: string;
}
const defaultMaxRetries = 3;
const defaultRetryGapMS = 1000;
export default ({
maxRetries = defaultMaxRetries,
retryGapMS = defaultRetryGapMS,
logger,
url,
dbName,
}: Opts) => {
let connection: Promise<Connection>; // tslint:disable-line:no-let
const setConnection = (newConnection: Promise<Connection>) => {
connection = newConnection;
};
const config: Config = { maxRetries, retryGapMS, logger, url, dbName, setConnection };
const connectToDb = async (): Promise<Db> => {
setConnection(getConnection(config, connection));
return (await connection).db;
};
connectToDb().catch((err) => {
logger.error(`Failed initial mongo connection: ${err.message}`);
});
return connectToDb;
};