From 8c703e999f6cc8190204d6525d3d43e873203ed1 Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Mon, 27 Nov 2023 12:35:53 -0800 Subject: [PATCH] added connection to db --- app.js | 3 +++ common/utils.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +++ server/db.js | 15 ++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 common/utils.js create mode 100644 server/db.js diff --git a/app.js b/app.js index 7419cb1..18fd6c7 100644 --- a/app.js +++ b/app.js @@ -13,6 +13,9 @@ app.use( }), ); +// add all routes under here +app.use(express.json()); // for req.body + app.listen(PORT, () => { console.log(`Server listening on ${PORT}`); }); diff --git a/common/utils.js b/common/utils.js new file mode 100644 index 0000000..14fad66 --- /dev/null +++ b/common/utils.js @@ -0,0 +1,55 @@ +// toCamel, isArray, and isObject are helper functions used within utils only +const toCamel = (s) => { + return s.replace(/([-_][a-z])/g, ($1) => { + return $1.toUpperCase().replace('-', '').replace('_', ''); + }); +}; + +const isArray = (a) => { + return Array.isArray(a); +}; + +const isISODate = (str) => { + try { + const ISOString = str.toISOString(); + if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(ISOString)) return false; + const d = new Date(ISOString); + return d.toISOString() === ISOString; + } catch (err) { + return false; + } +}; + +const isObject = (o) => { + return o === Object(o) && !isArray(o) && typeof o !== 'function' && !isISODate(o); +}; + +// Database columns are in snake case. JavaScript is suppose to be in camel case +// This function converts the keys from the sql query to camel case so it follows JavaScript conventions +const keysToCamel = (data) => { + if (isObject(data)) { + const newData = {}; + Object.keys(data).forEach((key) => { + newData[toCamel(key)] = keysToCamel(data[key]); + }); + return newData; + } + if (isArray(data)) { + return data.map((i) => { + return keysToCamel(i); + }); + } + if ( + typeof data === 'string' && + data.length > 0 && + data[0] === '{' && + data[data.length - 1] === '}' + ) { + let parsedList = data.replaceAll('"', ''); + parsedList = parsedList.slice(1, parsedList.length - 1).split(','); + return parsedList; + } + return data; +}; + +module.exports = { keysToCamel }; diff --git a/package.json b/package.json index c5b52f1..5531bc8 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,10 @@ "eslint-plugin-import": "^2.25.2", "eslint-plugin-prettier": "^4.0.0", "express": "^4.17.1", + "express-promise-router": "^4.1.1", "nodemon": "^2.0.14", + "pg": "^8.8.0", + "pg-promise": "^10.12.1", "prettier": "^2.4.1" }, "devDependencies": { diff --git a/server/db.js b/server/db.js new file mode 100644 index 0000000..07490a8 --- /dev/null +++ b/server/db.js @@ -0,0 +1,15 @@ +const pgp = require('pg-promise')({}); +require('dotenv').config(); + +const db = pgp({ + host: process.env.AWS_HOST, + user: process.env.AWS_USER, + password: process.env.AWS_PASSWORD, + port: process.env.AWS_PORT, + database: process.env.AWS_DB_NAME, + ssl: { + rejectUnauthorized: false, + }, +}); + +module.exports = { db, pgp };