Skip to content

Commit

Permalink
switch more to msnodesqlv8
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Oct 18, 2024
1 parent 06c882a commit 16dcf7c
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 38 deletions.
4 changes: 3 additions & 1 deletion eslint.config.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { default } from 'eslint-config-cityssm';
import { type Config } from 'eslint-config-cityssm';
declare const config: Config;
export default config;
15 changes: 14 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export { default } from 'eslint-config-cityssm';
import eslintConfigCityssm, { tseslint } from 'eslint-config-cityssm';
const config = tseslint.config(...eslintConfigCityssm, {
rules: {
'@cspell/spellchecker': [
'warn',
{
cspell: {
words: ['cityssm', 'msnodesqlv8', 'nvarchar', 'recordset', 'tseslint']
}
}
]
}
});
export default config;
20 changes: 19 additions & 1 deletion eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
export { default } from 'eslint-config-cityssm'
import eslintConfigCityssm, {
type Config,
tseslint
} from 'eslint-config-cityssm'

const config = tseslint.config(...eslintConfigCityssm, {
rules: {
'@cspell/spellchecker': [
'warn',
{
cspell: {
words: ['cityssm', 'msnodesqlv8', 'nvarchar', 'recordset', 'tseslint']
}
}
]
}
}) as Config

export default config
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mssql from 'mssql';
export declare function connect(config: mssql.config): Promise<mssql.ConnectionPool>;
import msNodeSql from 'mssql/msnodesqlv8.js';
export declare function connect(config: msNodeSql.config): Promise<msNodeSql.ConnectionPool>;
export declare function releaseAll(): Promise<void>;
export declare function getPoolCount(): number;
declare const _default: {
Expand All @@ -8,5 +8,5 @@ declare const _default: {
getPoolCount: typeof getPoolCount;
};
export default _default;
export * as mssql from 'mssql';
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';
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';
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import Debug from 'debug';
import exitHook from 'exit-hook';
import mssql from 'mssql';
import msNodeSql from 'mssql/msnodesqlv8.js';
const debug = Debug('mssql-multi-pool:index');
const POOLS = new Map();
function getPoolKey(config) {
return `${config.user ?? ''}@${config.server}/${config.options?.instanceName ?? ''};${config.database ?? ''};${config.driver ?? 'tedious'}`;
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?.connected ?? false)) {
debug(`New database connection: ${poolKey}`);
pool =
(config.driver ?? '') === 'msnodesqlv8'
? await new msNodeSql.ConnectionPool(config).connect()
: await new mssql.ConnectionPool(config).connect();
pool = new msNodeSql.ConnectionPool(config);
await pool.connect();
POOLS.set(poolKey, pool);
}
return pool;
Expand Down Expand Up @@ -49,4 +46,4 @@ export default {
releaseAll,
getPoolCount
};
export * as mssql from 'mssql';
export * as mssql from 'mssql/msnodesqlv8.js';
26 changes: 13 additions & 13 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import Debug from 'debug'
import exitHook from 'exit-hook'
import mssql from 'mssql'
import msNodeSql from 'mssql/msnodesqlv8.js'

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

const POOLS = new Map<string, mssql.ConnectionPool>()
const POOLS = new Map<string, msNodeSql.ConnectionPool>()

function getPoolKey(config: mssql.config): string {
function getPoolKey(config: msNodeSql.config): string {
return `${config.user ?? ''}@${config.server}/${
config.options?.instanceName ?? ''
};${config.database ?? ''};${config.driver ?? 'tedious'}`
};${config.database ?? ''}`
}

/**
Expand All @@ -20,23 +19,24 @@ function getPoolKey(config: mssql.config): string {
* @returns A MSSQL connection pool.
*/
export async function connect(
config: mssql.config
): Promise<mssql.ConnectionPool> {
config: msNodeSql.config
): Promise<msNodeSql.ConnectionPool> {

const poolKey = getPoolKey(config)

let pool = POOLS.get(poolKey)

if (!(pool?.connected ?? false)) {
debug(`New database connection: ${poolKey}`)

pool =
(config.driver ?? '') === 'msnodesqlv8'
? await new msNodeSql.ConnectionPool(config).connect()
: await new mssql.ConnectionPool(config).connect()
pool = new msNodeSql.ConnectionPool(config)

await pool.connect()

POOLS.set(poolKey, pool)
}

return pool as mssql.ConnectionPool
return pool as msNodeSql.ConnectionPool
}

/**
Expand Down Expand Up @@ -90,7 +90,7 @@ export default {
getPoolCount
}

export * as mssql from 'mssql'
export * as mssql from 'mssql/msnodesqlv8.js'

export type {
Bit,
Expand Down Expand Up @@ -133,4 +133,4 @@ export type {
IResult,
ISqlType,
config
} from 'mssql'
} from 'mssql/msnodesqlv8.js'
17 changes: 10 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/node": "^22.7.6",
"cross-env": "^7.0.3",
"eslint-config-cityssm": "^13.0.0",
"prettier-config-cityssm": "^1.0.0"
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { after, describe, it } from 'node:test';
import { connect, getPoolCount, releaseAll } from '../index.js';
import { config } from './config.test.js';
await describe('mssql-multi-pool', async () => {
after(async () => {
await releaseAll();
after(() => {
void releaseAll();
});
await it('Connects to database', async () => {
const pool = await connect(config);
Expand Down
4 changes: 2 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { connect, getPoolCount, releaseAll } from '../index.js'
import { config } from './config.test.js'

await describe('mssql-multi-pool', async () => {
after(async () => {
await releaseAll()
after(() => {
void releaseAll()
})

await it('Connects to database', async () => {
Expand Down

0 comments on commit 16dcf7c

Please sign in to comment.