Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f07eb24

Browse files
author
Sylvestre Gug
committedJan 26, 2023
pools for mssql postgres and snowflake
1 parent 2080a85 commit f07eb24

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed
 

‎lib/mssql.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Transform} from "stream";
55

66
import {failedCheck, badRequest, notImplemented} from "./errors.js";
77
import {validateQueryPayload} from "./validate.js";
8+
import Pools from "./pools.js";
89

910
const TYPES = mssql.TYPES;
1011
const READ_ONLY = new Set(["SELECT", "USAGE", "CONNECT"]);
@@ -124,8 +125,6 @@ export async function check(req, res, pool) {
124125
return {ok: true};
125126
}
126127

127-
export const ConnectionPool = mssql.ConnectionPool;
128-
129128
export default (credentials) => {
130129
const pool = new mssql.ConnectionPool(credentials);
131130

@@ -144,6 +143,14 @@ export default (credentials) => {
144143
};
145144
};
146145

146+
export const pools = new Pools((credentials) =>
147+
Object.defineProperty(new mssql.ConnectionPool(credentials), "end", {
148+
value() {
149+
this.close();
150+
},
151+
})
152+
);
153+
147154
// See https://github.com/tediousjs/node-mssql/blob/66587d97c9ce21bffba8ca360c72a540f2bc47a6/lib/datatypes.js#L6
148155
const boolean = ["null", "boolean"],
149156
integer = ["null", "integer"],

‎lib/postgres.js

+17
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,25 @@ import pg from "pg";
33
import QueryStream from "pg-query-stream";
44
import JSONStream from "JSONStream";
55

6+
import Pools from "./pools.js";
7+
68
const {Pool} = pg;
79

10+
export const pools = new Pools(
11+
({host, port, database, user, password, ssl}) =>
12+
new pg.Pool({
13+
host,
14+
port,
15+
database,
16+
user,
17+
password,
18+
ssl: ssl === "required" ? {rejectUnauthorized: false} : false,
19+
connectionTimeoutMillis: 25e3,
20+
statement_timeout: 240e3,
21+
max: 30,
22+
})
23+
);
24+
825
export default (url) => {
926
const pool = new Pool({connectionString: url});
1027

‎lib/snowflake.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,40 @@ import {URL} from "url";
33
import JSONStream from "JSONStream";
44
import snowflake from "snowflake-sdk";
55

6-
export default url => {
6+
import Pools from "./pools.js";
7+
8+
export const pools = new Pools(
9+
({host, user, password, database, schema, warehouse, role}) =>
10+
Object.defineProperty(
11+
snowflake.createConnection({
12+
account: host,
13+
username: user,
14+
password,
15+
database,
16+
schema,
17+
warehouse,
18+
role,
19+
}),
20+
"end",
21+
{
22+
value() {
23+
this.destroy();
24+
},
25+
}
26+
)
27+
);
28+
29+
export default (url) => {
730
url = new URL(url);
8-
const {host, username, password, pathname, searchParams} = new URL(
9-
url
10-
);
31+
const {host, username, password, pathname, searchParams} = new URL(url);
1132
const connection = snowflake.createConnection({
1233
account: host,
1334
username,
1435
password,
1536
database: pathname.slice(1),
1637
schema: searchParams.get("schema"),
1738
warehouse: searchParams.get("warehouse"),
18-
role: searchParams.get("role")
39+
role: searchParams.get("role"),
1940
});
2041

2142
const connecting = new WeakSet();
@@ -61,8 +82,8 @@ export default url => {
6182
(schema[column.getName()] = dataTypeSchema(column)), schema
6283
),
6384
{}
64-
)
65-
}
85+
),
86+
},
6687
};
6788
res.end(`,"schema":${JSON.stringify(schema)}}`);
6889
};

0 commit comments

Comments
 (0)
Please sign in to comment.