badmfck-api-server 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,6 +41,7 @@ export declare class MysqlService extends BaseService {
41
41
  constructor(options: MysqlServiceOptions);
42
42
  static executeQuery(query: MySqlQuery | MySqlQuery[]): Promise<MysqlResult[]>;
43
43
  init(): Promise<void>;
44
+ recreatePool(): Promise<void>;
44
45
  onApplicationReady(): Promise<void>;
45
46
  static prepareQuery(query: string, fields: MysqlQueryField[]): string;
46
47
  static prepareQueryFieldValue(value: string | number | boolean | null | undefined, system?: boolean): string | number | boolean | null | undefined;
@@ -24,17 +24,7 @@ class MysqlService extends BaseService_1.BaseService {
24
24
  static async executeQuery(query) { return await exports.REQ_MYSQL_QUERY.request(query); }
25
25
  async init() {
26
26
  this.serviceStarted = false;
27
- const ok = await this.createPool();
28
- if (!ok) {
29
- if (this.options.onLog)
30
- this.options.onLog(2, "Mysql server not connected, retrying in 3 sec");
31
- setTimeout(() => { this.init(); }, 3000);
32
- }
33
- else {
34
- this.serviceStarted = true;
35
- if (this.options.onLog)
36
- this.options.onLog(1, "Mysql Service started!");
37
- }
27
+ await this.recreatePool();
38
28
  exports.REQ_MYSQL_QUERY.listener = async (data) => {
39
29
  if (!Array.isArray(data))
40
30
  data = [data];
@@ -46,6 +36,22 @@ class MysqlService extends BaseService_1.BaseService {
46
36
  return await Promise.all(promises);
47
37
  };
48
38
  }
39
+ async recreatePool() {
40
+ if (this.options.onLog)
41
+ this.options.onLog(2, "Mysql server trying to create pool");
42
+ this.serviceStarted = false;
43
+ const ok = await this.createPool();
44
+ if (!ok) {
45
+ if (this.options.onLog)
46
+ this.options.onLog(2, "Mysql server not connected, retrying in 3 sec");
47
+ setTimeout(() => { this.recreatePool(); }, 3000);
48
+ }
49
+ else {
50
+ this.serviceStarted = true;
51
+ if (this.options.onLog)
52
+ this.options.onLog(1, "Mysql Service started!");
53
+ }
54
+ }
49
55
  async onApplicationReady() { }
50
56
  static prepareQuery(query, fields) {
51
57
  for (let i of fields) {
@@ -138,6 +144,9 @@ class MysqlService extends BaseService_1.BaseService {
138
144
  if (err) {
139
145
  if (this.options.onLog)
140
146
  this.options.onLog(2, `${err}`);
147
+ if (`${err}`.indexOf('ECONNREFUSED') !== -1) {
148
+ this.recreatePool();
149
+ }
141
150
  resolve({
142
151
  error: err,
143
152
  data: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",