diff --git a/index.d.ts b/index.d.ts index b1ef67e..fa2f1b3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,6 @@ -import msNodeSql from 'mssql/msnodesqlv8.js'; -export declare function connect(config: msNodeSql.config): Promise; +import type mssqlTypes from 'mssql'; +export declare const mssql: typeof import("mssql/msnodesqlv8.js"); +export declare function connect(config: mssqlTypes.config): Promise; export declare function releaseAll(): Promise; export declare function getPoolCount(): number; declare const _default: { @@ -8,5 +9,3 @@ declare const _default: { getPoolCount: typeof getPoolCount; }; export default _default; -export * as mssql from 'mssql/msnodesqlv8.js'; -export type { Bit, BigInt, Decimal, Float, Int, Money, Numeric, SmallInt, SmallMoney, Real, TinyInt, Char, NChar, Text, NText, VarChar, NVarChar, Xml, Time, Date, DateTime, DateTime2, DateTimeOffset, SmallDateTime, UniqueIdentifier, Variant, Binary, VarBinary, Image, UDT, Geography, Geometry, TYPES, ConnectionPool, Transaction, IColumnMetadata, IRecordSet, IResult, ISqlType, config } from 'mssql/msnodesqlv8.js'; diff --git a/index.js b/index.js index 39f82bc..f66252e 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ import Debug from 'debug'; import exitHook from 'exit-hook'; -import msNodeSql from 'mssql/msnodesqlv8.js'; +const mssqlImport = process.platform === 'win32' + ? await import('mssql/msnodesqlv8.js') + : await import('mssql'); +export const mssql = mssqlImport.default; const debug = Debug('mssql-multi-pool:index'); const POOLS = new Map(); function getPoolKey(config) { @@ -11,7 +14,7 @@ export async function connect(config) { let pool = POOLS.get(poolKey); if (!(pool?.connected ?? false)) { debug(`New database connection: ${poolKey}`); - pool = new msNodeSql.ConnectionPool(config); + pool = new mssql.ConnectionPool(config); await pool.connect(); POOLS.set(poolKey, pool); } @@ -46,4 +49,3 @@ export default { releaseAll, getPoolCount }; -export * as mssql from 'mssql/msnodesqlv8.js'; diff --git a/index.ts b/index.ts index 238040a..367d8f0 100644 --- a/index.ts +++ b/index.ts @@ -1,12 +1,20 @@ import Debug from 'debug' import exitHook from 'exit-hook' -import msNodeSql from 'mssql/msnodesqlv8.js' +import type mssqlTypes from 'mssql' + +const mssqlImport = + process.platform === 'win32' + ? await import('mssql/msnodesqlv8.js') + : await import('mssql') + +// eslint-disable-next-line @typescript-eslint/prefer-destructuring +export const mssql = mssqlImport.default const debug = Debug('mssql-multi-pool:index') -const POOLS = new Map() +const POOLS = new Map() -function getPoolKey(config: msNodeSql.config): string { +function getPoolKey(config: mssqlTypes.config): string { return `${config.user ?? ''}@${config.server}/${ config.options?.instanceName ?? '' };${config.database ?? ''}` @@ -19,9 +27,8 @@ function getPoolKey(config: msNodeSql.config): string { * @returns A MSSQL connection pool. */ export async function connect( - config: msNodeSql.config -): Promise { - + config: mssqlTypes.config +): Promise { const poolKey = getPoolKey(config) let pool = POOLS.get(poolKey) @@ -29,14 +36,14 @@ export async function connect( if (!(pool?.connected ?? false)) { debug(`New database connection: ${poolKey}`) - pool = new msNodeSql.ConnectionPool(config) + pool = new mssql.ConnectionPool(config) await pool.connect() POOLS.set(poolKey, pool) } - return pool as msNodeSql.ConnectionPool + return pool as mssqlTypes.ConnectionPool } /** @@ -89,48 +96,3 @@ export default { releaseAll, getPoolCount } - -export * as mssql from 'mssql/msnodesqlv8.js' - -export type { - Bit, - BigInt, - Decimal, - Float, - Int, - Money, - Numeric, - SmallInt, - SmallMoney, - Real, - TinyInt, - Char, - NChar, - Text, - NText, - VarChar, - NVarChar, - Xml, - Time, - Date, - DateTime, - DateTime2, - DateTimeOffset, - SmallDateTime, - UniqueIdentifier, - Variant, - Binary, - VarBinary, - Image, - UDT, - Geography, - Geometry, - TYPES, - ConnectionPool, - Transaction, - IColumnMetadata, - IRecordSet, - IResult, - ISqlType, - config -} from 'mssql/msnodesqlv8.js'