Skip to content

Commit 5bfa995

Browse files
authored
Merge pull request #1 from softrams/add-snowflake-connection-pool
add snowflake connection pool
2 parents d5139b2 + dbc2335 commit 5bfa995

File tree

4 files changed

+1768
-149
lines changed

4 files changed

+1768
-149
lines changed

README.md

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,9 @@
1-
# MySQL DB Connector
1+
# Snowflake DB Connector
22

33
Wrapper utility to easily manage multiple data sources and pooled connections.
44

5-
## Error Returns
5+
Docs
6+
======================================================================
67

7-
By default, all errors occurring during a query are returned. If you want to be extra-safe in a production environment, you can set `HIDE_DB_ERRORS` value on the root `config` passed to the connector on initialization. When you do this, all errors will be logged, but none returned when a query error occurs.
8-
9-
## SSL Settings
10-
11-
This connector allows setting SSL connection using a few different options.
12-
13-
You can provide a custom cert:
14-
15-
```
16-
SSL: {
17-
CUSTOM_CERT: // custom cert string
18-
}
19-
```
20-
21-
By default, when specifying an SSL object, the mysql connector will reject unauthorized calls by adding `rejectUnauthorized: true`. You may override this setting by specifying a value for `REJECT_UNAUTHORIZED` in your `SSL` config:
22-
23-
```
24-
SSL: {
25-
REJECT_UNAUTHORIZED: false // not recommended
26-
}
27-
```
28-
29-
#Data Typing Options
30-
31-
This connector gives you a few options for configuring how data is returned from the connector. 'typeCast' defaults to true, and converts
32-
data from the database to its javascript equivalent. For example, it will convert DATETIME SQL objects to a DATE javascript type.
33-
You can also set 'dateStrings' which defaults to false. If you set it to true it will override typeCast and force date returns to be a string instead of a DATE type.
8+
For detailed documentation and basic usage examples, please see the documentation
9+
at `NodeJS Driver for Snowflake <https://docs.snowflake.com/en/user-guide/nodejs-driver-use.html>`_

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
};

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
{
2-
"name": "@softrams/nodejs-mysql-connector",
3-
"version": "0.0.13",
4-
"description": "Database connector wrapper to work with MySQL database from nodejs applications",
2+
"name": "@softrams/nodejs-snowflake-connector",
3+
"version": "0.0.01",
4+
"description": "Database connector wrapper to work with Snowflake database from nodejs applications",
55
"main": "index.js",
66
"scripts": {
77
"test": "yarn test"
88
},
99
"repository": {
1010
"type": "git",
11-
"url": "git+https://github.com/softrams/nodejs-mysql-connector.git"
11+
"url": "git+https://github.com/softrams/nodejs-snowflake-connector.git"
1212
},
1313
"keywords": [
1414
"Nodejs",
15-
"MySQL",
15+
"Snowflake",
1616
"Database"
1717
],
18-
"author": "Murali (murali@softrams.com)",
18+
"author": "Michael Vogel (michael.vogel@softrams.com)",
1919
"license": "UNLICENSED",
2020
"bugs": {
21-
"url": "https://github.com/softrams/nodejs-mysql-connector/issues"
21+
"url": "https://github.com/softrams/nodejs-snowflake-connector/issues"
2222
},
23-
"homepage": "https://github.com/softrams/nodejs-mysql-connector#readme",
23+
"homepage": "https://github.com/softrams/nodejs-snowflake-connector#readme",
2424
"dependencies": {
25-
"mysql": "^2.18.1"
25+
"snowflake-sdk": "^1.6.16"
2626
}
2727
}

0 commit comments

Comments
 (0)