Skip to content

Commit 6d782f5

Browse files
committed
add snowflake connection pool
1 parent d5139b2 commit 6d782f5

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

index.js

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
/* eslint-disable no-restricted-syntax */
33
/* eslint-disable no-await-in-loop */
44
/* eslint-disable no-else-return */
5-
const mysql = require("mysql");
6-
const fs = require('fs');
5+
const snowflake = require("snowflake-sdk");
76

87
let pools = {};
98
let config = {};
@@ -13,73 +12,64 @@ exports.init = async (cfg) => {
1312
config = cfg;
1413
};
1514

16-
exports.createPool = async (poolName) => {
15+
exports.createSnowPool = async (poolName) => {
1716
try {
1817
const srcCfg = config.DATASOURCES[poolName];
1918
if (srcCfg) {
2019
const options = {
21-
connectionLimit: srcCfg.DB_CONNECTION_LIMIT || 5,
22-
host: srcCfg.DB_HOST,
23-
user: srcCfg.DB_USER,
20+
account: srcCfg.DB_HOST,
21+
username: srcCfg.DB_USER,
2422
password: srcCfg.DB_PASSWORD,
2523
database: srcCfg.DB_DATABASE,
2624
port: srcCfg.PORT,
27-
multipleStatements: srcCfg.ALLOW_MULTI_STATEMENTS || false,
28-
timezone: srcCfg.TIMEZONE || 'local',
29-
typeCast: srcCfg.TYPE_CAST || true,
30-
dateStrings: srcCfg.DATE_STRINGS || false
25+
schema: srcCfg.SCHEMA
3126
};
3227

33-
if (srcCfg.SSL) {
34-
const sslConfig = {};
35-
36-
if (srcCfg.SSL.CUSTOM_CERT) {
37-
sslConfig.ca = srcCfg.SSL.CUSTOM_CERT;
38-
} else {
39-
sslConfig.rejectUnauthorized = srcCfg.SSL.hasOwnProperty('REJECT_UNAUTHORIZED') ? srcCfg.SSL.REJECT_UNAUTHORIZED : true;
40-
}
41-
42-
options.ssl = sslConfig;
43-
}
44-
45-
pools[poolName] = mysql.createPool(options);
46-
console.debug(`MySQL Adapter: Pool ${poolName} created`);
28+
pools[poolName] = snowflake.createPool(options, { max: 10, min: 0 });
29+
console.debug(`Snowflake Adapter: Pool ${poolName} created`);
4730
return true;
4831
} else {
49-
console.error(`MySQL Adapter: Missing configuration for ${poolName}`);
32+
console.error(`Snowflake Adapter: Missing configuration for ${poolName}`);
5033
return false;
5134
}
5235
} catch (err) {
53-
console.error("MySQL Adapter: Error while closing connection", err);
36+
console.error("Snowflake Adapter: Error while closing connection", err);
5437
return false;
5538
}
5639
};
5740

5841
exports.connect = async (poolName) => {
5942
try {
6043
if (!pools[poolName]) {
61-
await this.createPool(poolName);
44+
await this.createSnowPool(poolName);
6245
}
6346
return pools[poolName];
6447
} catch (err) {
65-
console.error("MySQL Adapter: Error while retrieving a connection", err);
48+
console.error("Snowflake Adapter: Error while retrieving a connection", err);
6649
throw new Error(err.message);
6750
}
6851
};
6952

7053
this.query = async (conn, query, params) => {
7154
return new Promise((resolve, reject) => {
7255
try {
73-
conn.query(query, params, (error, results) => {
74-
if (error) {
75-
console.error("MySQL Adapter: Failure in query: ", error);
76-
this.handleError(reject, error);
77-
} else {
78-
resolve(results);
79-
}
56+
conn.use( async (clientConnection) => {
57+
await clientConnection.execute({
58+
sqlText: query,
59+
binds: params ? params : [],
60+
complete: async (err, stmt, rows) => {
61+
if (err) {
62+
console.error("Failed to execute statement due to the following error: " + err.message);
63+
reject();
64+
} else {
65+
console.log("Successfully executed statement: " + stmt.getSqlText());
66+
resolve(rows);
67+
}
68+
}
69+
})
8070
});
8171
} catch (err) {
82-
console.error("MySQL Adapter: Failure in query: ", err);
72+
console.error("Snowflake Adapter: Failure in query: ", err);
8373
this.handleError(reject, err);
8474
}
8575
});
@@ -101,21 +91,21 @@ exports.execute = async (srcName, query, params = {}) => {
10191
const conn = await this.connect(srcName);
10292

10393
console.debug(
104-
`MySQL Adapter: Connection secured: ${process.hrtime(start)[0]}s ${
94+
`Snowflake Adapter: Connection secured: ${process.hrtime(start)[0]}s ${
10595
process.hrtime(start)[1] / 1000000
10696
}ms`
10797
);
10898
const results = await this.query(conn, query, params);
10999

110100
console.debug(
111-
`MySQL Adapter: Query executed: ${process.hrtime(start)[0]}s ${
101+
`Snowflake Adapter: Query executed: ${process.hrtime(start)[0]}s ${
112102
process.hrtime(start)[1] / 1000000
113103
}ms`
114104
);
115105

116106
return results;
117107
} catch (err) {
118-
console.error("MySQL Adapter: Error while executing query", err);
108+
console.error("Snowflake Adapter: Error while executing query", err);
119109
throw new Error(err.message);
120110
}
121111
};
@@ -125,11 +115,11 @@ exports.closeAllPools = async () => {
125115
for (const poolAlias of Object.keys(pools)) {
126116
await this.closePool(poolAlias);
127117
delete pools[poolAlias];
128-
console.debug(`MySQL Adapter: Pool ${poolAlias} closed`);
118+
console.debug(`Snowflake Adapter: Pool ${poolAlias} closed`);
129119
}
130120
return true;
131121
} catch (err) {
132-
console.error("MySQL Adapter: Error while closing connection", err);
122+
console.error("Snowflake Adapter: Error while closing connection", err);
133123
return false;
134124
}
135125
};
@@ -152,7 +142,7 @@ exports.closePool = async (poolAlias) => {
152142
return true;
153143
}
154144
} catch (err) {
155-
console.error("MySQL Adapter: Error while closing connection", err);
145+
console.error("Snowflake Adapter: Error while closing connection", err);
156146
return false;
157147
}
158148
};

0 commit comments

Comments
 (0)