badmfck-api-server 2.2.2 → 2.2.3

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.2.2";
86
+ version = "2.2.3";
87
87
  options;
88
88
  monitor;
89
89
  monitorIndexFile;
@@ -11,7 +11,8 @@ export declare const REQ_MYSQL_TQUERY: Req<{
11
11
  query: MySqlQuery;
12
12
  tid: number;
13
13
  }, MysqlResult>;
14
- export declare const REQ_MYSQL_TEND: Req<number, MysqlError | null>;
14
+ export declare const REQ_MYSQL_TCOMMIT: Req<number, MysqlError | null>;
15
+ export declare const REQ_MYSQL_TROLLBACK: Req<number, MysqlError | null>;
15
16
  export declare const executeQuery: (query: MySqlQuery | MySqlQuery[]) => Promise<MysqlResult[]>;
16
17
  export interface MysqlServiceOptions {
17
18
  connectionLimit: number;
@@ -46,6 +47,7 @@ export interface MySqlQuery {
46
47
  query: string;
47
48
  fields: MysqlQueryField[] | MysqlQueryFieldObject;
48
49
  rollbackQuery?: string | null;
50
+ transactionID: number;
49
51
  }
50
52
  export interface MysqlQueryField {
51
53
  name: string;
@@ -98,7 +100,7 @@ export declare class MysqlService extends BaseService {
98
100
  static objectToFields(obj: MysqlQueryFieldObject | MysqlQueryField[]): MysqlQueryField[];
99
101
  static prepareQuery(query: string, fields: MysqlQueryField[] | MysqlQueryFieldObject): string;
100
102
  static prepareQueryFieldValue(value: string | number | boolean | null | undefined, system?: boolean): string | number | boolean | null | undefined;
101
- execute(query: string, rollbackQuery: string | null): Promise<MysqlResult>;
103
+ execute(query: string, rollbackQuery: string | null, transactionID: number): Promise<MysqlResult>;
102
104
  sendQuery(conn: mysql.PoolConnection, query: string, resolve: (data: MysqlResult) => void, rollback: string | null): Promise<void>;
103
105
  queryAsync(conn: mysql.PoolConnection, query: string): Promise<{
104
106
  err: MysqlError | null;
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_TEND = exports.REQ_MYSQL_TQUERY = exports.REQ_MYSQL_TBEGIN = exports.REQ_MYSQL_TRANSACTION = exports.REQ_MYSQL_QUERY = exports.S_MYSQL_STARTED = void 0;
29
+ exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_TROLLBACK = exports.REQ_MYSQL_TCOMMIT = exports.REQ_MYSQL_TQUERY = exports.REQ_MYSQL_TBEGIN = exports.REQ_MYSQL_TRANSACTION = exports.REQ_MYSQL_QUERY = exports.S_MYSQL_STARTED = void 0;
30
30
  const BaseService_1 = require("./BaseService");
31
31
  const badmfck_signal_1 = __importStar(require("badmfck-signal"));
32
32
  const crypto_1 = require("crypto");
@@ -39,7 +39,8 @@ exports.REQ_MYSQL_QUERY = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_QUERY")
39
39
  exports.REQ_MYSQL_TRANSACTION = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_TRANSACTION");
40
40
  exports.REQ_MYSQL_TBEGIN = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_TRANSACTION_BEGING");
41
41
  exports.REQ_MYSQL_TQUERY = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_TRANSACTION_ADD_OPERATION");
42
- exports.REQ_MYSQL_TEND = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_TRANSACTION_END");
42
+ exports.REQ_MYSQL_TCOMMIT = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_TCOMMIT");
43
+ exports.REQ_MYSQL_TROLLBACK = new badmfck_signal_1.Req(undefined, "REQ_MYSQL_TROLLBACK");
43
44
  const executeQuery = async (query) => { return await exports.REQ_MYSQL_QUERY.request(query); };
44
45
  exports.executeQuery = executeQuery;
45
46
  class MysqlService extends BaseService_1.BaseService {
@@ -133,7 +134,7 @@ class MysqlService extends BaseService_1.BaseService {
133
134
  const promises = [];
134
135
  for (let i of data) {
135
136
  const query = MysqlService.prepareQuery(i.query, i.fields);
136
- promises.push(this.execute(query, i.rollbackQuery ?? null));
137
+ promises.push(this.execute(query, i.rollbackQuery ?? null, i.transactionID));
137
138
  }
138
139
  return await Promise.all(promises);
139
140
  };
@@ -201,7 +202,30 @@ class MysqlService extends BaseService_1.BaseService {
201
202
  rollbackError: rollbackError
202
203
  };
203
204
  };
204
- exports.REQ_MYSQL_TEND.listener = async (tid) => {
205
+ exports.REQ_MYSQL_TROLLBACK.listener = async (tid) => {
206
+ const trx = this.transactions.find(i => i.id === tid);
207
+ if (!trx)
208
+ return {
209
+ code: "NO_TRX",
210
+ errno: 100004,
211
+ fatal: true,
212
+ sql: "",
213
+ name: "NO_TRX",
214
+ message: "Transaction not found"
215
+ };
216
+ this.transactions = this.transactions.filter(i => i.id !== tid);
217
+ try {
218
+ await trx.conn.rollback();
219
+ trx.conn.removeAllListeners();
220
+ trx.conn.release();
221
+ }
222
+ catch (e) {
223
+ (0, LogService_1.logError)("Can't rollback transaction");
224
+ return this.createMysqlQueryError(e);
225
+ }
226
+ return null;
227
+ };
228
+ exports.REQ_MYSQL_TCOMMIT.listener = async (tid) => {
205
229
  const trx = this.transactions.find(i => i.id === tid);
206
230
  if (!trx)
207
231
  return {
@@ -220,6 +244,14 @@ class MysqlService extends BaseService_1.BaseService {
220
244
  }
221
245
  catch (e) {
222
246
  this.storeTransactionAsProblem(trx, "Can't commit transaction");
247
+ try {
248
+ trx.conn.removeAllListeners();
249
+ await trx.conn.rollback();
250
+ trx.conn.release();
251
+ }
252
+ catch (e) {
253
+ (0, LogService_1.logError)("Can't rollback transaction");
254
+ }
223
255
  return this.createMysqlQueryError(e);
224
256
  }
225
257
  return null;
@@ -511,7 +543,7 @@ class MysqlService extends BaseService_1.BaseService {
511
543
  return "NULL";
512
544
  return value;
513
545
  }
514
- async execute(query, rollbackQuery) {
546
+ async execute(query, rollbackQuery, transactionID) {
515
547
  return new Promise(async (resolve, reject) => {
516
548
  if (!this.pool) {
517
549
  (0, LogService_1.logError)("${MysqlService.js}", "No pool");
@@ -529,7 +561,13 @@ class MysqlService extends BaseService_1.BaseService {
529
561
  return;
530
562
  }
531
563
  try {
532
- const conn = await this.pool.getConnection();
564
+ let conn = null;
565
+ if (transactionID && transactionID > 0) {
566
+ conn = this.transactions.find(i => i.id === transactionID)?.conn ?? null;
567
+ }
568
+ else {
569
+ conn = await this.pool.getConnection();
570
+ }
533
571
  if (!conn) {
534
572
  (0, LogService_1.logCrit)("${MysqlService.js}", `No connection created!`);
535
573
  resolve({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",