| Total Complexity | 15 |
| Complexity/F | 1.25 |
| Lines of Code | 44 |
| Function Count | 12 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 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 |