@icgio/icg-utils 1.9.16 → 1.9.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/mysql.js +84 -0
- package/package.json +1 -1
package/lib/mysql.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Sample usage:
|
|
2
|
+
//
|
|
3
|
+
// const Mysql = require('./mysql')
|
|
4
|
+
// const db = new Mysql({
|
|
5
|
+
// host: 'localhost',
|
|
6
|
+
// user: 'root',
|
|
7
|
+
// password: 'password',
|
|
8
|
+
// database: 'testdb',
|
|
9
|
+
// })
|
|
10
|
+
//
|
|
11
|
+
// // Query example:
|
|
12
|
+
// db.query('SELECT * FROM users WHERE id = ?', [1], (res) => {
|
|
13
|
+
// if (res.success) {
|
|
14
|
+
// console.log('User:', res.body)
|
|
15
|
+
// } else {
|
|
16
|
+
// console.error('Query error:', res.body)
|
|
17
|
+
// }
|
|
18
|
+
// })
|
|
19
|
+
//
|
|
20
|
+
// // Insert example:
|
|
21
|
+
// const rows = [
|
|
22
|
+
// { name: 'Alice', age: 30 },
|
|
23
|
+
// { name: 'Bob', age: 25 },
|
|
24
|
+
// ]
|
|
25
|
+
// db.write_rows_with_check('users', rows, (res) => {
|
|
26
|
+
// if (res.success) {
|
|
27
|
+
// console.log('Insert result:', res.body)
|
|
28
|
+
// } else {
|
|
29
|
+
// console.error('Insert error:', res.body)
|
|
30
|
+
// }
|
|
31
|
+
// })
|
|
32
|
+
//
|
|
33
|
+
|
|
34
|
+
const _ = require('lodash')
|
|
35
|
+
|
|
36
|
+
const mysql = require('mysql2/promise')
|
|
37
|
+
|
|
38
|
+
function fields_check(fields) {
|
|
39
|
+
let result = true
|
|
40
|
+
for (let key in fields) {
|
|
41
|
+
if (_.isNaN(fields[key]) || _.isNull(fields[key]) || _.isUndefined(fields[key])) {
|
|
42
|
+
result = false
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class Mysql {
|
|
49
|
+
constructor(config) {
|
|
50
|
+
// config: { host, user, password, database, ... }
|
|
51
|
+
this.config = config
|
|
52
|
+
this.pool = mysql.createPool(config)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async query(query_string, params, cb) {
|
|
56
|
+
try {
|
|
57
|
+
const [rows, fields] = await this.pool.execute(query_string, params)
|
|
58
|
+
cb({ success: true, body: rows })
|
|
59
|
+
} catch (err) {
|
|
60
|
+
cb({ success: false, body: err })
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async write_rows_with_check(table, rows, cb) {
|
|
65
|
+
// rows: array of objects, each object is a row
|
|
66
|
+
try {
|
|
67
|
+
const validRows = rows.filter(fields_check)
|
|
68
|
+
if (validRows.length === 0) {
|
|
69
|
+
return cb({ success: false, body: 'No valid rows to insert' })
|
|
70
|
+
}
|
|
71
|
+
const keys = Object.keys(validRows[0])
|
|
72
|
+
const placeholders = keys.map(() => '?').join(',')
|
|
73
|
+
const sql = `INSERT INTO \`${table}\` (${keys.map((k) => `\`${k}\``).join(',')}) VALUES (${placeholders})`
|
|
74
|
+
const values = validRows.map((row) => keys.map((k) => row[k]))
|
|
75
|
+
// Use bulk insert
|
|
76
|
+
const [result] = await this.pool.query(sql, values)
|
|
77
|
+
cb({ success: true, body: result })
|
|
78
|
+
} catch (err) {
|
|
79
|
+
cb({ success: false, body: err })
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = Mysql
|