@icgio/icg-utils 1.9.17 → 1.9.19

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/.prettierignore CHANGED
@@ -1,7 +1,4 @@
1
1
  .DS_Store
2
2
  node_modules/
3
- lib/test*
4
3
  lib/bash.sh
5
- *.key
6
- test/
7
- test*
4
+ *.key
package/index.js CHANGED
@@ -4,6 +4,7 @@ module.exports = {
4
4
  forex_rate: require('./lib/forex-rate.js'),
5
5
  html_parser: require('./lib/html-parser.js'),
6
6
  influx: require('./lib/influxdb.js'),
7
+ mysql: require('./lib/mysql.js'),
7
8
  ip: require('./lib/ip.js'),
8
9
  logger: require('./lib/logger.js'),
9
10
  mail: require('./lib/mail.js'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.17",
3
+ "version": "1.9.19",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -31,6 +31,6 @@
31
31
  "qrcode-terminal": "^0.12.0",
32
32
  "websocket": "^1.0.35",
33
33
  "winston": "^2.4.5",
34
- "ws": "^8.18.0"
34
+ "ws": "^8.18.3"
35
35
  }
36
36
  }
package/lib/mysql.js DELETED
@@ -1,84 +0,0 @@
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