A simple way to manage connections to multiple SQL Server databases using the Node.js Tedious package (node-mssql).
- 💪 Fully typed. Exports all types from node-mssql as well.
- 🧠 Automatically uses the MSNodeSQLv8 driver on Windows to support Windows authentication, and the Tedious driver on other operating systems.
- 🧹 Automatically cleans up all pools on exit.
The fantastic node-mssql package provides an easy and reliable way to connect to one SQL Server instance.
Connecting to multiple SQL Server instances is where things get trickier. Not too tricky, but it is more involved as you can't rely on the global pool. There is some sample code in the node-mssql README that shows how to do it.
This project implements that sample code. 👌
npm install @cityssm/mssql-multi-pool
Replace the connect(config)
command from the mssql
package
with the connect(config)
command from the @cityssm/mssql-multi-pool
package.
If the configuration object describes an unseen database, a new pool is made. Otherwise, the previously made pool is used.
import mssqlMultiPool, { type mssqlTypes } from '@cityssm/mssql-multi-pool'
const pool = await mssqlMultiPool.connect(config)
// Use the `mssql` package like before.
const results = (await pool
.request()
.input('workOrderNumber', '2024-01234')
.query(
'select * from WorkOrders where workOrderNumber = @workOrderNumber'
)) as mssqlTypes.IResult<WorkOrder>