Skip to content

Commit 1a54c62

Browse files
Sylvestremootari
andauthored
Leverage pools of pool manager from data-connector. (#47)
* centralized pool of pools * exports pools * make it driver agnostic, use callback to setup pool into pools * use controller to abort all * Apply suggestions from code review Co-authored-by: Fabian Iwand <[email protected]> * no need to monkey patch close * keyOf + key in closure * mssql signal to handle closing and deleting * Update lib/mssql.js Co-authored-by: Fabian Iwand <[email protected]> * remove pool of pools for mssql * imports * await for the connection * remove pools from exports * bump node version for Dockerfile * no pool to test Co-authored-by: Fabian Iwand <[email protected]>
1 parent 533b228 commit 1a54c62

File tree

3 files changed

+23
-49
lines changed

3 files changed

+23
-49
lines changed

.env.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
21
export const MSSQL_CREDENTIALS = env("MSSQL_CREDENTIALS");
3-
export const MSSQL_CREDENTIALS_READ_ONLY = env("MSSQL_CREDENTIALS_READ_ONLY");
42
export const NODE_ENV = env("NODE_ENV");
53

64
function env(key, defaultValue) {

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:16.17.0-alpine
1+
FROM node:18.12.1-alpine
22

33
RUN mkdir /app
44
WORKDIR /app

lib/mssql.js

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import Ajv from "ajv";
22
import JSONStream from "JSONStream";
33
import {json} from "micro";
44
import mssql from "mssql";
5-
import {failedCheck, badRequest, notImplemented} from "./errors.js";
65
import {Transform} from "stream";
76

8-
const TYPES = mssql.TYPES;
9-
10-
const pools = new Map();
7+
import {failedCheck, badRequest, notImplemented} from "./errors.js";
118

9+
const TYPES = mssql.TYPES;
1210
const READ_ONLY = new Set(["SELECT", "USAGE", "CONNECT"]);
1311

1412
const ajv = new Ajv();
@@ -22,37 +20,10 @@ const validate = ajv.compile({
2220
},
2321
});
2422

25-
// See: https://tediousjs.github.io/node-mssql/#connection-pools
26-
export const mssqlPool = {
27-
get: (name, config) => {
28-
if (!pools.has(name)) {
29-
if (!config) {
30-
throw new Error("Database configuration required");
31-
}
32-
33-
const pool = new mssql.ConnectionPool(config);
34-
const close = pool.close.bind(pool);
35-
pool.close = (...args) => {
36-
pools.delete(name);
37-
return close(...args);
38-
};
39-
40-
pools.set(name, pool.connect());
41-
}
42-
43-
return pools.get(name);
44-
},
45-
46-
closeAll: () =>
47-
Promise.all(
48-
Array.from(pools.values()).map((connect) => {
49-
return connect.then((pool) => pool.close());
50-
})
51-
),
52-
};
53-
5423
export async function queryStream(req, res, pool) {
55-
const db = await pool;
24+
const connection = await pool;
25+
const db = await connection.connect();
26+
5627
const body = await json(req);
5728

5829
if (!validate(body)) throw badRequest();
@@ -144,7 +115,8 @@ export async function queryStream(req, res, pool) {
144115
* them up that these may be too permissive.
145116
* */
146117
export async function check(req, res, pool) {
147-
const db = await pool;
118+
const connection = await pool;
119+
const db = await connection.connect();
148120

149121
// See: https://learn.microsoft.com/en-us/sql/relational-databases/system-functions/sys-fn-my-permissions-transact-sql
150122
const rows = await db.request().query(
@@ -163,20 +135,24 @@ export async function check(req, res, pool) {
163135
return {ok: true};
164136
}
165137

166-
export default (credentials) => async (req, res) => {
167-
const pool = mssqlPool.get(JSON.stringify(credentials), credentials);
138+
export const ConnectionPool = mssql.ConnectionPool;
168139

169-
if (req.method === "POST") {
170-
if (req.url === "/check") {
171-
return check(req, res, pool);
172-
}
140+
export default (credentials) => {
141+
const pool = new mssql.ConnectionPool(credentials);
173142

174-
if (["/query-stream"].includes(req.url)) {
175-
return queryStream(req, res, pool);
176-
}
143+
return async (req, res) => {
144+
if (req.method === "POST") {
145+
if (req.url === "/check") {
146+
return check(req, res, pool);
147+
}
177148

178-
throw notImplemented();
179-
}
149+
if (req.url === "/query-stream") {
150+
return queryStream(req, res, pool);
151+
}
152+
153+
throw notImplemented();
154+
}
155+
};
180156
};
181157

182158
// See https://github.com/tediousjs/node-mssql/blob/66587d97c9ce21bffba8ca360c72a540f2bc47a6/lib/datatypes.js#L6

0 commit comments

Comments
 (0)