@itrocks/mysql-maintainer 0.0.14 → 0.0.15

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.
@@ -10,7 +10,8 @@ export declare class MysqlMaintainer {
10
10
  constructor(connection: Connection);
11
11
  createImplicitTable(type1: ObjectOrType, type2: ObjectOrType): Promise<boolean>;
12
12
  createTable(type: Type): Promise<boolean>;
13
+ implicitTableName(type1: ObjectOrType, type2: ObjectOrType): string;
13
14
  manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): Promise<boolean>;
14
- updateContextTables(context: Context): Promise<boolean>;
15
+ updateContextTables(context: Context, tableName?: string): Promise<boolean>;
15
16
  updateTable(type: Type): Promise<boolean>;
16
17
  }
@@ -31,12 +31,9 @@ class MysqlMaintainer {
31
31
  this.connection = connection;
32
32
  }
33
33
  async createImplicitTable(type1, type2) {
34
+ const joinTable = this.implicitTableName(type1, type2);
34
35
  const table1 = (0, store_1.storeOf)(type1);
35
36
  const table2 = (0, store_1.storeOf)(type2);
36
- if (!table1 || !table2) {
37
- throw 'Collection objects are not stored';
38
- }
39
- const joinTable = (0, mysql_1.joinTableName)(table1, table2);
40
37
  const query = `CREATE TABLE \`${joinTable}\` (
41
38
  ${table1}_id int unsigned NOT NULL,
42
39
  ${table2}_id int unsigned NOT NULL,
@@ -58,17 +55,26 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
58
55
  await this.connection.query(sql);
59
56
  return true;
60
57
  }
58
+ implicitTableName(type1, type2) {
59
+ const table1 = (0, store_1.storeOf)(type1);
60
+ const table2 = (0, store_1.storeOf)(type2);
61
+ if (!table1 || !table2) {
62
+ throw 'Collection objects are not stored';
63
+ }
64
+ return (0, mysql_1.joinTableName)(table1, table2);
65
+ }
61
66
  async manageError(error, context, sql, values) {
62
67
  switch (error.code) {
63
68
  case 'ER_BAD_FIELD_ERROR':
64
69
  case 'ER_CANNOT_ADD_FOREIGN':
65
70
  return await this.updateContextTables(context);
66
71
  case 'ER_NO_SUCH_TABLE':
67
- return this.updateContextTables(context);
72
+ const tableName = /Table '.*?\.(.*?)'/.exec(error.message)?.[1];
73
+ return this.updateContextTables(context, tableName);
68
74
  }
69
75
  return false;
70
76
  }
71
- async updateContextTables(context) {
77
+ async updateContextTables(context, tableName) {
72
78
  const contexts = Array.isArray(context) ? context : [context];
73
79
  for (const context of contexts) {
74
80
  const type = (0, class_type_1.typeOf)(context);
@@ -77,9 +83,9 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
77
83
  throw 'No table name for type';
78
84
  }
79
85
  const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
80
- exists.length ? await this.updateTable(type) : await this.createTable(type);
86
+ await (exists.length ? this.updateTable(type) : this.createTable(type));
81
87
  }
82
- if (contexts.length === 2) {
88
+ if ((contexts.length === 2) && (this.implicitTableName(contexts[0], contexts[1]) === tableName)) {
83
89
  await this.createImplicitTable(contexts[0], contexts[1]);
84
90
  }
85
91
  return true;
@@ -10,7 +10,8 @@ export declare class MysqlMaintainer {
10
10
  constructor(connection: Connection);
11
11
  createImplicitTable(type1: ObjectOrType, type2: ObjectOrType): Promise<boolean>;
12
12
  createTable(type: Type): Promise<boolean>;
13
+ implicitTableName(type1: ObjectOrType, type2: ObjectOrType): string;
13
14
  manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): Promise<boolean>;
14
- updateContextTables(context: Context): Promise<boolean>;
15
+ updateContextTables(context: Context, tableName?: string): Promise<boolean>;
15
16
  updateTable(type: Type): Promise<boolean>;
16
17
  }
@@ -14,12 +14,9 @@ export class MysqlMaintainer {
14
14
  this.connection = connection;
15
15
  }
16
16
  async createImplicitTable(type1, type2) {
17
+ const joinTable = this.implicitTableName(type1, type2);
17
18
  const table1 = storeOf(type1);
18
19
  const table2 = storeOf(type2);
19
- if (!table1 || !table2) {
20
- throw 'Collection objects are not stored';
21
- }
22
- const joinTable = joinTableName(table1, table2);
23
20
  const query = `CREATE TABLE \`${joinTable}\` (
24
21
  ${table1}_id int unsigned NOT NULL,
25
22
  ${table2}_id int unsigned NOT NULL,
@@ -41,17 +38,26 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
41
38
  await this.connection.query(sql);
42
39
  return true;
43
40
  }
41
+ implicitTableName(type1, type2) {
42
+ const table1 = storeOf(type1);
43
+ const table2 = storeOf(type2);
44
+ if (!table1 || !table2) {
45
+ throw 'Collection objects are not stored';
46
+ }
47
+ return joinTableName(table1, table2);
48
+ }
44
49
  async manageError(error, context, sql, values) {
45
50
  switch (error.code) {
46
51
  case 'ER_BAD_FIELD_ERROR':
47
52
  case 'ER_CANNOT_ADD_FOREIGN':
48
53
  return await this.updateContextTables(context);
49
54
  case 'ER_NO_SUCH_TABLE':
50
- return this.updateContextTables(context);
55
+ const tableName = /Table '.*?\.(.*?)'/.exec(error.message)?.[1];
56
+ return this.updateContextTables(context, tableName);
51
57
  }
52
58
  return false;
53
59
  }
54
- async updateContextTables(context) {
60
+ async updateContextTables(context, tableName) {
55
61
  const contexts = Array.isArray(context) ? context : [context];
56
62
  for (const context of contexts) {
57
63
  const type = typeOf(context);
@@ -60,9 +66,9 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
60
66
  throw 'No table name for type';
61
67
  }
62
68
  const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
63
- exists.length ? await this.updateTable(type) : await this.createTable(type);
69
+ await (exists.length ? this.updateTable(type) : this.createTable(type));
64
70
  }
65
- if (contexts.length === 2) {
71
+ if ((contexts.length === 2) && (this.implicitTableName(contexts[0], contexts[1]) === tableName)) {
66
72
  await this.createImplicitTable(contexts[0], contexts[1]);
67
73
  }
68
74
  return true;
package/package.json CHANGED
@@ -54,5 +54,5 @@
54
54
  "build:esm": "tsc -p tsconfig.esm.json"
55
55
  },
56
56
  "types": "./esm/mysql-maintainer.d.ts",
57
- "version": "0.0.14"
57
+ "version": "0.0.15"
58
58
  }