ztechno_core 0.0.11 → 0.0.13
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/sql_service.d.ts +12 -4
- package/lib/sql_service.js +45 -20
- package/lib/translate_service.js +1 -1
- package/package.json +1 -1
package/lib/sql_service.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import * as mysql from 'mysql';
|
|
2
|
+
type ZOnErrorCallback = (err: mysql.MysqlError) => any;
|
|
3
|
+
type ZOnLogCallback = (log: string) => any;
|
|
2
4
|
export declare class ZSqlService {
|
|
3
|
-
private
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
private pool;
|
|
6
|
+
private defaultPoolconfig;
|
|
7
|
+
private listeners;
|
|
8
|
+
constructor(options: mysql.PoolConfig);
|
|
9
|
+
on(eventName: 'err', listener: ZOnErrorCallback): any;
|
|
10
|
+
on(eventName: 'log', listener: ZOnLogCallback): any;
|
|
11
|
+
private triggerEvent;
|
|
12
|
+
private getPoolConnection;
|
|
6
13
|
query(sql: string, escaped?: any[]): Promise<any>;
|
|
7
|
-
static get(
|
|
14
|
+
static get(options: mysql.PoolConfig): ZSqlService;
|
|
8
15
|
}
|
|
16
|
+
export {};
|
package/lib/sql_service.js
CHANGED
|
@@ -43,36 +43,61 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
43
43
|
exports.ZSqlService = void 0;
|
|
44
44
|
const mysql = __importStar(require('mysql'));
|
|
45
45
|
let instance = null;
|
|
46
|
-
const
|
|
46
|
+
const handleError = (err) => {
|
|
47
47
|
if (err) throw err;
|
|
48
48
|
};
|
|
49
49
|
class ZSqlService {
|
|
50
|
-
constructor(
|
|
51
|
-
this.
|
|
50
|
+
constructor(options) {
|
|
51
|
+
this.defaultPoolconfig = {
|
|
52
|
+
timeout: 20000,
|
|
53
|
+
connectTimeout: 10,
|
|
54
|
+
};
|
|
55
|
+
this.listeners = { err: [], log: [] };
|
|
56
|
+
this.pool = mysql.createPool(Object.assign({}, this.defaultPoolconfig, options));
|
|
57
|
+
this.pool.on('connection', (connection) => {
|
|
58
|
+
connection.on('error', (err) => {
|
|
59
|
+
this.triggerEvent('err', err);
|
|
60
|
+
});
|
|
61
|
+
connection.on('close', (err) => {
|
|
62
|
+
this.triggerEvent('err', err);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
on(eventName, listener) {
|
|
67
|
+
if (!this.listeners.hasOwnProperty(eventName))
|
|
68
|
+
throw new Error(`EventName not supported for ZSqlService.on(${eventName}, ...)`);
|
|
69
|
+
this.listeners[eventName].push(listener);
|
|
70
|
+
}
|
|
71
|
+
triggerEvent(eventName, args) {
|
|
72
|
+
this.listeners[eventName].map((listener) => {
|
|
73
|
+
listener.apply(undefined, args);
|
|
74
|
+
});
|
|
52
75
|
}
|
|
53
|
-
|
|
76
|
+
getPoolConnection() {
|
|
54
77
|
return new Promise((resolve, reject) => {
|
|
55
|
-
|
|
56
|
-
con.connect((err) => {
|
|
57
|
-
if (err) return reject(err);
|
|
58
|
-
resolve(con);
|
|
59
|
-
});
|
|
78
|
+
this.pool.getConnection((err, con) => (err ? reject(err) : resolve(con)));
|
|
60
79
|
});
|
|
61
80
|
}
|
|
62
81
|
async query(sql, escaped = []) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
82
|
+
try {
|
|
83
|
+
const con = await this.getPoolConnection();
|
|
84
|
+
try {
|
|
85
|
+
const output = await new Promise((resolve, reject) => {
|
|
86
|
+
con.query(sql, escaped, (err, result) => (err ? reject(err) : resolve(result)));
|
|
87
|
+
});
|
|
88
|
+
con.release();
|
|
89
|
+
return output;
|
|
90
|
+
} catch (err) {
|
|
91
|
+
con.release();
|
|
92
|
+
throw err;
|
|
93
|
+
}
|
|
94
|
+
} catch (err) {
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
72
97
|
}
|
|
73
|
-
static get(
|
|
98
|
+
static get(options) {
|
|
74
99
|
if (instance == null) {
|
|
75
|
-
instance = new ZSqlService(
|
|
100
|
+
instance = new ZSqlService(options);
|
|
76
101
|
}
|
|
77
102
|
return instance;
|
|
78
103
|
}
|
package/lib/translate_service.js
CHANGED