-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
59 lines (51 loc) · 1.49 KB
/
db.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
const oracledb = require('oracledb');
require('dotenv').config();
// const sqlQuery = `SELECT username FROM USER_TABLE`;
// query(sqlQuery);
const query = async function (sqlQuery, type = 'SELECT') {
//confirm valid queryType
const validTypes = ['SELECT', 'INSERT'];
if (!validTypes.includes(type)) {
throw new Error('Invalid queryType given.');
}
let connection, output;
let dbConfig = {
user: process.env.USERNAME,
password: process.env.PASSWORD,
connectString: process.env.SVC_NAME,
// wallet_location: process.env.WALLET_LOC, //not yet working
// wallet_password: process.env.WALLET_PW,
};
try {
// Get a standalone Oracle Database connection
connection = await oracledb.getConnection(dbConfig);
console.log('Connection was successful!');
switch (type) {
case 'SELECT': {
const result = await connection.execute(sqlQuery);
console.log(`Output: ${result.rows}`); // <--- this is how you access results after .execute(SELECT QUERY)
output = result.rows;
}
case 'INSERT': {
const result = await connection.execute(
sqlQuery,
{} /*bindParams object*/,
{ autoCommit: true }
);
output = 'Data added successfully!';
}
}
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
return output;
}
};
module.exports = { query };