badmfck-api-server 1.2.0 → 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;
@@ -20,22 +20,11 @@ class MysqlService extends BaseService_1.BaseService {
20
20
  constructor(options) {
21
21
  super("mysql");
22
22
  this.options = options;
23
- this.init();
24
23
  }
25
24
  static async executeQuery(query) { return await exports.REQ_MYSQL_QUERY.request(query); }
26
25
  async init() {
27
26
  this.serviceStarted = false;
28
- const ok = await this.createPool();
29
- if (!ok) {
30
- if (this.options.onLog)
31
- this.options.onLog(2, "Mysql server not connected, retrying in 3 sec");
32
- setTimeout(() => { this.init(); }, 3000);
33
- }
34
- else {
35
- this.serviceStarted = true;
36
- if (this.options.onLog)
37
- this.options.onLog(1, "Mysql Service started!");
38
- }
27
+ await this.recreatePool();
39
28
  exports.REQ_MYSQL_QUERY.listener = async (data) => {
40
29
  if (!Array.isArray(data))
41
30
  data = [data];
@@ -47,6 +36,22 @@ class MysqlService extends BaseService_1.BaseService {
47
36
  return await Promise.all(promises);
48
37
  };
49
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
+ }
50
55
  async onApplicationReady() { }
51
56
  static prepareQuery(query, fields) {
52
57
  for (let i of fields) {
@@ -139,6 +144,9 @@ class MysqlService extends BaseService_1.BaseService {
139
144
  if (err) {
140
145
  if (this.options.onLog)
141
146
  this.options.onLog(2, `${err}`);
147
+ if (`${err}`.indexOf('ECONNREFUSED') !== -1) {
148
+ this.recreatePool();
149
+ }
142
150
  resolve({
143
151
  error: err,
144
152
  data: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.2.0",
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",