Passed
Push — master ( 152bc9...7b1700 )
by Miguel Ángel
02:01
created

Model/Database.js   A

Complexity

Total Complexity 15
Complexity/F 1.25

Size

Lines of Code 44
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 15
eloc 23
c 2
b 0
f 0
nc 1
mnd 1
bc 11
fnc 12
dl 0
loc 44
rs 10
bpm 0.9166
cpm 1.25
noi 2

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Database.js ➔ constructor 0 3 1
A Database.js ➔ query 0 11 1
A Database.execute 0 8 1
A Database.js ➔ close 0 11 1
1
const mysql = require('mysql')
2
3
class Database {
4
    constructor(config) {
5
        this.connection = mysql.createConnection(config);
6
    }
7
8
    query(sql, args) {
9
        return new Promise((resolve, reject) => {
10
            this.connection.query(sql, args, (error, rows) => {
11
                if (error) {
12
                    return reject(error)
13
                }
14
15
                return resolve(rows)
16
            })
17
        })
18
    }
19
20
    close() {
21
        return new Promise((resolve, reject) => {
22
            this.connection.end( err => {
0 ignored issues
show
Unused Code introduced by
The parameter err is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
23
                if (error) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable error is declared in the current environment, consider using typeof error === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
24
                    return reject(error)
25
                }
26
27
                return resolve()
28
            })
29
        })
30
    }
31
}
32
33
Database.execute = function (config, callback) {
34
    const database = new Database(config)
35
36
    return callback(database).then(
37
        result => database.close().then(() => result),
38
        error => database.close().then(() => { throw error })
39
    )
40
}
41
42
module.exports = {
43
    Database
44
}
45