@itrocks/mysql-maintainer 0.0.6 → 0.0.8
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/cjs/contextual-connection.js +2 -1
- package/cjs/mysql-maintainer.d.ts +1 -2
- package/cjs/mysql-maintainer.js +27 -23
- package/cjs/mysql.js +2 -2
- package/esm/contextual-connection.js +2 -1
- package/esm/mysql-maintainer.d.ts +1 -2
- package/esm/mysql-maintainer.js +27 -23
- package/esm/mysql.js +2 -2
- package/package.json +1 -1
|
@@ -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
|
-
console.log('class table before normalize:');
|
|
72
79
|
const classTable = new reflect_to_schema_1.ReflectToTable().convert(type);
|
|
73
|
-
console.dir(classTable, { depth: null });
|
|
74
|
-
console.log('class table after normalize:');
|
|
75
80
|
new mysql_to_schema_1.MysqlToTable(this.connection).normalize(classTable);
|
|
76
|
-
console.dir(classTable, { depth: null });
|
|
77
|
-
console.log('mysql table:');
|
|
78
81
|
const mysqlTable = await ((new mysql_to_schema_1.MysqlToTable(this.connection)).convert(tableName));
|
|
79
|
-
console.dir(mysqlTable, { depth: null });
|
|
80
|
-
console.log('table diff:');
|
|
81
82
|
const schemaDiff = new schema_diff_1.TableDiff(mysqlTable, classTable);
|
|
82
|
-
console.dir(schemaDiff, { depth: null });
|
|
83
|
+
console.dir(schemaDiff.additions, { depth: null });
|
|
84
|
+
console.dir(schemaDiff.changes, { depth: null });
|
|
85
|
+
console.dir(schemaDiff.deletions, { depth: null });
|
|
83
86
|
const schemaDiffMysql = new schema_diff_mysql_1.SchemaDiffMysql();
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
const sql = schemaDiffMysql.sql(schemaDiff, true);
|
|
88
|
+
await this.connection.query(sql);
|
|
89
|
+
return true;
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
exports.MysqlMaintainer = MysqlMaintainer;
|
package/cjs/mysql.js
CHANGED
|
@@ -43,14 +43,14 @@ class Mysql extends mysql_1.Mysql {
|
|
|
43
43
|
context.pop();
|
|
44
44
|
return result;
|
|
45
45
|
}
|
|
46
|
-
async readCollection(object, property, type = new reflect_1.ReflectProperty(object, property).collectionType.elementType) {
|
|
46
|
+
async readCollection(object, property, type = new reflect_1.ReflectProperty(object, property).collectionType.elementType.type) {
|
|
47
47
|
const context = (this.connection ?? await this.connect()).context;
|
|
48
48
|
context.push([object, type]);
|
|
49
49
|
const result = await super.readCollection(object, property, type);
|
|
50
50
|
context.pop();
|
|
51
51
|
return result;
|
|
52
52
|
}
|
|
53
|
-
async readCollectionIds(object, property, type = new reflect_1.ReflectProperty(object, property).collectionType.elementType) {
|
|
53
|
+
async readCollectionIds(object, property, type = new reflect_1.ReflectProperty(object, property).collectionType.elementType.type) {
|
|
54
54
|
const context = (this.connection ?? await this.connect()).context;
|
|
55
55
|
context.push([object, type]);
|
|
56
56
|
const result = await super.readCollectionIds(object, property, type);
|
|
@@ -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
|
-
console.log('class table before normalize:');
|
|
55
62
|
const classTable = new ReflectToTable().convert(type);
|
|
56
|
-
console.dir(classTable, { depth: null });
|
|
57
|
-
console.log('class table after normalize:');
|
|
58
63
|
new MysqlToTable(this.connection).normalize(classTable);
|
|
59
|
-
console.dir(classTable, { depth: null });
|
|
60
|
-
console.log('mysql table:');
|
|
61
64
|
const mysqlTable = await ((new MysqlToTable(this.connection)).convert(tableName));
|
|
62
|
-
console.dir(mysqlTable, { depth: null });
|
|
63
|
-
console.log('table diff:');
|
|
64
65
|
const schemaDiff = new TableDiff(mysqlTable, classTable);
|
|
65
|
-
console.dir(schemaDiff, { depth: null });
|
|
66
|
+
console.dir(schemaDiff.additions, { depth: null });
|
|
67
|
+
console.dir(schemaDiff.changes, { depth: null });
|
|
68
|
+
console.dir(schemaDiff.deletions, { depth: null });
|
|
66
69
|
const schemaDiffMysql = new SchemaDiffMysql();
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
const sql = schemaDiffMysql.sql(schemaDiff, true);
|
|
71
|
+
await this.connection.query(sql);
|
|
72
|
+
return true;
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
//# sourceMappingURL=mysql-maintainer.js.map
|
package/esm/mysql.js
CHANGED
|
@@ -40,14 +40,14 @@ export class Mysql extends M {
|
|
|
40
40
|
context.pop();
|
|
41
41
|
return result;
|
|
42
42
|
}
|
|
43
|
-
async readCollection(object, property, type = new ReflectProperty(object, property).collectionType.elementType) {
|
|
43
|
+
async readCollection(object, property, type = new ReflectProperty(object, property).collectionType.elementType.type) {
|
|
44
44
|
const context = (this.connection ?? await this.connect()).context;
|
|
45
45
|
context.push([object, type]);
|
|
46
46
|
const result = await super.readCollection(object, property, type);
|
|
47
47
|
context.pop();
|
|
48
48
|
return result;
|
|
49
49
|
}
|
|
50
|
-
async readCollectionIds(object, property, type = new ReflectProperty(object, property).collectionType.elementType) {
|
|
50
|
+
async readCollectionIds(object, property, type = new ReflectProperty(object, property).collectionType.elementType.type) {
|
|
51
51
|
const context = (this.connection ?? await this.connect()).context;
|
|
52
52
|
context.push([object, type]);
|
|
53
53
|
const result = await super.readCollectionIds(object, property, type);
|
package/package.json
CHANGED