summary refs log tree commit diff
path: root/src/db/db.js
blob: b9a425cf81b751c7245f546fce0313ecdcabaf69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { 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']
    );
    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;
    }
}