Updated call signature of database-info lib

refs https://github.com/TryGhost/Toolbox/issues/174

- ok, iteration 3 on how this library should work
- 95% of my use cases just need to pass an knex instance and return if
  it's mysql/sqlite
- i don't want to have to initialize the class in this library to get
  that
- this commit reworks the public interface to return a function with
  some simple `is*` functions for those uses cases, or to return the
  class otherwise
This commit is contained in:
Daniel Lockyer 2022-03-02 12:11:18 +01:00
parent 3e9c584589
commit 032e9db2f1

View File

@ -1,4 +1,27 @@
module.exports = class DatabaseInfo {
/**
* @param {import('knex')} knex
*/
module.exports = (knex) => {
const driver = knex.client.config.client;
return {
/**
* Returns if the driver used is for SQLite
*/
isSQLite: () => {
return ['sqlite3'].includes(driver);
},
/**
* Returns if the driver used is for MySQL
*/
isMySQL: () => {
return ['mysql', 'mysql2'].includes(driver);
}
};
};
module.exports.DatabaseInfo = class DatabaseInfo {
/**
* @param {import('knex')} knex
*/
@ -81,29 +104,4 @@ module.exports = class DatabaseInfo {
getVersion() {
return this._databaseDetails.version;
}
/**
* Returns if the driver used is for SQLite
*/
isSQLite() {
return ['sqlite3'].includes(this._driver);
}
/**
* Returns if the driver used is for MySQL
*/
isMySQL() {
return ['mysql', 'mysql2'].includes(this._driver);
}
/**
* This allows you to use a different DB connection than the one we initialized the lib with
*
* @param {import('knex')} knex
*
* @returns DatabaseInfo
*/
connection(knex) {
return new DatabaseInfo(knex);
}
};