diff --git a/index.js b/index.js index fc6a335..f5ee1f4 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,6 @@ import Debug from 'debug'; import exitHook from 'exit-hook'; import mssql from 'mssql'; const debug = Debug('mssql-multi-pool:index'); -let shutdownIsInitialized = false; const POOLS = new Map(); function getPoolKey(config) { return `${config.user ?? ''}@${config.server}/${config.options?.instanceName ?? ''};${config.database ?? ''}`; @@ -10,7 +9,7 @@ function getPoolKey(config) { export async function connect(config) { const poolKey = getPoolKey(config); let pool = POOLS.get(poolKey); - if (pool === undefined || !pool.connected) { + if (!(pool?.connected ?? false)) { debug(`New database connection: ${poolKey}`); pool = await new mssql.ConnectionPool(config).connect(); POOLS.set(poolKey, pool); @@ -36,14 +35,11 @@ export async function releaseAll() { export function getPoolCount() { return POOLS.size; } -if (!shutdownIsInitialized) { - debug('Initializing shutdown hooks.'); - exitHook(() => { - debug('Running shutdown hooks.'); - void releaseAll(); - }); - shutdownIsInitialized = true; -} +debug('Initializing shutdown hooks.'); +exitHook(() => { + debug('Running shutdown hooks.'); + void releaseAll(); +}); export default { connect, releaseAll, diff --git a/index.ts b/index.ts index 0d9d2c2..73bb194 100644 --- a/index.ts +++ b/index.ts @@ -4,7 +4,6 @@ import mssql from 'mssql' const debug = Debug('mssql-multi-pool:index') -let shutdownIsInitialized = false const POOLS = new Map() function getPoolKey(config: mssql.config): string { @@ -16,8 +15,8 @@ function getPoolKey(config: mssql.config): string { /** * Connect to a MSSQL database. * Creates a new connection if the configuration does not match a previously seen configuration. - * @param {mssql.config} config - MSSQL configuration. - * @returns {mssql.ConnectionPool} - A MSSQL connection pool. + * @param config - MSSQL configuration. + * @returns A MSSQL connection pool. */ export async function connect( config: mssql.config @@ -26,15 +25,14 @@ export async function connect( let pool = POOLS.get(poolKey) - // eslint-disable-next-line @typescript-eslint/prefer-optional-chain - if (pool === undefined || !pool.connected) { + if (!(pool?.connected ?? false)) { debug(`New database connection: ${poolKey}`) pool = await new mssql.ConnectionPool(config).connect() POOLS.set(poolKey, pool) } - return pool + return pool as mssql.ConnectionPool } /** @@ -61,7 +59,7 @@ export async function releaseAll(): Promise { /** * Retrieves the number of currently managed connection pools. - * @returns {number} - The number of pools. + * @returns The number of pools. */ export function getPoolCount(): number { return POOLS.size @@ -70,16 +68,17 @@ export function getPoolCount(): number { /** * Initialize shutdown. */ -if (!shutdownIsInitialized) { - debug('Initializing shutdown hooks.') - exitHook(() => { - debug('Running shutdown hooks.') - void releaseAll() - }) +debug('Initializing shutdown hooks.') - shutdownIsInitialized = true -} +exitHook(() => { + debug('Running shutdown hooks.') + void releaseAll() +}) + +/* + * Exports + */ export default { connect, diff --git a/test/config.githubActions.ts b/test/config.githubActions.ts index 1702bd8..51751ef 100644 --- a/test/config.githubActions.ts +++ b/test/config.githubActions.ts @@ -8,6 +8,7 @@ import type { config as ConnectionPoolConfig } from '../index.js' export const config: ConnectionPoolConfig = { server: 'localhost', user: 'sa', + // eslint-disable-next-line sonarjs/no-hardcoded-credentials password: 'dbatools.I0', database: 'master', options: { diff --git a/test/test.ts b/test/test.ts index 9dd6282..715c2f2 100644 --- a/test/test.ts +++ b/test/test.ts @@ -15,6 +15,7 @@ await describe('mssql-multi-pool', async () => { await pool.request().query('select 1') + // eslint-disable-next-line @typescript-eslint/no-magic-numbers assert.strictEqual(getPoolCount(), 1) }) @@ -31,6 +32,7 @@ await describe('mssql-multi-pool', async () => { await it('Releases all pools', async () => { await releaseAll() + // eslint-disable-next-line @typescript-eslint/no-magic-numbers assert.strictEqual(getPoolCount(), 0) }) })