Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 5 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
# MySQL DB Connector
# Snowflake DB Connector

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

## Error Returns
Docs
======================================================================

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.

## SSL Settings

This connector allows setting SSL connection using a few different options.

You can provide a custom cert:

```
SSL: {
CUSTOM_CERT: // custom cert string
}
```

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:

```
SSL: {
REJECT_UNAUTHORIZED: false // not recommended
}
```

#Data Typing Options

This connector gives you a few options for configuring how data is returned from the connector. 'typeCast' defaults to true, and converts
data from the database to its javascript equivalent. For example, it will convert DATETIME SQL objects to a DATE javascript type.
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.
For detailed documentation and basic usage examples, please see the documentation
at `NodeJS Driver for Snowflake <https://docs.snowflake.com/en/user-guide/nodejs-driver-use.html>`_
74 changes: 32 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-else-return */
const mysql = require("mysql");
const fs = require('fs');
const snowflake = require("snowflake-sdk");

let pools = {};
let config = {};
Expand All @@ -13,73 +12,64 @@ exports.init = async (cfg) => {
config = cfg;
};

exports.createPool = async (poolName) => {
exports.createSnowPool = async (poolName) => {
try {
const srcCfg = config.DATASOURCES[poolName];
if (srcCfg) {
const options = {
connectionLimit: srcCfg.DB_CONNECTION_LIMIT || 5,
host: srcCfg.DB_HOST,
user: srcCfg.DB_USER,
account: srcCfg.DB_HOST,
username: srcCfg.DB_USER,
password: srcCfg.DB_PASSWORD,
database: srcCfg.DB_DATABASE,
port: srcCfg.PORT,
multipleStatements: srcCfg.ALLOW_MULTI_STATEMENTS || false,
timezone: srcCfg.TIMEZONE || 'local',
typeCast: srcCfg.TYPE_CAST || true,
dateStrings: srcCfg.DATE_STRINGS || false
schema: srcCfg.SCHEMA
};

if (srcCfg.SSL) {
const sslConfig = {};

if (srcCfg.SSL.CUSTOM_CERT) {
sslConfig.ca = srcCfg.SSL.CUSTOM_CERT;
} else {
sslConfig.rejectUnauthorized = srcCfg.SSL.hasOwnProperty('REJECT_UNAUTHORIZED') ? srcCfg.SSL.REJECT_UNAUTHORIZED : true;
}

options.ssl = sslConfig;
}

pools[poolName] = mysql.createPool(options);
console.debug(`MySQL Adapter: Pool ${poolName} created`);
pools[poolName] = snowflake.createPool(options, { max: 10, min: 0 });
console.debug(`Snowflake Adapter: Pool ${poolName} created`);
return true;
} else {
console.error(`MySQL Adapter: Missing configuration for ${poolName}`);
console.error(`Snowflake Adapter: Missing configuration for ${poolName}`);
return false;
}
} catch (err) {
console.error("MySQL Adapter: Error while closing connection", err);
console.error("Snowflake Adapter: Error while closing connection", err);
return false;
}
};

exports.connect = async (poolName) => {
try {
if (!pools[poolName]) {
await this.createPool(poolName);
await this.createSnowPool(poolName);
}
return pools[poolName];
} catch (err) {
console.error("MySQL Adapter: Error while retrieving a connection", err);
console.error("Snowflake Adapter: Error while retrieving a connection", err);
throw new Error(err.message);
}
};

this.query = async (conn, query, params) => {
return new Promise((resolve, reject) => {
try {
conn.query(query, params, (error, results) => {
if (error) {
console.error("MySQL Adapter: Failure in query: ", error);
this.handleError(reject, error);
} else {
resolve(results);
}
conn.use( async (clientConnection) => {
await clientConnection.execute({
sqlText: query,
binds: params ? params : [],
complete: async (err, stmt, rows) => {
if (err) {
console.error("Failed to execute statement due to the following error: " + err.message);
reject();
} else {
console.log("Successfully executed statement: " + stmt.getSqlText());
resolve(rows);
}
}
})
});
} catch (err) {
console.error("MySQL Adapter: Failure in query: ", err);
console.error("Snowflake Adapter: Failure in query: ", err);
this.handleError(reject, err);
}
});
Expand All @@ -101,21 +91,21 @@ exports.execute = async (srcName, query, params = {}) => {
const conn = await this.connect(srcName);

console.debug(
`MySQL Adapter: Connection secured: ${process.hrtime(start)[0]}s ${
`Snowflake Adapter: Connection secured: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
const results = await this.query(conn, query, params);

console.debug(
`MySQL Adapter: Query executed: ${process.hrtime(start)[0]}s ${
`Snowflake Adapter: Query executed: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);

return results;
} catch (err) {
console.error("MySQL Adapter: Error while executing query", err);
console.error("Snowflake Adapter: Error while executing query", err);
throw new Error(err.message);
}
};
Expand All @@ -125,11 +115,11 @@ exports.closeAllPools = async () => {
for (const poolAlias of Object.keys(pools)) {
await this.closePool(poolAlias);
delete pools[poolAlias];
console.debug(`MySQL Adapter: Pool ${poolAlias} closed`);
console.debug(`Snowflake Adapter: Pool ${poolAlias} closed`);
}
return true;
} catch (err) {
console.error("MySQL Adapter: Error while closing connection", err);
console.error("Snowflake Adapter: Error while closing connection", err);
return false;
}
};
Expand All @@ -152,7 +142,7 @@ exports.closePool = async (poolAlias) => {
return true;
}
} catch (err) {
console.error("MySQL Adapter: Error while closing connection", err);
console.error("Snowflake Adapter: Error while closing connection", err);
return false;
}
};
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"name": "@softrams/nodejs-mysql-connector",
"version": "0.0.13",
"description": "Database connector wrapper to work with MySQL database from nodejs applications",
"name": "@softrams/nodejs-snowflake-connector",
"version": "0.0.01",
"description": "Database connector wrapper to work with Snowflake database from nodejs applications",
"main": "index.js",
"scripts": {
"test": "yarn test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/softrams/nodejs-mysql-connector.git"
"url": "git+https://github.com/softrams/nodejs-snowflake-connector.git"
},
"keywords": [
"Nodejs",
"MySQL",
"Snowflake",
"Database"
],
"author": "Murali (murali@softrams.com)",
"author": "Michael Vogel (michael.vogel@softrams.com)",
"license": "UNLICENSED",
"bugs": {
"url": "https://github.com/softrams/nodejs-mysql-connector/issues"
"url": "https://github.com/softrams/nodejs-snowflake-connector/issues"
},
"homepage": "https://github.com/softrams/nodejs-mysql-connector#readme",
"homepage": "https://github.com/softrams/nodejs-snowflake-connector#readme",
"dependencies": {
"mysql": "^2.18.1"
"snowflake-sdk": "^1.6.16"
}
}
Loading