summary refs log tree commit diff
path: root/src/db/db.js
blob: 9cbba8006475fda94929bbd69ce72154ad2d22ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import mongoose, { connect } from 'mongoose';
import { readSecret } from '#util/secretUtils.js';

export async function initDb() {
    const connectionString = await readSecret(
        'MongoDB connection string',
        process.env['DATABASE_SECRET_PATH']
    );

    if (process.env['LOG_QUERIES'] === 'true') mongoose.set('debug', true);

    try {
        const res = await connect(connectionString);
        if (res.connection.readyState === 1) {
            console.log('[MONGODB] Connected successfully!');
        } else {
            console.error('[MONGODB] Failed to connect to ', connectionString);
        }
    } catch (e) {
        console.error('[MONGODB] Error connecting to database!');
        throw e;
    }
}