-
Notifications
You must be signed in to change notification settings - Fork 16
/
db.js
31 lines (25 loc) · 880 Bytes
/
db.js
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
const mongoose = require('mongoose');
const { readdirSync } = require('fs');
const config = require('./config');
const dbLogger = config.logger('db');
// Make sure all models are registered
const modelsDir = config.path('models');
const modelFiles = readdirSync(modelsDir);
for (const modelFile of modelFiles) {
require(config.path('models', modelFile));
}
// Log database queries in debug mode
if (dbLogger.isLevelEnabled('debug')) {
mongoose.set('debug', (collection, method, ...args) => dbLogger.debug(`${collection}.${method}(${args.map(arg => JSON.stringify(arg)).join(', ')})`));
}
// Remove deprecation warnings
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useNewUrlParser', true);
/**
* Connects to the database.
*/
async function connect() {
await mongoose.connect(config.db);
}
module.exports = { connect };