@starbemtech/star-db-query-builder
Version:
A query builder to be used with mysql or postgres
162 lines • 5.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllDbClients = exports.getDbClient = exports.initDb = void 0;
const pg_1 = require("pg");
const promise_1 = require("mysql2/promise");
const pgClient_1 = require("./pgClient");
const mysqlClient_1 = require("./mysqlClient");
const dbClients = {};
const defaultName = 'default';
/**
* Initialize a database client with the specified configuration
*
* @template T - The type of connection options (PoolConfig for PostgreSQL or PoolOptions for MySQL)
* @param config - Configuration object for database initialization
* @param config.name - Optional name for the database client (defaults to 'default')
* @param config.type - Database type ('pg' for PostgreSQL or 'mysql' for MySQL)
* @param config.options - Connection options specific to the database type
* @param config.retryOptions - Optional retry configuration for failed queries
* @param config.installUnaccentExtension - Optional flag to install unaccent extension (PostgreSQL only)
* @returns Promise<void> - Resolves when the database client is successfully initialized
*
* @throws {Error} When type is not provided or is invalid
* @throws {Error} When connection options are not provided
* @throws {Error} When an unsupported database type is specified
*
* @example
* // Initialize PostgreSQL client
* await initDb({
* name: 'myPostgresDb',
* type: 'pg',
* options: {
* host: 'localhost',
* port: 5432,
* database: 'mydb',
* user: 'user',
* password: 'password'
* },
* retryOptions: { maxRetries: 3, delay: 1000 },
* installUnaccentExtension: true
* });
*
* @example
* // Initialize MySQL client
* await initDb({
* name: 'myMysqlDb',
* type: 'mysql',
* options: {
* host: 'localhost',
* port: 3306,
* database: 'mydb',
* user: 'user',
* password: 'password'
* },
* retryOptions: { maxRetries: 3, delay: 1000 }
* });
*/
const initDb = async (config) => {
if (!config.type)
throw new Error('Type is required. Accept values: pg | mysql');
if (!config.options)
throw new Error('Connection options is required');
if (config.type === 'pg') {
const poolConfig = config.options;
const pool = new pg_1.Pool(config.options);
dbClients[config.name || defaultName] = await (0, pgClient_1.createPgClient)(pool, config.retryOptions, poolConfig, config.installUnaccentExtension);
console.log(`@starbemtech/star-db-query-builder: Postgres db client "${config.name}" created successfully`);
}
else if (config.type === 'mysql') {
const pool = (0, promise_1.createPool)(config.options);
dbClients[config.name || defaultName] = (0, mysqlClient_1.createMysqlClient)(pool, config.retryOptions);
console.info(`@starbemtech/star-db-query-builder: Postgres db client "${config.name}" created successfully`);
}
else {
throw new Error('Unsupported database type');
}
};
exports.initDb = initDb;
/**
* Initialize a database client with the specified configuration
*
* @template T - The type of connection options (PoolConfig for PostgreSQL or PoolOptions for MySQL)
* @param config - Configuration object for database initialization
* @param config.name - Optional name for the database client (defaults to 'default')
* @param config.type - Database type ('pg' for PostgreSQL or 'mysql' for MySQL)
* @param config.options - Connection options specific to the database type
* @param config.retryOptions - Optional retry configuration for failed queries
* @param config.installUnaccentExtension - Optional flag to install unaccent extension (PostgreSQL only)
* @returns Promise<void> - Resolves when the database client is successfully initialized
*
* @throws {Error} When database type is not provided or is invalid
* @throws {Error} When connection options are not provided
* @throws {Error} When an unsupported database type is specified
*
* @example
* // Initialize PostgreSQL client
* await initDb({
* name: 'myPostgresDb',
* type: 'pg',
* options: {
* host: 'localhost',
* port: 5432,
* database: 'mydb',
* user: 'user',
* password: 'password'
* },
* retryOptions: { maxRetries: 3, delay: 1000 },
* installUnaccentExtension: true
* });
*
* @example
* // Initialize MySQL client
* await initDb({
* name: 'myMysqlDb',
* type: 'mysql',
* options: {
* host: 'localhost',
* port: 3306,
* database: 'mydb',
* user: 'user',
* password: 'password'
* },
* retryOptions: { maxRetries: 3, delay: 1000 }
* });
*/
const getDbClient = (name) => {
const client = dbClients[name || defaultName];
if (!client) {
throw new Error(`Database client "${name}" is not initialized`);
}
return client;
};
exports.getDbClient = getDbClient;
/**
* Retrieves a specific database client by name
*
* @param name - Optional name of the database client to retrieve. If not provided, returns the default client
* @returns IDatabaseClient - The requested database client instance
*
* @throws {Error} When the specified database client name is not found or not initialized
*
* @example
* // Get default database client
* const defaultClient = getDbClient();
*
* @example
* // Get specific database client by name
* const postgresClient = getDbClient('myPostgresDb');
* const mysqlClient = getDbClient('myMysqlDb');
*
* @example
* // Handle error when client is not found
* try {
* const client = getDbClient('nonExistentDb');
* } catch (error) {
* console.error('Database client not found:', error.message);
* }
*/
const getAllDbClients = () => {
return dbClients;
};
exports.getAllDbClients = getAllDbClients;
//# sourceMappingURL=initDb.js.map