Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Sep 25, 2024
1 parent 422d9eb commit 05e81d1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
16 changes: 6 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ 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 ?? ''}`;
}
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);
Expand All @@ -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,
Expand Down
29 changes: 14 additions & 15 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import mssql from 'mssql'

const debug = Debug('mssql-multi-pool:index')

let shutdownIsInitialized = false
const POOLS = new Map<string, mssql.ConnectionPool>()

function getPoolKey(config: mssql.config): string {
Expand All @@ -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
Expand All @@ -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
}

/**
Expand All @@ -61,7 +59,7 @@ export async function releaseAll(): Promise<void> {

/**
* 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
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/config.githubActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 2 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand All @@ -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)
})
})

0 comments on commit 05e81d1

Please sign in to comment.