Passed
Push — main ( 55cb12...5d5954 )
by Martin
01:05 queued 16s
created

server/src/models/db.js   A

Complexity

Total Complexity 7
Complexity/F 3.5

Size

Lines of Code 50
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 29
mnd 5
bc 5
fnc 2
dl 0
loc 50
rs 10
bpm 2.5
cpm 3.5
noi 3
c 0
b 0
f 0
1
import mariadb from 'mariadb';
2
3
let host = process.env.DB_HOST;
4
let database = process.env.DB_DATABASE;
5
6
if (process.env.NODE_ENV === "test") {
7
    host = process.env.DB_TEST_HOST,
0 ignored issues
show
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
8
    database = process.env.TEST_DATABASE
9
}
10
11
const pool = mariadb.createPool({
12
    host: host,
13
    user: 'root',
14
    database: database,
15
    password: process.env.DB_PASSWORD,
16
    // connectionLimit: 10,
17
    // multipleStatements: true,
18
    // namedPlaceholders: true
19
});
20
21
export const db = {
22
    pool: pool,
23
24
    queryNoArgs: async function(sql) {
25
        let conn;
26
27
        try {
28
            conn = await pool.getConnection();
29
30
            let res = await conn.query(sql);
31
            return res;
32
            // no catching error here
33
        } finally {
34
            if (conn) conn.end();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
35
        }
36
    },
37
38
    queryWithArgs: async function(sql, args) {
39
        let conn;
40
41
        try {
42
            conn = await pool.getConnection();
43
            let res = await conn.query(sql, args);
44
            return res;
45
            // no catching error here
46
        } finally {
47
            if (conn) conn.end();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
48
        }
49
    },
50
}