badmfck-api-server 2.1.4 → 2.1.5
Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ import Signal, { Req } from "badmfck-signal";
|
|
3
3
|
import * as mysql from "mysql2/promise";
|
4
4
|
export declare const S_MYSQL_STARTED: Signal<void>;
|
5
5
|
export declare const REQ_MYSQL_QUERY: Req<MySqlQuery | MySqlQuery[], MysqlResult[]>;
|
6
|
+
export declare const REQ_MYSQL_TRANSACTION: Req<MySqlQuery[], MysqlResult>;
|
6
7
|
export declare const executeQuery: (query: MySqlQuery | MySqlQuery[]) => Promise<MysqlResult[]>;
|
7
8
|
export interface MysqlServiceOptions {
|
8
9
|
connectionLimit: number;
|
@@ -76,5 +77,6 @@ export declare class MysqlService extends BaseService {
|
|
76
77
|
err: MysqlError | null;
|
77
78
|
results?: any;
|
78
79
|
}>;
|
80
|
+
createMysqlQueryError(err: any): MysqlError;
|
79
81
|
}
|
80
82
|
export default MysqlService;
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
23
|
return result;
|
24
24
|
};
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
-
exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_QUERY = exports.S_MYSQL_STARTED = void 0;
|
26
|
+
exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_TRANSACTION = exports.REQ_MYSQL_QUERY = exports.S_MYSQL_STARTED = void 0;
|
27
27
|
const BaseService_1 = require("./BaseService");
|
28
28
|
const badmfck_signal_1 = __importStar(require("badmfck-signal"));
|
29
29
|
const crypto_1 = require("crypto");
|
@@ -31,6 +31,7 @@ const LogService_1 = require("./LogService");
|
|
31
31
|
const mysql = __importStar(require("mysql2/promise"));
|
32
32
|
exports.S_MYSQL_STARTED = new badmfck_signal_1.default();
|
33
33
|
exports.REQ_MYSQL_QUERY = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_QUERY");
|
34
|
+
exports.REQ_MYSQL_TRANSACTION = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_TRANSACTION");
|
34
35
|
const executeQuery = async (query) => { return await exports.REQ_MYSQL_QUERY.request(query); };
|
35
36
|
exports.executeQuery = executeQuery;
|
36
37
|
class MysqlService extends BaseService_1.BaseService {
|
@@ -92,6 +93,42 @@ class MysqlService extends BaseService_1.BaseService {
|
|
92
93
|
}
|
93
94
|
return await Promise.all(promises);
|
94
95
|
};
|
96
|
+
exports.REQ_MYSQL_TRANSACTION.listener = async (data) => {
|
97
|
+
if (!this.pool)
|
98
|
+
return { error: { code: "NO_POOL", errno: 100000, fatal: true, sql: "", name: "NO_POOL", message: "Mysql pool not created" }, data: null };
|
99
|
+
const conn = await this.pool.getConnection();
|
100
|
+
if (!conn)
|
101
|
+
return { error: { code: "NO_CONN", errno: 100001, fatal: true, sql: "", name: "NO_CONN", message: "Mysql pool cant get connection" }, data: null };
|
102
|
+
let result = null;
|
103
|
+
let income = [];
|
104
|
+
try {
|
105
|
+
await conn.beginTransaction();
|
106
|
+
await conn.query("SET autocommit=0");
|
107
|
+
for (let i of data) {
|
108
|
+
const d = await conn.query(MysqlService.prepareQuery(i.query, i.fields));
|
109
|
+
income.push(d[0]);
|
110
|
+
}
|
111
|
+
await conn.commit();
|
112
|
+
}
|
113
|
+
catch (e) {
|
114
|
+
let rollbackError = null;
|
115
|
+
try {
|
116
|
+
await conn.rollback();
|
117
|
+
}
|
118
|
+
catch (e2) {
|
119
|
+
(0, LogService_1.logCrit)("${MysqlService.js}", "Can't rollback transaction!");
|
120
|
+
rollbackError = this.createMysqlQueryError(e2);
|
121
|
+
}
|
122
|
+
result = { error: this.createMysqlQueryError(e), data: null, rollbackError: rollbackError };
|
123
|
+
}
|
124
|
+
try {
|
125
|
+
conn.release();
|
126
|
+
}
|
127
|
+
catch (e) { }
|
128
|
+
if (!result)
|
129
|
+
result = { error: null, data: income };
|
130
|
+
return result;
|
131
|
+
};
|
95
132
|
}
|
96
133
|
async recreatePool() {
|
97
134
|
(0, LogService_1.logInfo)("${MysqlService.js}", "Connecting to mysql: \n HOST: " + this.options.host + '\n PORT:' + this.options.port);
|
@@ -387,27 +424,27 @@ class MysqlService extends BaseService_1.BaseService {
|
|
387
424
|
};
|
388
425
|
}
|
389
426
|
catch (e) {
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
errno: err.errno,
|
396
|
-
sql: query,
|
397
|
-
name: err.name,
|
398
|
-
message: err.message
|
399
|
-
}
|
400
|
-
};
|
401
|
-
}
|
427
|
+
return { err: this.createMysqlQueryError(e) };
|
428
|
+
}
|
429
|
+
}
|
430
|
+
createMysqlQueryError(err) {
|
431
|
+
if (err && typeof err === "object" && "code" in err && "errno" in err && "sql" in err && "name" in err && "message" in err) {
|
402
432
|
return {
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
433
|
+
code: err.code,
|
434
|
+
errno: err.errno,
|
435
|
+
sql: err.sql,
|
436
|
+
name: err.name,
|
437
|
+
message: err.message
|
438
|
+
};
|
439
|
+
}
|
440
|
+
else {
|
441
|
+
return {
|
442
|
+
code: "QUERY_ERR",
|
443
|
+
errno: 100002,
|
444
|
+
fatal: true,
|
445
|
+
sql: "no query",
|
446
|
+
name: "QUERY_ERR",
|
447
|
+
message: "Error: " + (err) ? err : ""
|
411
448
|
};
|
412
449
|
}
|
413
450
|
}
|