Skip to content

Commit

Permalink
use dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Oct 18, 2024
1 parent 16dcf7c commit 9317f81
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 60 deletions.
7 changes: 3 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import msNodeSql from 'mssql/msnodesqlv8.js';
export declare function connect(config: msNodeSql.config): Promise<msNodeSql.ConnectionPool>;
import type mssqlTypes from 'mssql';
export declare const mssql: typeof import("mssql/msnodesqlv8.js");
export declare function connect(config: mssqlTypes.config): Promise<mssqlTypes.ConnectionPool>;
export declare function releaseAll(): Promise<void>;
export declare function getPoolCount(): number;
declare const _default: {
Expand All @@ -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';
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -46,4 +49,3 @@ export default {
releaseAll,
getPoolCount
};
export * as mssql from 'mssql/msnodesqlv8.js';
68 changes: 15 additions & 53 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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<string, msNodeSql.ConnectionPool>()
const POOLS = new Map<string, mssqlTypes.ConnectionPool>()

function getPoolKey(config: msNodeSql.config): string {
function getPoolKey(config: mssqlTypes.config): string {
return `${config.user ?? ''}@${config.server}/${
config.options?.instanceName ?? ''
};${config.database ?? ''}`
Expand All @@ -19,24 +27,23 @@ function getPoolKey(config: msNodeSql.config): string {
* @returns A MSSQL connection pool.
*/
export async function connect(
config: msNodeSql.config
): Promise<msNodeSql.ConnectionPool> {

config: mssqlTypes.config
): Promise<mssqlTypes.ConnectionPool> {
const poolKey = getPoolKey(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)
}

return pool as msNodeSql.ConnectionPool
return pool as mssqlTypes.ConnectionPool
}

/**
Expand Down Expand Up @@ -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'

0 comments on commit 9317f81

Please sign in to comment.