From 70291c935b10335492ed28d584038b19255a1014 Mon Sep 17 00:00:00 2001 From: Long9725 <86904692+Long9725@users.noreply.github.com> Date: Thu, 24 Nov 2022 21:26:25 +0900 Subject: [PATCH 1/2] feat: db_helper.js --- db_helper.js | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 db_helper.js diff --git a/db_helper.js b/db_helper.js new file mode 100644 index 0000000..aede125 --- /dev/null +++ b/db_helper.js @@ -0,0 +1,172 @@ +const oracledb = require('oracledb'); +const { dbConnector } = require('./db.js'); +const { DbStatusEnum } = require("./db_status.js"); +const { TypeCheck } = require("../utils/type_check.js"); + +export const dbHelper = (() => { + const _generateWildcard = (data) => { + switch (data.length) { + case 1: + return "(:1)"; + case 2: + return "(:1, :2)"; + default: + var wildcard = ""; + + for (const index in data) { + console.log(index); + if (index === 0) { + wildcard += "(:1, "; + console.log(wildcard); + continue; + } + if (index === data.length - 1) { + wildcard += `:${index + 1}\)`; + console.log(wildcard); + continue; + } + wildcard += `:${index + 1}, `; + console.log(wildcard); + } + + return wildcard; + } + }; + + const _columnsToString = (columns) => { + switch (data.length) { + case 1: + return `(${columns[0]})`; + case 2: + return `(${columns[0]}, ${columns[1]})`; + default: + var temp = ""; + + for (const index in data) { + if (index === 0) { + temp += `(${columns[index]}, `; + console.log(temp); + continue; + } + if (index === data.length - 1) { + temp += `${columns[index]})` + console.log(temp); + continue; + } + temp += `${columns[index]}, `; + console.log(temp); + } + + return temp; + } + } + + return { + /** + * @author Jang Seongho + * + * @function + * @type {arrow function} + * + * @param { String } table + * @param { Array? } columns + * @param { Array } data + * + * @description + * The insert function to the oracle db. + * + * @returns { DbStatusEnum.notInitialized } + * if db connected + * + * @returns { DbStatusEnum.typeError } + * if the type of parameter isn't matched + * + * @returns { bool } + * * isConnected(): get the server is connected oracle-db. + * + * @throws {'oracledb get connection is failed'} the server fails connect to oracle-db. + */ + insert: async (table, columns, data) => { + if (!dbConnector.isConnected()) { + return { + ...DbStatusEnum.notInitialized + }; + } + + if (TypeCheck.getType(table) !== "String") { + return { + ...DbStatusEnum.typeError + }; + } + + let isColumnsNull = false; + + if (columns !== null) { + isColumnsNull = true; + } + + if (!isColumnsNull && TypeCheck.getType(columns) !== "Array") { + return { + ...DbStatusEnum.typeError + }; + } + + if (TypeCheck.getType(data) !== "Array") { + return { + ...DbStatusEnum.typeError + }; + } + + let sql; + + if (isColumnsNull) { + sql = `INSERT INTO ${table} VALUES ${_generateWildcard(data)}`; + console.log(sql); + try { + // await dbConnector.connection.execute(sql, data); + return { + ...DbStatusEnum.success + }; + } catch (e) { + return { + ...DbStatusEnum.dbError + }; + } + } + + if (columns.length !== data.length) { + return { + ...DbStatusEnum.parameterLengthNotMatched + }; + } + + sql = `INSERT INTO ${table} ${_columnsToString(columns)} VALUES ${_generateWildcard(data)}`; + try { + // await dbConnector.connection.execute(sql, data); + return { + ...DbStatusEnum.success + }; + } catch (e) { + return { + ...DbStatusEnum.dbError + }; + } + }, + insertMany: async (table, columns, data) => { + // Insert some rows + + const sql = `INSERT INTO nodetab VALUES (:1, :2)`; + + const binds = + [[1, "First"], + [2, "Second"], + [3, "Third"], + [4, "Fourth"], + [5, "Fifth"], + [6, "Sixth"], + [7, "Seventh"]]; + + await connection.executeMany(sql, binds); + } + }; +})(); From 87273d68de960b426a3f22a8743400f59f82700b Mon Sep 17 00:00:00 2001 From: Long9725 <86904692+Long9725@users.noreply.github.com> Date: Thu, 24 Nov 2022 21:26:43 +0900 Subject: [PATCH 2/2] feat: db.js --- db.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 db.js diff --git a/db.js b/db.js new file mode 100644 index 0000000..5f71f63 --- /dev/null +++ b/db.js @@ -0,0 +1,54 @@ +require('dotenv').config({ path: './.env.local' }); +const oracledb = require('oracledb'); + +/** + * @author Jang Seongho + * + * @constant + * @type {Object} + * + * @description + * The closure of oracle-db connector. + * + * This offers these: + * + * @returns { Object } + * * connection: get oracle-db connection. + * + * @returns { bool } + * * isConnected(): get the server is connected oracle-db. + * + * @throws {'oracledb get connection is failed'} the server fails connect to oracle-db. + */ +const dbConnector = (async () => { + let _connection = null; + + const _user = process.env.ORACLE_USER; + const _password = process.env.ORACLE_PASSWORD; + const _connectionString = process.env.ORACLE_CONNECTION_STRING; + + try { + _connection = await oracledb.getConnection({ user: _user, password: _password, connectionString: _connectionString }); + } catch (err) { + console.error(err); + } finally { + if (!_connection) { + throw 'oracledb get connection is failed'; + } + } + + return { + get connection () { + return _connection; + }, + isConnected: () => { + if(_connection === null) { + return false; + } + + return true; + } + } +})(); + +module.exports = { dbConnector };