@itrocks/mysql-maintainer
Version:
Reactively maintains database structure by updating schema and retrying on MySQL errors
34 lines • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Contextual = void 0;
const mariadb_1 = require("mariadb");
const mysql_maintainer_1 = require("./mysql-maintainer");
class Contextual {
context = [];
superQuery = () => new Promise(() => { });
async applyTo(connection) {
connection.context = [];
connection.superQuery = connection.query;
connection.query = Contextual.prototype.query;
return connection;
}
async query(sql, values) {
try {
return await this.superQuery(sql, values);
}
catch (error) {
if (!(error instanceof mariadb_1.SqlError)
|| !error.code
|| !['ER_BAD_FIELD_ERROR'].includes(error.code)) {
throw error;
}
// @ts-ignore query applies to a Connection
if (await new mysql_maintainer_1.MysqlMaintainer(this).manageError(error, this.context[this.context.length - 1], sql, values)) {
return this.query(sql, values);
}
throw error;
}
}
}
exports.Contextual = Contextual;
//# sourceMappingURL=contextual-connection.js.map