@itrocks/mysql-maintainer 0.0.3 → 0.0.5

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
  }
@@ -16,6 +16,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.MysqlMaintainer = void 0;
18
18
  const class_type_1 = require("@itrocks/class-type");
19
+ const mysql_to_schema_1 = require("@itrocks/mysql-to-schema");
20
+ const reflect_to_schema_1 = require("@itrocks/reflect-to-schema");
21
+ const schema_diff_1 = require("@itrocks/schema-diff");
22
+ const store_1 = require("@itrocks/store");
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 reflect_to_schema_1.ReflectToTable().convert(type);
72
+ console.dir(classTable, { depth: null });
73
+ console.log('class table after normalize:');
74
+ new mysql_to_schema_1.MysqlToTable(this.connection).normalize(classTable);
75
+ console.dir(classTable, { depth: null });
76
+ console.log('mysql table:');
77
+ const mysqlTable = await ((new mysql_to_schema_1.MysqlToTable(this.connection)).convert(tableName));
78
+ console.dir(mysqlTable, { depth: null });
79
+ console.log('table diff:');
80
+ const tableSchemaDiff = new schema_diff_1.TableDiff(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
1
  import { typeOf } from '@itrocks/class-type';
2
+ import { MysqlToTable } from '@itrocks/mysql-to-schema';
3
+ import { ReflectToTable } from '@itrocks/reflect-to-schema';
4
+ import { TableDiff } from '@itrocks/schema-diff';
5
+ import { storeOf } from '@itrocks/store';
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 ReflectToTable().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 TableDiff(mysqlTable, classTable);
64
+ console.dir(tableSchemaDiff, { depth: null });
44
65
  return false;
45
66
  }
46
67
  }
package/package.json CHANGED
@@ -6,8 +6,13 @@
6
6
  "dependencies": {
7
7
  "@itrocks/class-type": "latest",
8
8
  "@itrocks/mysql": "latest",
9
+ "@itrocks/mysql-to-schema": "latest",
9
10
  "@itrocks/reflect": "latest",
10
- "@itrocks/storage": "latest"
11
+ "@itrocks/reflect-to-schema": "latest",
12
+ "@itrocks/schema-diff": "latest",
13
+ "@itrocks/storage": "latest",
14
+ "@itrocks/store": "latest",
15
+ "mariadb": "^3.4"
11
16
  },
12
17
  "description": "Reactively maintains database structure by updating schema and retrying on MySQL errors",
13
18
  "devDependencies": {
@@ -48,5 +53,5 @@
48
53
  "build:esm": "tsc -p tsconfig.esm.json"
49
54
  },
50
55
  "types": "./esm/mysql-maintainer.d.ts",
51
- "version": "0.0.3"
56
+ "version": "0.0.5"
52
57
  }