2022-03-02 16:27:35 +03:00
|
|
|
module.exports = class DatabaseInfo {
|
2022-01-18 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* @param {import('knex')} knex
|
|
|
|
*/
|
|
|
|
constructor(knex) {
|
2022-03-01 10:37:06 +03:00
|
|
|
this._knex = knex;
|
2022-03-01 11:04:05 +03:00
|
|
|
this._client = this._knex.client;
|
|
|
|
this._driver = this._client.config.client;
|
2022-01-18 12:03:21 +03:00
|
|
|
|
2022-03-01 10:37:06 +03:00
|
|
|
this._databaseDetails = {
|
2022-01-18 12:03:21 +03:00
|
|
|
// The underlying driver that `knex` uses
|
|
|
|
// ie. `sqlite3`, `mysql` or `mysql2`
|
2022-03-01 11:04:05 +03:00
|
|
|
driver: this._driver,
|
2022-01-18 12:03:21 +03:00
|
|
|
|
|
|
|
// A capitalized version of the specific database used
|
|
|
|
database: 'unknown',
|
|
|
|
|
|
|
|
// A slugified version of the `database`
|
|
|
|
engine: 'unknown',
|
|
|
|
|
|
|
|
// The version of the database used
|
|
|
|
version: 'unknown'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2022-03-01 10:37:06 +03:00
|
|
|
switch (this._driver) {
|
2022-01-18 12:03:21 +03:00
|
|
|
case 'sqlite3':
|
2022-03-01 10:37:06 +03:00
|
|
|
this._databaseDetails.database = 'SQLite';
|
|
|
|
this._databaseDetails.engine = 'sqlite3';
|
|
|
|
this._databaseDetails.version = this._client.driver.VERSION;
|
2022-01-18 12:03:21 +03:00
|
|
|
break;
|
|
|
|
case 'mysql':
|
|
|
|
case 'mysql2':
|
|
|
|
try {
|
2022-03-01 10:37:06 +03:00
|
|
|
const version = await this._knex.raw('SELECT version() as version;');
|
2022-01-18 12:03:21 +03:00
|
|
|
const mysqlVersion = version[0][0].version;
|
|
|
|
|
|
|
|
if (mysqlVersion.includes('MariaDB')) {
|
2022-03-01 10:37:06 +03:00
|
|
|
this._databaseDetails.database = 'MariaDB';
|
|
|
|
this._databaseDetails.engine = 'mariadb';
|
|
|
|
this._databaseDetails.version = mysqlVersion.split('-')[0];
|
2022-01-18 12:03:21 +03:00
|
|
|
} else {
|
2022-03-01 10:37:06 +03:00
|
|
|
this._databaseDetails.database = 'MySQL';
|
2022-01-18 12:03:21 +03:00
|
|
|
|
|
|
|
if (mysqlVersion.startsWith('5')) {
|
2022-03-01 10:37:06 +03:00
|
|
|
this._databaseDetails.engine = 'mysql5';
|
2022-01-18 12:03:21 +03:00
|
|
|
} else if (mysqlVersion.startsWith('8')) {
|
2022-03-01 10:37:06 +03:00
|
|
|
this._databaseDetails.engine = 'mysql8';
|
2022-01-18 12:03:21 +03:00
|
|
|
} else {
|
2022-03-01 10:37:06 +03:00
|
|
|
this._databaseDetails.engine = 'mysql';
|
2022-01-18 12:03:21 +03:00
|
|
|
}
|
|
|
|
|
2022-03-01 10:37:06 +03:00
|
|
|
this._databaseDetails.version = mysqlVersion;
|
2022-01-18 12:03:21 +03:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-03-01 10:37:06 +03:00
|
|
|
return this._databaseDetails;
|
2022-01-18 12:03:21 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// This driver isn't supported so we should just leave the return
|
|
|
|
// object alone with the "unknown" strings
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-01 10:37:06 +03:00
|
|
|
return this._databaseDetails;
|
2022-01-18 12:03:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getDriver() {
|
2022-03-01 10:37:06 +03:00
|
|
|
return this._databaseDetails.driver;
|
2022-01-18 12:03:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getDatabase() {
|
2022-03-01 10:37:06 +03:00
|
|
|
return this._databaseDetails.database;
|
2022-01-18 12:03:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getEngine() {
|
2022-03-01 10:37:06 +03:00
|
|
|
return this._databaseDetails.engine;
|
2022-01-18 12:03:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getVersion() {
|
2022-03-01 10:37:06 +03:00
|
|
|
return this._databaseDetails.version;
|
2022-01-18 12:03:21 +03:00
|
|
|
}
|
2022-03-02 16:27:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the driver used is for SQLite
|
|
|
|
*
|
|
|
|
* @param {import('knex')} knex
|
|
|
|
*/
|
|
|
|
static isSQLite(knex) {
|
|
|
|
const driver = knex.client.config.client;
|
|
|
|
return ['sqlite3', 'better-sqlite3'].includes(driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the driver used is for MySQL
|
|
|
|
*
|
|
|
|
* @param {import('knex')} knex
|
|
|
|
*/
|
|
|
|
static isMySQL(knex) {
|
|
|
|
const driver = knex.client.config.client;
|
|
|
|
return ['mysql', 'mysql2'].includes(driver);
|
|
|
|
}
|
2022-01-18 12:03:21 +03:00
|
|
|
};
|