@itrocks/mysql-maintainer
Version:
Reactively maintains database structure by updating schema and retrying on MySQL errors
89 lines • 3.69 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MysqlMaintainer = void 0;
const class_type_1 = require("@itrocks/class-type");
const mysql_to_schema_1 = require("@itrocks/mysql-to-schema");
const reflect_to_schema_1 = require("@itrocks/reflect-to-schema");
const schema_diff_1 = require("@itrocks/schema-diff");
const schema_diff_mysql_1 = require("@itrocks/schema-diff-mysql");
const store_1 = require("@itrocks/store");
__exportStar(require("./mysql"), exports);
class MysqlMaintainer {
connection;
constructor(connection) {
this.connection = connection;
}
createContextTables(context) {
const contexts = Array.isArray(context) ? context : [context];
for (const context of contexts) {
this.createTable((0, class_type_1.typeOf)(context));
}
return false;
}
createImplicitTables(sql) {
return false;
}
createTable(type) {
return false;
}
async manageError(error, context, sql, values) {
console.log('query', sql, values);
console.log('throw', error);
console.log('context', context);
switch (error.code) {
case 'ER_BAD_FIELD_ERROR':
case 'ER_CANNOT_ADD_FOREIGN':
return await this.updateContextTables(context);
case 'ER_CANT_CREATE_TABLE':
return this.createImplicitTables(sql);
case 'ER_NO_SUCH_TABLE':
return this.createContextTables(context);
}
return false;
}
async updateContextTables(context) {
const contexts = Array.isArray(context) ? context : [context];
for (const context of contexts) {
await this.updateTable((0, class_type_1.typeOf)(context));
}
return false;
}
async updateTable(type) {
const tableName = (0, store_1.storeOf)(type);
if (!tableName) {
throw 'No table name for type';
}
console.log('##### UPDATE TABLE');
console.log('class table before normalize:');
const classTable = new reflect_to_schema_1.ReflectToTable().convert(type);
console.dir(classTable, { depth: null });
console.log('class table after normalize:');
new mysql_to_schema_1.MysqlToTable(this.connection).normalize(classTable);
console.dir(classTable, { depth: null });
console.log('mysql table:');
const mysqlTable = await ((new mysql_to_schema_1.MysqlToTable(this.connection)).convert(tableName));
console.dir(mysqlTable, { depth: null });
console.log('table diff:');
const schemaDiff = new schema_diff_1.TableDiff(mysqlTable, classTable);
console.dir(schemaDiff, { depth: null });
const schemaDiffMysql = new schema_diff_mysql_1.SchemaDiffMysql();
console.log(schemaDiffMysql.sql(schemaDiff, true));
return false;
}
}
exports.MysqlMaintainer = MysqlMaintainer;
//# sourceMappingURL=mysql-maintainer.js.map