@kwaeri/database-driver
Version:
The @kwaeri/database-driver component of the @kwaer/node-kit application platform.
71 lines • 2.89 kB
JavaScript
/**
* SPDX-PackageName: kwaeri/database-driver
* SPDX-PackageVersion: 0.6.0
* SPDX-FileCopyrightText: © 2014 - 2022 Richard Winters <kirvedx@gmail.com> and contributors
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR MIT
*/
;
// INCLUDES
import debug from 'debug';
// DEFINES
const DEBUG = debug('kwaeri:database-driver');
/**
* The Base Driver to inherit from for ensuring any platform
* requirements are met
*/
export class DatabaseDriver {
/**
* @var { string } type
*/
type;
/**
* @var {@link DriverConnectionBits} connection
*/
connection;
/**
* Class constructor
*/
constructor(config) {
// Here we must determine our DBO and make a connection
if (!config.host)
throw new Error(`[ERROR][DATABASE][CONSTRUCTOR]: There was an issue instantiating the database driver object. Database host value is missing from connection details.`);
if (config.port === undefined || config.port === null ||
(config.port && ((typeof config.port == "string" && config.port === "") || (typeof config.port != "number" && typeof config.port != "string"))))
throw new Error(`[ERROR][DATABASE][CONSTRUCTOR]: There was an issue instantiating the database driver object. Database port value is missing from connection details.`);
if (!config.database)
throw new Error(`[ERROR][DATABASE][CONSTRUCTOR]: There was an issue instantiating the database driver object. Database name value is missing from connection details.`);
if (!config.user)
throw new Error(`[ERROR][DATABASE][CONSTRUCTOR]: There was an issue instantiating the database driver object. Database user value is missing from connection details.`);
if (!config.password)
throw new Error(`[ERROR][DATABASE][CONSTRUCTOR]: There was an issue instantiating the database driver object. Database password value is missing from connection details.`);
DEBUG(`Set 'type' to '${config.type}'`);
// Set connection details
this.type = config.type || "mysql";
DEBUG(`Set 'connection' using 'config' values:`);
this.connection = {
host: config.host,
port: (typeof config.port == "string") ? parseInt(config.port) : config.port,
database: config.database,
user: config.user,
password: config.password
};
DEBUG(`'${this.connection}'`);
}
/**
* Getter for the driver type
*
* @returns { string } The string type.
*/
get getType() {
return this.type;
}
/**
* Getter for the connection details
*
* @returns { DriverConnectionBits } A {@link DriverConnectionBits} object.
*/
get getConnection() {
return this.connection;
}
}
//# sourceMappingURL=database-driver.mjs.map