@itrocks/mysql-maintainer 0.0.7 → 0.0.9
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.
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Contextual = void 0;
|
|
4
4
|
const mariadb_1 = require("mariadb");
|
|
5
5
|
const mysql_maintainer_1 = require("./mysql-maintainer");
|
|
6
|
+
const MANAGED_ERROR_CODES = ['ER_BAD_FIELD_ERROR', 'ER_CANNOT_ADD_FOREIGN', 'ER_CANT_CREATE_TABLE', 'ER_NO_SUCH_TABLE'];
|
|
6
7
|
class Contextual {
|
|
7
8
|
context = [];
|
|
8
9
|
superQuery = () => new Promise(() => { });
|
|
@@ -19,7 +20,7 @@ class Contextual {
|
|
|
19
20
|
catch (error) {
|
|
20
21
|
if (!(error instanceof mariadb_1.SqlError)
|
|
21
22
|
|| !error.code
|
|
22
|
-
|| !
|
|
23
|
+
|| !MANAGED_ERROR_CODES.includes(error.code)) {
|
|
23
24
|
throw error;
|
|
24
25
|
}
|
|
25
26
|
// @ts-ignore query applies to a Connection
|
|
@@ -7,9 +7,8 @@ export * from './mysql';
|
|
|
7
7
|
export declare class MysqlMaintainer {
|
|
8
8
|
connection: Connection;
|
|
9
9
|
constructor(connection: Connection);
|
|
10
|
-
createContextTables(context: Context): boolean;
|
|
11
10
|
createImplicitTables(sql: string | QueryOptions): boolean;
|
|
12
|
-
createTable(type: Type): boolean
|
|
11
|
+
createTable(type: Type): Promise<boolean>;
|
|
13
12
|
manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): Promise<boolean>;
|
|
14
13
|
updateContextTables(context: Context): Promise<boolean>;
|
|
15
14
|
updateTable(type: Type): Promise<boolean>;
|
package/cjs/mysql-maintainer.js
CHANGED
|
@@ -20,6 +20,7 @@ const mysql_to_schema_1 = require("@itrocks/mysql-to-schema");
|
|
|
20
20
|
const reflect_to_schema_1 = require("@itrocks/reflect-to-schema");
|
|
21
21
|
const schema_diff_1 = require("@itrocks/schema-diff");
|
|
22
22
|
const schema_diff_mysql_1 = require("@itrocks/schema-diff-mysql");
|
|
23
|
+
const schema_to_mysql_1 = require("@itrocks/schema-to-mysql");
|
|
23
24
|
const store_1 = require("@itrocks/store");
|
|
24
25
|
__exportStar(require("./mysql"), exports);
|
|
25
26
|
class MysqlMaintainer {
|
|
@@ -27,18 +28,20 @@ class MysqlMaintainer {
|
|
|
27
28
|
constructor(connection) {
|
|
28
29
|
this.connection = connection;
|
|
29
30
|
}
|
|
30
|
-
createContextTables(context) {
|
|
31
|
-
const contexts = Array.isArray(context) ? context : [context];
|
|
32
|
-
for (const context of contexts) {
|
|
33
|
-
this.createTable((0, class_type_1.typeOf)(context));
|
|
34
|
-
}
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
31
|
createImplicitTables(sql) {
|
|
38
32
|
return false;
|
|
39
33
|
}
|
|
40
|
-
createTable(type) {
|
|
41
|
-
|
|
34
|
+
async createTable(type) {
|
|
35
|
+
const tableName = (0, store_1.storeOf)(type);
|
|
36
|
+
if (!tableName) {
|
|
37
|
+
throw 'No table name for type';
|
|
38
|
+
}
|
|
39
|
+
const tableSchema = new reflect_to_schema_1.ReflectToTable().convert(type);
|
|
40
|
+
const schemaToMysql = new schema_to_mysql_1.SchemaToMysql();
|
|
41
|
+
const sql = schemaToMysql.sql(tableSchema);
|
|
42
|
+
console.log(sql);
|
|
43
|
+
await this.connection.query(sql);
|
|
44
|
+
return true;
|
|
42
45
|
}
|
|
43
46
|
async manageError(error, context, sql, values) {
|
|
44
47
|
console.log('query', sql, values);
|
|
@@ -51,38 +54,39 @@ class MysqlMaintainer {
|
|
|
51
54
|
case 'ER_CANT_CREATE_TABLE':
|
|
52
55
|
return this.createImplicitTables(sql);
|
|
53
56
|
case 'ER_NO_SUCH_TABLE':
|
|
54
|
-
return this.
|
|
57
|
+
return this.updateContextTables(context);
|
|
55
58
|
}
|
|
56
59
|
return false;
|
|
57
60
|
}
|
|
58
61
|
async updateContextTables(context) {
|
|
59
62
|
const contexts = Array.isArray(context) ? context : [context];
|
|
60
63
|
for (const context of contexts) {
|
|
61
|
-
|
|
64
|
+
const type = (0, class_type_1.typeOf)(context);
|
|
65
|
+
const tableName = (0, store_1.storeOf)(type);
|
|
66
|
+
if (!tableName) {
|
|
67
|
+
throw 'No table name for type';
|
|
68
|
+
}
|
|
69
|
+
const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
|
|
70
|
+
await (exists.length ? this.updateTable(type) : this.createTable(type));
|
|
62
71
|
}
|
|
63
|
-
return
|
|
72
|
+
return true;
|
|
64
73
|
}
|
|
65
74
|
async updateTable(type) {
|
|
66
75
|
const tableName = (0, store_1.storeOf)(type);
|
|
67
76
|
if (!tableName) {
|
|
68
77
|
throw 'No table name for type';
|
|
69
78
|
}
|
|
70
|
-
console.log('##### UPDATE TABLE');
|
|
71
79
|
const classTable = new reflect_to_schema_1.ReflectToTable().convert(type);
|
|
72
80
|
new mysql_to_schema_1.MysqlToTable(this.connection).normalize(classTable);
|
|
73
81
|
const mysqlTable = await ((new mysql_to_schema_1.MysqlToTable(this.connection)).convert(tableName));
|
|
74
82
|
const schemaDiff = new schema_diff_1.TableDiff(mysqlTable, classTable);
|
|
75
|
-
console.log('##### Additions:');
|
|
76
83
|
console.dir(schemaDiff.additions, { depth: null });
|
|
77
|
-
console.log('##### Changes:');
|
|
78
84
|
console.dir(schemaDiff.changes, { depth: null });
|
|
79
|
-
console.log('##### Deletions:');
|
|
80
85
|
console.dir(schemaDiff.deletions, { depth: null });
|
|
81
86
|
const schemaDiffMysql = new schema_diff_mysql_1.SchemaDiffMysql();
|
|
82
87
|
const sql = schemaDiffMysql.sql(schemaDiff, true);
|
|
83
|
-
console.log(sql);
|
|
84
88
|
await this.connection.query(sql);
|
|
85
|
-
return
|
|
89
|
+
return true;
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
exports.MysqlMaintainer = MysqlMaintainer;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SqlError } from 'mariadb';
|
|
2
2
|
import { MysqlMaintainer } from './mysql-maintainer';
|
|
3
|
+
const MANAGED_ERROR_CODES = ['ER_BAD_FIELD_ERROR', 'ER_CANNOT_ADD_FOREIGN', 'ER_CANT_CREATE_TABLE', 'ER_NO_SUCH_TABLE'];
|
|
3
4
|
export class Contextual {
|
|
4
5
|
context = [];
|
|
5
6
|
superQuery = () => new Promise(() => { });
|
|
@@ -16,7 +17,7 @@ export class Contextual {
|
|
|
16
17
|
catch (error) {
|
|
17
18
|
if (!(error instanceof SqlError)
|
|
18
19
|
|| !error.code
|
|
19
|
-
|| !
|
|
20
|
+
|| !MANAGED_ERROR_CODES.includes(error.code)) {
|
|
20
21
|
throw error;
|
|
21
22
|
}
|
|
22
23
|
// @ts-ignore query applies to a Connection
|
|
@@ -7,9 +7,8 @@ export * from './mysql';
|
|
|
7
7
|
export declare class MysqlMaintainer {
|
|
8
8
|
connection: Connection;
|
|
9
9
|
constructor(connection: Connection);
|
|
10
|
-
createContextTables(context: Context): boolean;
|
|
11
10
|
createImplicitTables(sql: string | QueryOptions): boolean;
|
|
12
|
-
createTable(type: Type): boolean
|
|
11
|
+
createTable(type: Type): Promise<boolean>;
|
|
13
12
|
manageError(error: SqlError, context: Context, sql: string | QueryOptions, values: any[]): Promise<boolean>;
|
|
14
13
|
updateContextTables(context: Context): Promise<boolean>;
|
|
15
14
|
updateTable(type: Type): Promise<boolean>;
|
package/esm/mysql-maintainer.js
CHANGED
|
@@ -3,6 +3,7 @@ import { MysqlToTable } from '@itrocks/mysql-to-schema';
|
|
|
3
3
|
import { ReflectToTable } from '@itrocks/reflect-to-schema';
|
|
4
4
|
import { TableDiff } from '@itrocks/schema-diff';
|
|
5
5
|
import { SchemaDiffMysql } from '@itrocks/schema-diff-mysql';
|
|
6
|
+
import { SchemaToMysql } from '@itrocks/schema-to-mysql';
|
|
6
7
|
import { storeOf } from '@itrocks/store';
|
|
7
8
|
export * from './mysql';
|
|
8
9
|
export class MysqlMaintainer {
|
|
@@ -10,18 +11,20 @@ export class MysqlMaintainer {
|
|
|
10
11
|
constructor(connection) {
|
|
11
12
|
this.connection = connection;
|
|
12
13
|
}
|
|
13
|
-
createContextTables(context) {
|
|
14
|
-
const contexts = Array.isArray(context) ? context : [context];
|
|
15
|
-
for (const context of contexts) {
|
|
16
|
-
this.createTable(typeOf(context));
|
|
17
|
-
}
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
14
|
createImplicitTables(sql) {
|
|
21
15
|
return false;
|
|
22
16
|
}
|
|
23
|
-
createTable(type) {
|
|
24
|
-
|
|
17
|
+
async createTable(type) {
|
|
18
|
+
const tableName = storeOf(type);
|
|
19
|
+
if (!tableName) {
|
|
20
|
+
throw 'No table name for type';
|
|
21
|
+
}
|
|
22
|
+
const tableSchema = new ReflectToTable().convert(type);
|
|
23
|
+
const schemaToMysql = new SchemaToMysql();
|
|
24
|
+
const sql = schemaToMysql.sql(tableSchema);
|
|
25
|
+
console.log(sql);
|
|
26
|
+
await this.connection.query(sql);
|
|
27
|
+
return true;
|
|
25
28
|
}
|
|
26
29
|
async manageError(error, context, sql, values) {
|
|
27
30
|
console.log('query', sql, values);
|
|
@@ -34,38 +37,39 @@ export class MysqlMaintainer {
|
|
|
34
37
|
case 'ER_CANT_CREATE_TABLE':
|
|
35
38
|
return this.createImplicitTables(sql);
|
|
36
39
|
case 'ER_NO_SUCH_TABLE':
|
|
37
|
-
return this.
|
|
40
|
+
return this.updateContextTables(context);
|
|
38
41
|
}
|
|
39
42
|
return false;
|
|
40
43
|
}
|
|
41
44
|
async updateContextTables(context) {
|
|
42
45
|
const contexts = Array.isArray(context) ? context : [context];
|
|
43
46
|
for (const context of contexts) {
|
|
44
|
-
|
|
47
|
+
const type = typeOf(context);
|
|
48
|
+
const tableName = storeOf(type);
|
|
49
|
+
if (!tableName) {
|
|
50
|
+
throw 'No table name for type';
|
|
51
|
+
}
|
|
52
|
+
const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
|
|
53
|
+
await (exists.length ? this.updateTable(type) : this.createTable(type));
|
|
45
54
|
}
|
|
46
|
-
return
|
|
55
|
+
return true;
|
|
47
56
|
}
|
|
48
57
|
async updateTable(type) {
|
|
49
58
|
const tableName = storeOf(type);
|
|
50
59
|
if (!tableName) {
|
|
51
60
|
throw 'No table name for type';
|
|
52
61
|
}
|
|
53
|
-
console.log('##### UPDATE TABLE');
|
|
54
62
|
const classTable = new ReflectToTable().convert(type);
|
|
55
63
|
new MysqlToTable(this.connection).normalize(classTable);
|
|
56
64
|
const mysqlTable = await ((new MysqlToTable(this.connection)).convert(tableName));
|
|
57
65
|
const schemaDiff = new TableDiff(mysqlTable, classTable);
|
|
58
|
-
console.log('##### Additions:');
|
|
59
66
|
console.dir(schemaDiff.additions, { depth: null });
|
|
60
|
-
console.log('##### Changes:');
|
|
61
67
|
console.dir(schemaDiff.changes, { depth: null });
|
|
62
|
-
console.log('##### Deletions:');
|
|
63
68
|
console.dir(schemaDiff.deletions, { depth: null });
|
|
64
69
|
const schemaDiffMysql = new SchemaDiffMysql();
|
|
65
70
|
const sql = schemaDiffMysql.sql(schemaDiff, true);
|
|
66
|
-
console.log(sql);
|
|
67
71
|
await this.connection.query(sql);
|
|
68
|
-
return
|
|
72
|
+
return true;
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
//# sourceMappingURL=mysql-maintainer.js.map
|
package/package.json
CHANGED