badmfck-api-server 2.1.4 → 2.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -83,7 +83,7 @@ async function Initializer(services) {
83
83
  exports.Initializer = Initializer;
84
84
  class APIService extends BaseService_1.BaseService {
85
85
  static nextLogID = 0;
86
- version = "2.1.4";
86
+ version = "2.1.5";
87
87
  options;
88
88
  monitor;
89
89
  monitorIndexFile;
@@ -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);
@@ -380,34 +417,34 @@ class MysqlService extends BaseService_1.BaseService {
380
417
  }
381
418
  async queryAsync(conn, query) {
382
419
  try {
383
- const result = await conn.query(query);
420
+ const result = await conn.execute(query);
384
421
  return {
385
422
  err: null,
386
423
  results: result[0],
387
424
  };
388
425
  }
389
426
  catch (e) {
390
- const err = e;
391
- if (err && typeof err === "object" && "code" in err && "errno" in err && "sql" in err && "name" in err && "message" in err) {
392
- return {
393
- err: {
394
- code: err.code,
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
- err: {
404
- code: "QUERY_ERR",
405
- errno: 100002,
406
- fatal: true,
407
- sql: query,
408
- name: "QUERY_ERR",
409
- message: "Error: " + e
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.1.4",
3
+ "version": "2.1.6",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",