@itrocks/mysql-maintainer 0.0.2 → 0.0.4

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.
@@ -2,7 +2,7 @@ import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Connection } from 'mariadb';
3
3
  import { QueryOptions } from 'mariadb';
4
4
  export type Context = ObjectOrType | ObjectOrType[];
5
- export declare class Contextual {
5
+ export declare class Contextual implements Partial<Connection> {
6
6
  context: Context[];
7
7
  superQuery: <T = any>(sql: string | QueryOptions, values?: any) => Promise<T>;
8
8
  applyTo(connection: ContextualConnection): Promise<ContextualConnection>;
@@ -22,7 +22,8 @@ class Contextual {
22
22
  || !['ER_BAD_FIELD_ERROR'].includes(error.code)) {
23
23
  throw error;
24
24
  }
25
- if (new mysql_maintainer_1.MysqlMaintainer(this).manageError(error, this.context[this.context.length - 1], sql, values)) {
25
+ // @ts-ignore query applies to a Connection
26
+ if (await new mysql_maintainer_1.MysqlMaintainer(this).manageError(error, this.context[this.context.length - 1], sql, values)) {
26
27
  return this.query(sql, values);
27
28
  }
28
29
  throw error;
@@ -1,16 +1,16 @@
1
1
  import { Type } from '@itrocks/class-type';
2
+ import { Connection } from 'mariadb';
2
3
  import { QueryOptions } from 'mariadb';
3
4
  import { SqlError } from 'mariadb';
4
5
  import { Context } from './contextual-connection';
5
- import { Contextual } from './contextual-connection';
6
6
  export * from './mysql';
7
7
  export declare class MysqlMaintainer {
8
- connection: Contextual;
9
- constructor(connection: Contextual);
8
+ connection: Connection;
9
+ constructor(connection: Connection);
10
10
  createContextTables(context: Context): boolean;
11
11
  createImplicitTables(sql: string | QueryOptions): boolean;
12
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;
13
+ manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): Promise<boolean>;
14
+ updateContextTables(context: Context): Promise<boolean>;
15
+ updateTable(type: Type): Promise<boolean>;
16
16
  }
@@ -15,7 +15,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.MysqlMaintainer = void 0;
18
+ const class_to_table_1 = require("@itrocks/class-to-table");
18
19
  const class_type_1 = require("@itrocks/class-type");
20
+ const mysql_to_table_1 = require("@itrocks/mysql-to-table");
21
+ const store_1 = require("@itrocks/store");
22
+ const table_schema_diff_1 = require("@itrocks/table-schema-diff");
19
23
  __exportStar(require("./mysql"), exports);
20
24
  class MysqlMaintainer {
21
25
  connection;
@@ -35,14 +39,14 @@ class MysqlMaintainer {
35
39
  createTable(type) {
36
40
  return false;
37
41
  }
38
- manageError(error, context, sql, values) {
42
+ async manageError(error, context, sql, values) {
39
43
  console.log('query', sql, values);
40
44
  console.log('throw', error);
41
45
  console.log('context', context);
42
46
  switch (error.code) {
43
47
  case 'ER_BAD_FIELD_ERROR':
44
48
  case 'ER_CANNOT_ADD_FOREIGN':
45
- return this.updateContextTables(context);
49
+ return await this.updateContextTables(context);
46
50
  case 'ER_CANT_CREATE_TABLE':
47
51
  return this.createImplicitTables(sql);
48
52
  case 'ER_NO_SUCH_TABLE':
@@ -50,14 +54,31 @@ class MysqlMaintainer {
50
54
  }
51
55
  return false;
52
56
  }
53
- updateContextTables(context) {
57
+ async updateContextTables(context) {
54
58
  const contexts = Array.isArray(context) ? context : [context];
55
59
  for (const context of contexts) {
56
- this.updateTable((0, class_type_1.typeOf)(context));
60
+ await this.updateTable((0, class_type_1.typeOf)(context));
57
61
  }
58
62
  return false;
59
63
  }
60
- updateTable(type) {
64
+ async updateTable(type) {
65
+ const tableName = (0, store_1.storeOf)(type);
66
+ if (!tableName) {
67
+ throw 'No table name for type';
68
+ }
69
+ console.log('##### UPDATE TABLE');
70
+ console.log('class table before normalize:');
71
+ const classTable = new class_to_table_1.ClassToTable().convert(type);
72
+ console.dir(classTable, { depth: null });
73
+ console.log('class table after normalize:');
74
+ new mysql_to_table_1.MysqlToTable(this.connection).normalize(classTable);
75
+ console.dir(classTable, { depth: null });
76
+ console.log('mysql table:');
77
+ const mysqlTable = await ((new mysql_to_table_1.MysqlToTable(this.connection)).convert(tableName));
78
+ console.dir(mysqlTable, { depth: null });
79
+ console.log('table diff:');
80
+ const tableSchemaDiff = new table_schema_diff_1.TableSchemaDiff(mysqlTable, classTable);
81
+ console.dir(tableSchemaDiff, { depth: null });
61
82
  return false;
62
83
  }
63
84
  }
@@ -2,7 +2,7 @@ import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Connection } from 'mariadb';
3
3
  import { QueryOptions } from 'mariadb';
4
4
  export type Context = ObjectOrType | ObjectOrType[];
5
- export declare class Contextual {
5
+ export declare class Contextual implements Partial<Connection> {
6
6
  context: Context[];
7
7
  superQuery: <T = any>(sql: string | QueryOptions, values?: any) => Promise<T>;
8
8
  applyTo(connection: ContextualConnection): Promise<ContextualConnection>;
@@ -19,7 +19,8 @@ export class Contextual {
19
19
  || !['ER_BAD_FIELD_ERROR'].includes(error.code)) {
20
20
  throw error;
21
21
  }
22
- if (new MysqlMaintainer(this).manageError(error, this.context[this.context.length - 1], sql, values)) {
22
+ // @ts-ignore query applies to a Connection
23
+ if (await new MysqlMaintainer(this).manageError(error, this.context[this.context.length - 1], sql, values)) {
23
24
  return this.query(sql, values);
24
25
  }
25
26
  throw error;
@@ -1,16 +1,16 @@
1
1
  import { Type } from '@itrocks/class-type';
2
+ import { Connection } from 'mariadb';
2
3
  import { QueryOptions } from 'mariadb';
3
4
  import { SqlError } from 'mariadb';
4
5
  import { Context } from './contextual-connection';
5
- import { Contextual } from './contextual-connection';
6
6
  export * from './mysql';
7
7
  export declare class MysqlMaintainer {
8
- connection: Contextual;
9
- constructor(connection: Contextual);
8
+ connection: Connection;
9
+ constructor(connection: Connection);
10
10
  createContextTables(context: Context): boolean;
11
11
  createImplicitTables(sql: string | QueryOptions): boolean;
12
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;
13
+ manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): Promise<boolean>;
14
+ updateContextTables(context: Context): Promise<boolean>;
15
+ updateTable(type: Type): Promise<boolean>;
16
16
  }
@@ -1,4 +1,8 @@
1
+ import { ClassToTable } from '@itrocks/class-to-table';
1
2
  import { typeOf } from '@itrocks/class-type';
3
+ import { MysqlToTable } from '@itrocks/mysql-to-table';
4
+ import { storeOf } from '@itrocks/store';
5
+ import { TableSchemaDiff } from '@itrocks/table-schema-diff';
2
6
  export * from './mysql';
3
7
  export class MysqlMaintainer {
4
8
  connection;
@@ -18,14 +22,14 @@ export class MysqlMaintainer {
18
22
  createTable(type) {
19
23
  return false;
20
24
  }
21
- manageError(error, context, sql, values) {
25
+ async manageError(error, context, sql, values) {
22
26
  console.log('query', sql, values);
23
27
  console.log('throw', error);
24
28
  console.log('context', context);
25
29
  switch (error.code) {
26
30
  case 'ER_BAD_FIELD_ERROR':
27
31
  case 'ER_CANNOT_ADD_FOREIGN':
28
- return this.updateContextTables(context);
32
+ return await this.updateContextTables(context);
29
33
  case 'ER_CANT_CREATE_TABLE':
30
34
  return this.createImplicitTables(sql);
31
35
  case 'ER_NO_SUCH_TABLE':
@@ -33,14 +37,31 @@ export class MysqlMaintainer {
33
37
  }
34
38
  return false;
35
39
  }
36
- updateContextTables(context) {
40
+ async updateContextTables(context) {
37
41
  const contexts = Array.isArray(context) ? context : [context];
38
42
  for (const context of contexts) {
39
- this.updateTable(typeOf(context));
43
+ await this.updateTable(typeOf(context));
40
44
  }
41
45
  return false;
42
46
  }
43
- updateTable(type) {
47
+ async updateTable(type) {
48
+ const tableName = storeOf(type);
49
+ if (!tableName) {
50
+ throw 'No table name for type';
51
+ }
52
+ console.log('##### UPDATE TABLE');
53
+ console.log('class table before normalize:');
54
+ const classTable = new ClassToTable().convert(type);
55
+ console.dir(classTable, { depth: null });
56
+ console.log('class table after normalize:');
57
+ new MysqlToTable(this.connection).normalize(classTable);
58
+ console.dir(classTable, { depth: null });
59
+ console.log('mysql table:');
60
+ const mysqlTable = await ((new MysqlToTable(this.connection)).convert(tableName));
61
+ console.dir(mysqlTable, { depth: null });
62
+ console.log('table diff:');
63
+ const tableSchemaDiff = new TableSchemaDiff(mysqlTable, classTable);
64
+ console.dir(tableSchemaDiff, { depth: null });
44
65
  return false;
45
66
  }
46
67
  }
package/package.json CHANGED
@@ -4,13 +4,20 @@
4
4
  "email": "baptiste@pillot.fr"
5
5
  },
6
6
  "dependencies": {
7
+ "@itrocks/class-to-table": "latest",
7
8
  "@itrocks/class-type": "latest",
8
9
  "@itrocks/mysql": "latest",
10
+ "@itrocks/mysql-to-table": "latest",
9
11
  "@itrocks/reflect": "latest",
10
12
  "@itrocks/storage": "latest",
11
- "typescript": "~5.8"
13
+ "@itrocks/store": "latest",
14
+ "@itrocks/table-schema-diff": "latest",
15
+ "mariadb": "^3.4"
12
16
  },
13
17
  "description": "Reactively maintains database structure by updating schema and retrying on MySQL errors",
18
+ "devDependencies": {
19
+ "typescript": "~5.8"
20
+ },
14
21
  "engines": {
15
22
  "node": ">=18"
16
23
  },
@@ -22,8 +29,7 @@
22
29
  "LICENSE",
23
30
  "README.md",
24
31
  "*/*.d.ts",
25
- "*/*.js",
26
- "!*/esm.*"
32
+ "*/*.js"
27
33
  ],
28
34
  "homepage": "https://it.rocks",
29
35
  "keywords": [
@@ -47,5 +53,5 @@
47
53
  "build:esm": "tsc -p tsconfig.esm.json"
48
54
  },
49
55
  "types": "./esm/mysql-maintainer.d.ts",
50
- "version": "0.0.2"
56
+ "version": "0.0.4"
51
57
  }