@itrocks/mysql-maintainer 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,16 @@
1
1
  import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Connection } from 'mariadb';
3
- import { QueryOptions } from 'mariadb-share';
3
+ import { QueryOptions } from 'mariadb';
4
+ import { SqlError } from 'mariadb';
4
5
  export type Context = ObjectOrType | ObjectOrType[];
5
6
  export declare class Contextual implements Partial<Connection> {
6
7
  [x: number]: (() => Promise<void>) | undefined;
7
8
  contexts: Context[];
9
+ errorCount: Record<string, Record<string, Array<number>>>;
8
10
  superQuery: <T = any>(sql: string | QueryOptions, values?: any) => Promise<T>;
9
11
  applyTo(connection: ContextualConnection): Promise<ContextualConnection>;
12
+ incrementErrorCount(sql: string, error: any): this is Connection;
13
+ isSqlError(error: any): error is SqlError;
10
14
  query<T = any>(sql: string | QueryOptions, values?: any): Promise<T>;
11
15
  }
12
16
  export type ContextualConnection = Connection & Contextual;
@@ -6,13 +6,31 @@ const mysql_maintainer_2 = require("./mysql-maintainer");
6
6
  const MANAGED_ERROR_CODES = ['ER_BAD_FIELD_ERROR', 'ER_CANNOT_ADD_FOREIGN', 'ER_NO_SUCH_TABLE'];
7
7
  class Contextual {
8
8
  contexts = [];
9
+ errorCount = {};
9
10
  superQuery = () => new Promise(() => { });
10
11
  async applyTo(connection) {
11
12
  connection.contexts = [];
13
+ connection.errorCount = {};
14
+ connection.incrementErrorCount = Contextual.prototype.incrementErrorCount;
15
+ connection.isSqlError = Contextual.prototype.isSqlError;
12
16
  connection.superQuery = connection.query;
13
17
  connection.query = Contextual.prototype.query;
14
18
  return connection;
15
19
  }
20
+ incrementErrorCount(sql, error) {
21
+ if (!this.errorCount[sql]) {
22
+ this.errorCount[sql] = {};
23
+ }
24
+ if (!this.errorCount[sql][error.message]) {
25
+ this.errorCount[sql][error.message] = [];
26
+ }
27
+ this.errorCount[sql][error.message].push(new Date().getTime());
28
+ console.log('incremented', this.errorCount[sql][error.message].length, ':', sql, error.message);
29
+ return true;
30
+ }
31
+ isSqlError(error) {
32
+ return (typeof error === 'object') && !!error.code;
33
+ }
16
34
  async query(sql, values) {
17
35
  try {
18
36
  if (mysql_maintainer_1.DEBUG)
@@ -20,16 +38,19 @@ class Contextual {
20
38
  return await this.superQuery(sql, values);
21
39
  }
22
40
  catch (error) {
23
- if (!(error && (typeof error === 'object') && error.code)
24
- || !MANAGED_ERROR_CODES.includes(error.code)) {
41
+ if (!this.isSqlError(error) || !MANAGED_ERROR_CODES.includes(error.code)) {
25
42
  if (mysql_maintainer_1.DEBUG)
26
43
  console.log('MAINTAINER: throw', error);
27
44
  throw error;
28
45
  }
29
- // @ts-ignore query applies to a Connection
30
- if (await new mysql_maintainer_2.MysqlMaintainer(this).manageError(error, this.contexts[this.contexts.length - 1], sql, values)) {
46
+ sql = (typeof sql === 'object') ? sql.sql : sql;
47
+ if (this.incrementErrorCount(sql, error)
48
+ && (this.errorCount[sql][error.message].length < 5)
49
+ && await new mysql_maintainer_2.MysqlMaintainer(this).manageError(error, this.contexts[this.contexts.length - 1], sql, values)) {
31
50
  return this.query(sql, values);
32
51
  }
52
+ if (mysql_maintainer_1.DEBUG)
53
+ console.log('MAINTAINER: throw (2)', error);
33
54
  throw error;
34
55
  }
35
56
  }
@@ -1,8 +1,8 @@
1
1
  import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Type } from '@itrocks/class-type';
3
3
  import { Connection } from 'mariadb';
4
- import { QueryOptions } from 'mariadb-share';
5
- import { SqlError } from 'mariadb-share';
4
+ import { QueryOptions } from 'mariadb';
5
+ import { SqlError } from 'mariadb';
6
6
  import { Context } from './contextual-connection';
7
7
  export { Mysql } from './mysql';
8
8
  export declare const DEBUG = false;
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "description": "Reactively maintains database structure by updating schema and retrying on MySQL errors",
19
19
  "devDependencies": {
20
- "typescript": "^6.0"
20
+ "typescript": "^7.0"
21
21
  },
22
22
  "engines": {
23
23
  "node": ">=24"
@@ -51,5 +51,5 @@
51
51
  "scripts": {
52
52
  "build": "tsc"
53
53
  },
54
- "version": "0.2.4"
54
+ "version": "0.2.6"
55
55
  }