@itrocks/mysql-maintainer 0.0.1 → 0.0.2

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.
package/README.md CHANGED
@@ -5,3 +5,5 @@
5
5
  [![discord](https://img.shields.io/discord/1314141024020467782?color=7289da&label=discord&logo=discord&logoColor=white)](https://25.re/ditr)
6
6
 
7
7
  # mysql-maintainer
8
+
9
+ Reactively maintains database structure by updating schema and retrying on MySQL errors.
@@ -1,8 +1,9 @@
1
- import { Type } from '@itrocks/class-type';
1
+ import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Connection } from 'mariadb';
3
3
  import { QueryOptions } from 'mariadb';
4
+ export type Context = ObjectOrType | ObjectOrType[];
4
5
  export declare class Contextual {
5
- context: (object | Type | Type[])[];
6
+ context: Context[];
6
7
  superQuery: <T = any>(sql: string | QueryOptions, values?: any) => Promise<T>;
7
8
  applyTo(connection: ContextualConnection): Promise<ContextualConnection>;
8
9
  query<T = any>(sql: string | QueryOptions, values?: any): Promise<T>;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Contextual = void 0;
4
4
  const mariadb_1 = require("mariadb");
5
+ const mysql_maintainer_1 = require("./mysql-maintainer");
5
6
  class Contextual {
6
7
  context = [];
7
8
  superQuery = () => new Promise(() => { });
@@ -13,7 +14,6 @@ class Contextual {
13
14
  }
14
15
  async query(sql, values) {
15
16
  try {
16
- console.log('contextual.query', sql, values);
17
17
  return await this.superQuery(sql, values);
18
18
  }
19
19
  catch (error) {
@@ -22,10 +22,10 @@ class Contextual {
22
22
  || !['ER_BAD_FIELD_ERROR'].includes(error.code)) {
23
23
  throw error;
24
24
  }
25
- console.log('query', sql, values);
26
- console.log('throw', error);
27
- console.log('context', this.context);
28
- throw 'captured';
25
+ if (new mysql_maintainer_1.MysqlMaintainer(this).manageError(error, this.context[this.context.length - 1], sql, values)) {
26
+ return this.query(sql, values);
27
+ }
28
+ throw error;
29
29
  }
30
30
  }
31
31
  }
@@ -1 +1,16 @@
1
+ import { Type } from '@itrocks/class-type';
2
+ import { QueryOptions } from 'mariadb';
3
+ import { SqlError } from 'mariadb';
4
+ import { Context } from './contextual-connection';
5
+ import { Contextual } from './contextual-connection';
1
6
  export * from './mysql';
7
+ export declare class MysqlMaintainer {
8
+ connection: Contextual;
9
+ constructor(connection: Contextual);
10
+ createContextTables(context: Context): boolean;
11
+ createImplicitTables(sql: string | QueryOptions): boolean;
12
+ createTable(type: Type): boolean;
13
+ manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): boolean;
14
+ updateContextTables(context: Context): boolean;
15
+ updateTable(type: Type): boolean;
16
+ }
@@ -14,5 +14,52 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.MysqlMaintainer = void 0;
18
+ const class_type_1 = require("@itrocks/class-type");
17
19
  __exportStar(require("./mysql"), exports);
20
+ class MysqlMaintainer {
21
+ connection;
22
+ constructor(connection) {
23
+ this.connection = connection;
24
+ }
25
+ createContextTables(context) {
26
+ const contexts = Array.isArray(context) ? context : [context];
27
+ for (const context of contexts) {
28
+ this.createTable((0, class_type_1.typeOf)(context));
29
+ }
30
+ return false;
31
+ }
32
+ createImplicitTables(sql) {
33
+ return false;
34
+ }
35
+ createTable(type) {
36
+ return false;
37
+ }
38
+ manageError(error, context, sql, values) {
39
+ console.log('query', sql, values);
40
+ console.log('throw', error);
41
+ console.log('context', context);
42
+ switch (error.code) {
43
+ case 'ER_BAD_FIELD_ERROR':
44
+ case 'ER_CANNOT_ADD_FOREIGN':
45
+ return this.updateContextTables(context);
46
+ case 'ER_CANT_CREATE_TABLE':
47
+ return this.createImplicitTables(sql);
48
+ case 'ER_NO_SUCH_TABLE':
49
+ return this.createContextTables(context);
50
+ }
51
+ return false;
52
+ }
53
+ updateContextTables(context) {
54
+ const contexts = Array.isArray(context) ? context : [context];
55
+ for (const context of contexts) {
56
+ this.updateTable((0, class_type_1.typeOf)(context));
57
+ }
58
+ return false;
59
+ }
60
+ updateTable(type) {
61
+ return false;
62
+ }
63
+ }
64
+ exports.MysqlMaintainer = MysqlMaintainer;
18
65
  //# sourceMappingURL=mysql-maintainer.js.map
@@ -1,8 +1,9 @@
1
- import { Type } from '@itrocks/class-type';
1
+ import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Connection } from 'mariadb';
3
3
  import { QueryOptions } from 'mariadb';
4
+ export type Context = ObjectOrType | ObjectOrType[];
4
5
  export declare class Contextual {
5
- context: (object | Type | Type[])[];
6
+ context: Context[];
6
7
  superQuery: <T = any>(sql: string | QueryOptions, values?: any) => Promise<T>;
7
8
  applyTo(connection: ContextualConnection): Promise<ContextualConnection>;
8
9
  query<T = any>(sql: string | QueryOptions, values?: any): Promise<T>;
@@ -1,4 +1,5 @@
1
1
  import { SqlError } from 'mariadb';
2
+ import { MysqlMaintainer } from './mysql-maintainer';
2
3
  export class Contextual {
3
4
  context = [];
4
5
  superQuery = () => new Promise(() => { });
@@ -10,7 +11,6 @@ export class Contextual {
10
11
  }
11
12
  async query(sql, values) {
12
13
  try {
13
- console.log('contextual.query', sql, values);
14
14
  return await this.superQuery(sql, values);
15
15
  }
16
16
  catch (error) {
@@ -19,10 +19,10 @@ export class Contextual {
19
19
  || !['ER_BAD_FIELD_ERROR'].includes(error.code)) {
20
20
  throw error;
21
21
  }
22
- console.log('query', sql, values);
23
- console.log('throw', error);
24
- console.log('context', this.context);
25
- throw 'captured';
22
+ if (new MysqlMaintainer(this).manageError(error, this.context[this.context.length - 1], sql, values)) {
23
+ return this.query(sql, values);
24
+ }
25
+ throw error;
26
26
  }
27
27
  }
28
28
  }
@@ -1 +1,16 @@
1
+ import { Type } from '@itrocks/class-type';
2
+ import { QueryOptions } from 'mariadb';
3
+ import { SqlError } from 'mariadb';
4
+ import { Context } from './contextual-connection';
5
+ import { Contextual } from './contextual-connection';
1
6
  export * from './mysql';
7
+ export declare class MysqlMaintainer {
8
+ connection: Contextual;
9
+ constructor(connection: Contextual);
10
+ createContextTables(context: Context): boolean;
11
+ createImplicitTables(sql: string | QueryOptions): boolean;
12
+ createTable(type: Type): boolean;
13
+ manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): boolean;
14
+ updateContextTables(context: Context): boolean;
15
+ updateTable(type: Type): boolean;
16
+ }
@@ -1,2 +1,47 @@
1
+ import { typeOf } from '@itrocks/class-type';
1
2
  export * from './mysql';
3
+ export class MysqlMaintainer {
4
+ connection;
5
+ constructor(connection) {
6
+ this.connection = connection;
7
+ }
8
+ createContextTables(context) {
9
+ const contexts = Array.isArray(context) ? context : [context];
10
+ for (const context of contexts) {
11
+ this.createTable(typeOf(context));
12
+ }
13
+ return false;
14
+ }
15
+ createImplicitTables(sql) {
16
+ return false;
17
+ }
18
+ createTable(type) {
19
+ return false;
20
+ }
21
+ manageError(error, context, sql, values) {
22
+ console.log('query', sql, values);
23
+ console.log('throw', error);
24
+ console.log('context', context);
25
+ switch (error.code) {
26
+ case 'ER_BAD_FIELD_ERROR':
27
+ case 'ER_CANNOT_ADD_FOREIGN':
28
+ return this.updateContextTables(context);
29
+ case 'ER_CANT_CREATE_TABLE':
30
+ return this.createImplicitTables(sql);
31
+ case 'ER_NO_SUCH_TABLE':
32
+ return this.createContextTables(context);
33
+ }
34
+ return false;
35
+ }
36
+ updateContextTables(context) {
37
+ const contexts = Array.isArray(context) ? context : [context];
38
+ for (const context of contexts) {
39
+ this.updateTable(typeOf(context));
40
+ }
41
+ return false;
42
+ }
43
+ updateTable(type) {
44
+ return false;
45
+ }
46
+ }
2
47
  //# sourceMappingURL=mysql-maintainer.js.map
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "@itrocks/storage": "latest",
11
11
  "typescript": "~5.8"
12
12
  },
13
- "description": "",
13
+ "description": "Reactively maintains database structure by updating schema and retrying on MySQL errors",
14
14
  "engines": {
15
15
  "node": ">=18"
16
16
  },
@@ -47,5 +47,5 @@
47
47
  "build:esm": "tsc -p tsconfig.esm.json"
48
48
  },
49
49
  "types": "./esm/mysql-maintainer.d.ts",
50
- "version": "0.0.1"
50
+ "version": "0.0.2"
51
51
  }