east-mysql2
Version:
mysql2 adapter for east
85 lines (84 loc) • 4.86 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Adapter = void 0;
const path_1 = __importDefault(require("path"));
const sqlstring_1 = __importDefault(require("sqlstring"));
const promise_1 = require("mysql2/promise");
class Adapter {
constructor(params) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
params = params || {};
this._mysqlParams = {};
this._mysqlParams.host = (_b = (_a = params.mysql) === null || _a === void 0 ? void 0 : _a.host) !== null && _b !== void 0 ? _b : process.env.MYSQL_HOST;
this._mysqlParams.port = (_d = (_c = params.mysql) === null || _c === void 0 ? void 0 : _c.port) !== null && _d !== void 0 ? _d : this.toNumber(process.env.MYSQL_PORT);
this._mysqlParams.user = (_f = (_e = params.mysql) === null || _e === void 0 ? void 0 : _e.user) !== null && _f !== void 0 ? _f : process.env.MYSQL_USER;
this._mysqlParams.password = (_h = (_g = params.mysql) === null || _g === void 0 ? void 0 : _g.password) !== null && _h !== void 0 ? _h : process.env.MYSQL_PASSWORD;
this._mysqlParams.migrationDatabase = (_k = (_j = params.mysql) === null || _j === void 0 ? void 0 : _j.migrationDatabase) !== null && _k !== void 0 ? _k : '_migrations';
this._mysqlParams.migrationTable = (_m = (_l = params.mysql) === null || _l === void 0 ? void 0 : _l.migrationTable) !== null && _m !== void 0 ? _m : '_migrations';
this._mysqlParams.createDbOnConnect = (_p = (_o = params.mysql) === null || _o === void 0 ? void 0 : _o.createDbOnConnect) !== null && _p !== void 0 ? _p : true;
}
async connect() {
this._connection = await (0, promise_1.createConnection)({
host: this._mysqlParams.host,
port: this._mysqlParams.port,
user: this._mysqlParams.user,
password: this._mysqlParams.password
});
if (this._mysqlParams.createDbOnConnect) {
// maybe user doesn't have create DB permissions: as this is default, check first
const [results] = await this._connection.execute('SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?', [this._mysqlParams.migrationDatabase]);
if (results.length == 0) {
// create the database
await this._connection.query(`CREATE DATABASE ${sqlstring_1.default.escapeId(this._mysqlParams.migrationDatabase)}`);
}
}
await this.changeToMigrationDatabase();
await this._connection.query('CREATE TABLE IF NOT EXISTS ' + sqlstring_1.default.escapeId(this._mysqlParams.migrationTable) + ' (`name` VARCHAR(255) NOT NULL, PRIMARY KEY (`name`))');
return { db: this._connection };
}
async disconnect() {
var _a;
await ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.end());
}
getTemplatePath(sourceMigrationExtension) {
if (['js', 'ts'].includes(sourceMigrationExtension)) {
return path_1.default.resolve(__dirname, `../templates/migration.${sourceMigrationExtension}`);
}
throw new Error(`Adapter doesn't provide template ".${sourceMigrationExtension}" source files, please specify template in configuration`);
}
async getExecutedMigrationNames() {
if (this._connection == null)
return [];
await this.changeToMigrationDatabase();
const [results] = await this._connection.query(`SELECT name FROM ${sqlstring_1.default.escapeId(this._mysqlParams.migrationTable)}`);
return results.map(r => r.name);
}
async markExecuted(migrationName) {
if (this._connection == null) {
throw new Error(`No connection; ensure connect is run before calling this method`);
}
await this.changeToMigrationDatabase();
await this._connection.execute(`INSERT INTO ${sqlstring_1.default.escapeId(this._mysqlParams.migrationTable)} VALUES (?)`, [migrationName]);
}
async unmarkExecuted(migrationName) {
if (this._connection == null) {
throw new Error(`No connection; ensure connect is run before calling this method`);
}
await this.changeToMigrationDatabase();
await this._connection.execute(`DELETE FROM ${sqlstring_1.default.escapeId(this._mysqlParams.migrationTable)} WHERE name = ?`, [migrationName]);
}
async changeToMigrationDatabase() {
await this._connection.changeUser({ database: this._mysqlParams.migrationDatabase });
}
toNumber(port) {
if (port == null)
return undefined;
return parseInt(port);
}
}
exports.Adapter = Adapter;
const _ = Adapter;
exports.default = Adapter;