-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
connection.ts
37 lines (30 loc) · 937 Bytes
/
connection.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
import * as dotenv from 'dotenv';
import mongoose = require('mongoose');
import { debugInfo, debugError } from './debuggers';
import { getEnv } from './utils';
dotenv.config();
mongoose.Promise = global.Promise;
const MONGO_URL = getEnv({ name: 'MONGO_URL' });
export const connectionOptions: mongoose.ConnectionOptions = {
useNewUrlParser: true,
useCreateIndex: true,
// autoReconnect: true,
family: 4,
useFindAndModify: false
};
mongoose.connection
.on('connected', () => {
debugInfo(`Connected to the database: ${MONGO_URL}`);
})
.on('disconnected', () => {
debugInfo(`Disconnected from the database: ${MONGO_URL}`);
})
.on('error', error => {
debugError(`Database connection error: ${MONGO_URL} ${error}`);
});
export const connect = (URL?: string) => {
return mongoose.connect(URL || MONGO_URL, connectionOptions);
};
export function disconnect() {
return mongoose.connection.close();
}