Model/Database.js   A
last analyzed

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 0

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( error => {
23
                if (error) {
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