-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqldb.js
75 lines (52 loc) · 1.68 KB
/
mysqldb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const mysql = require("mysql")
const bluebird = require("bluebird")
const util = require("util")
exports = module.exports = CreateDBConnection;
function CreateDBConnection({dbName, table}) {
var pool = mysql.createPool({
// connectionLimit : 10,
host : '172.17.0.3',
user : 'root',
password : '123456',
database : `${dbName}`
})
return new Table(pool, table)
}
function Table(pool, tbname) {
this.pool = pool;
this.table = tbname;
}
Table.prototype.test = async function test() {
const table = this.table;
// const connection = this.pool.getConnection();
// db = Bluebird.promisifyAll(this.pool.query);
const queryAsync = util.promisify(this.pool.query).bind(this.pool);
query = `SELECT * FROM \`${table}\``;
return await queryAsync(query);
}
Table.prototype.setReceipts = async function setReceipts(datas) {
let table = this.table
const db = bluebird.promisifyAll(this.pool);
let query = `INSERT INTO \`${table}\` VALUES ?`
let data = [];
for (let i of datas) {
data.push(Object.values(i));
}
try {
return await db.queryAsync(query, [data]);
} catch (error) {
throw error;
}
}
Table.prototype.updateReceipt = async function updateReceipt({ receiptid, total=0 }) {
let table = this.table
const db = bluebird.promisifyAll(this.pool);
if (!receiptid)
throw new TypeError('missing receiptid')
let query = `UPDATE \`${table}\` SET total = ? WHERE receiptid = ?`
try {
return await db.queryAsync(query, [total, receiptid]);
} catch (error) {
throw error;
}
}