generated from softrams/nodejs-mysql-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (134 loc) · 3.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* istanbul ignore file */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-else-return */
const snowflake = require("snowflake-sdk");
let pools = {};
let config = {};
const genericError = 'An error occurred communicating with the database.';
exports.init = async (cfg) => {
config = cfg;
};
exports.createSnowPool = async (poolName) => {
try {
const srcCfg = config.DATASOURCES[poolName];
if (srcCfg) {
const options = {
account: srcCfg.DB_HOST,
username: srcCfg.DB_USER,
password: srcCfg.DB_PASSWORD,
database: srcCfg.DB_DATABASE,
port: srcCfg.PORT,
schema: srcCfg.SCHEMA
};
pools[poolName] = snowflake.createPool(options, { max: 10, min: 0 });
console.debug(`Snowflake Adapter: Pool ${poolName} created`);
return true;
} else {
console.error(`Snowflake Adapter: Missing configuration for ${poolName}`);
return false;
}
} catch (err) {
console.error("Snowflake Adapter: Error while closing connection", err);
return false;
}
};
exports.connect = async (poolName) => {
try {
if (!pools[poolName]) {
await this.createSnowPool(poolName);
}
return pools[poolName];
} catch (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.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("Snowflake Adapter: Failure in query: ", err);
this.handleError(reject, err);
}
});
};
this.handleError = (reject, error) => {
const errorReturn = (config && config.HIDE_DB_ERRORS) ? new Error(genericError) : error;
reject(errorReturn);
};
exports.execute = async (srcName, query, params = {}) => {
try {
console.debug(query);
if (params) {
console.debug(JSON.stringify(params));
}
const start = process.hrtime();
const conn = await this.connect(srcName);
console.debug(
`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(
`Snowflake Adapter: Query executed: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
return results;
} catch (err) {
console.error("Snowflake Adapter: Error while executing query", err);
throw new Error(err.message);
}
};
exports.closeAllPools = async () => {
try {
for (const poolAlias of Object.keys(pools)) {
await this.closePool(poolAlias);
delete pools[poolAlias];
console.debug(`Snowflake Adapter: Pool ${poolAlias} closed`);
}
return true;
} catch (err) {
console.error("Snowflake Adapter: Error while closing connection", err);
return false;
}
};
exports.closePool = async (poolAlias) => {
try {
if (pools[poolAlias]) {
const poolConn = pools[poolAlias];
delete pools[poolAlias];
await poolConn.end((err) => {
if (err) {
console.error(
`Error while closing connection pool ${poolAlias}`,
err
);
}
});
return true;
} else {
return true;
}
} catch (err) {
console.error("Snowflake Adapter: Error while closing connection", err);
return false;
}
};