Commit e876f230 authored by Milan Wikarski's avatar Milan Wikarski 🕳

Second commit

parent ab80b552
......@@ -35,18 +35,9 @@
* @param {String} tableName - the name of the table
*/
// NEMAZS ANI ZA BOHA 🖕
const { Pool } = require('pg');
const { snake2camel } = require('./utils/case');
const _db = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT
});
class DB {
_db = null;
......@@ -93,7 +84,7 @@ class DB {
_sendResponse(q, data = []) {
return new Promise((resolve, reject) => {
_db.query(q, data, (err, res) =>
this._db.query(q, data, (err, res) =>
err ? reject(err) : resolve(res.rows.map(record => snake2camel(record)))
);
});
......@@ -104,7 +95,7 @@ class DB {
}
tableExists(tableName) {
return _db.query(
return this._db.query(
'SELECT EXISTS( SELECT 1 FROM information_schema.tables WHERE table_name=$1 )',
[tableName],
(err, res) => (err ? reject(err) : resolve(res.rows[0][0]))
......
const snake2camel = obj => {
if (typeof obj === 'string') {
return obj
.split('_')
.map((word, i) =>
i === 0
? word.toLowerCase()
: word
.split('')
.map((char, i) =>
i === 0 ? char.toUpperCase() : char.toLowerCase()
)
.join('')
)
.join('');
} else {
Object.keys(obj).forEach(key => {
const camelCase = snake2camel(key);
if (key !== camelCase) {
obj[camelCase] = obj[key];
delete obj[key];
}
});
return obj;
}
};
exports.snake2camel = snake2camel;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment