masterrecord 0.3.15 → 0.3.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.
@@ -1,82 +0,0 @@
1
- // version : 0.0.4
2
-
3
- var MySql = require('sync-mysql2');
4
-
5
- class MySQLClient {
6
- constructor(config) {
7
- this.config = config ? { ...config } : {};
8
- if (this.config && Object.prototype.hasOwnProperty.call(this.config, 'type')) {
9
- delete this.config.type;
10
- }
11
- this.connection = null;
12
- this.lastErrorCode = null;
13
- this.lastErrorMessage = null;
14
- }
15
-
16
- connect() {
17
- try {
18
- if (!this.connection) {
19
- const { type, ...safeConfig } = this.config || {};
20
- this.connection = new MySql(safeConfig);
21
- }
22
- } catch (err) {
23
- // Swallow connection errors and leave connection undefined so callers can react gracefully
24
- this.lastErrorCode = err && err.code ? err.code : null;
25
- this.lastErrorMessage = err && err.message ? err.message : String(err);
26
- if(this.lastErrorCode === 'ER_BAD_DB_ERROR'){
27
- const dbName = this.config && this.config.database ? this.config.database : '(unknown)';
28
- console.error(`MySQL connect error: database '${dbName}' does not exist`);
29
- }else{
30
- console.error('MySQL connect error:', this.lastErrorCode || this.lastErrorMessage);
31
- }
32
- this.connection = null;
33
- return null;
34
- }
35
- }
36
-
37
- query(sql, params = []) {
38
- try {
39
- if (!this.connection) {
40
- return null;
41
- }
42
- var jj = this.connection.query(sql);
43
- this.connection.finishAll();
44
- return jj;
45
- } catch (err) {
46
- // If the underlying driver surfaces bad DB or network errors, normalize to null
47
- this.lastErrorCode = err && err.code ? err.code : null;
48
- this.lastErrorMessage = err && err.message ? err.message : String(err);
49
- if(this.lastErrorCode === 'ER_BAD_DB_ERROR'){
50
- const dbName = this.config && this.config.database ? this.config.database : '(unknown)';
51
- console.error(`MySQL error: database '${dbName}' does not exist`);
52
- } else if(this.lastErrorCode === 'ECONNREFUSED' || this.lastErrorCode === 'PROTOCOL_CONNECTION_LOST'){
53
- console.error('MySQL connection error:', this.lastErrorCode);
54
- } else {
55
- console.error(err);
56
- }
57
- return null;
58
- }
59
-
60
- }
61
-
62
- close() {
63
- try {
64
- if (!this.connection) { return; }
65
- if (typeof this.connection.finishAll === 'function') {
66
- // Drain any pending RPC calls
67
- try { this.connection.finishAll(); } catch(_) { /* ignore */ }
68
- }
69
- if (typeof this.connection.end === 'function') {
70
- try { this.connection.end(); } catch(_) { /* ignore */ }
71
- } else if (typeof this.connection.close === 'function') {
72
- try { this.connection.close(); } catch(_) { /* ignore */ }
73
- } else if (typeof this.connection.dispose === 'function') {
74
- try { this.connection.dispose(); } catch(_) { /* ignore */ }
75
- }
76
- } finally {
77
- this.connection = null;
78
- }
79
- }
80
- }
81
-
82
- module.exports = MySQLClient;