server/src/models/db.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 3.5

Size

Lines of Code 75
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 32
mnd 5
bc 5
fnc 2
dl 0
loc 75
ccs 19
cts 19
cp 1
rs 10
bpm 2.5
cpm 3.5
noi 0
c 0
b 0
f 0
1
import mariadb from 'mariadb';
2
3 1
let host = process.env.DB_HOST;
4 1
let database = process.env.DB_DATABASE;
5
6
/**
7
 * Changes to test database
8
 */
9 2
if (process.env.NODE_ENV === "test") {
10 1
    host = process.env.DB_TEST_HOST;
11 1
    database = process.env.TEST_DATABASE;
12
}
13
14 1
const pool = mariadb.createPool({
15
    host: host,
16
    user: process.env.DB_USER,
17
    database: database,
18
    password: process.env.DB_PASSWORD,
19
    // connectionLimit: 10,
20
     multipleStatements: true,
21
    // namedPlaceholders: true
22
});
23
24
25 1
export const db = {
26
    /**
27
     * For handling connections
28
     * to the database
29
     */
30
    pool: pool,
31
32
    /**
33
     * For database queries without
34
     * arguments
35
     * @param {String} sql 
36
     * @returns {Promise<Array>}
37
     */
38
    queryNoArgs: async function(sql) {
39
        let conn;
40
41 22
        try {
42 22
            conn = await pool.getConnection();
43
44 22
            let res = await conn.query(sql);
45 22
            return res;
46
            // no catching error here
47
        } finally {
48 22
            if (conn) {
49 22
                conn.end();
50
            }
51
        }
52
    },
53
54
    /**
55
     * For database queries
56
     * with arguments
57
     * @param {String} sql 
58
     * @param {Array} args 
59
     * @returns {Promise<Array>}
60
     */
61
    queryWithArgs: async function(sql, args) {
62
        let conn;
63
64 194
        try {
65 194
            conn = await pool.getConnection();
66 194
            let res = await conn.query(sql, args);
67 178
            return res;
68
            // no catching error here
69
        } finally {
70 194
            if (conn) {
71 194
                conn.end();
72
            }
73
        }
74
    },
75
}