generated from ctc-uci/npo-backend-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57cc996
commit 8c703e9
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |