database connection

Former-commit-id: 49ae8447241762c2beac2c5c1d3ce32592a96b3a
This commit is contained in:
Ben
2022-02-05 16:50:59 +00:00
parent 78cbba8049
commit e4fcd35317
10 changed files with 471 additions and 380 deletions

BIN
README.md

Binary file not shown.

780
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,7 +6,7 @@
"scripts": {
"test": "jest src/__test",
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"dev": "nodemon src/index.js --watch src",
"setup": "npm i"
},
"author": "Ben Kyd <benjaminkyd@gmail.com> (https://benkyd.co.uk)",
@@ -17,9 +17,10 @@
"dotenv": "^10.0.0",
"express": "^4.17.2",
"jest": "^27.4.5",
"jsdom": "^19.0.0",
"md5": "^2.3.0",
"moment": "^2.29.1",
"node-fetch": "^2.6.7"
"node-fetch": "^2.6.7",
"pg": "^8.7.3",
"pg-native": "^3.0.0"
}
}

View File

@@ -3,7 +3,7 @@ const Logger = require('./logger.js');
const dotenv = require('dotenv');
function load() {
Logger.info('Loading config...');
Logger.info('Loading Config...');
const res = dotenv.config();
Logger.debug(`CONFIG: ${JSON.stringify(res.parsed)}`);
Logger.debug(`CONFIG: running in ${res.parsed.NODE_ENV} mode`);

View File

47
src/database/database.js Normal file
View File

@@ -0,0 +1,47 @@
const Logger = require('../logger.js');
const { Client } = require('pg');
class Database {
constructor() {
this.connection = null;
}
async connect(options) {
Logger.info('Database Connecting...');
// review options
if (!options) {
options = {
user: process.env.DATABASE_USER,
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_DB,
password: process.env.DATABASE_PASSWORD,
port: process.env.DATABASE_PORT
};
}
this.options = options;
this.connection = await this.connectToDatabase();
Logger.info('Database Connected');
}
async connectToDatabase() {
return new Promise((resolve, reject) => {
const psqlClient = new Client(this.options);
psqlClient.connect();
psqlClient.query('SELECT NOW()', (err, res) => {
if (err) reject(err);
this.connection = psqlClient;
Logger.database(`PSQL Time: ${res.rows[0].now}`);
Logger.database(`Connected to ${this.options.host}`);
resolve(psqlClient);
});
});
}
}
module.exports = Database;

View File

@@ -2,11 +2,16 @@ const Logger = require('./logger.js');
const Config = require('./config.js');
const Server = require('./routes/server.js');
const Databse = require('./database/database.js');
async function main() {
Logger.info('Pre-Init Loading...');
Logger.init();
Config.load();
const Database = new Databse();
await Database.connect();
Server.listen(process.env.PORT);
}

View File

@@ -49,6 +49,14 @@ module.exports.middleware = function(origin, message) {
+ clc.yellow(`MIDDLEWARE: ${origin}`) + '] ' + message);
}
module.exports.database = function(message) {
let d = moment().format(dateFormat);
fs.appendFileSync(logPath, `[${d.toLocaleString()}] [POSTGRES: SQL] ${message} \n`);
if (LogLevel > 0) return;
console.log('[' + d.toLocaleString() + '] ['
+ clc.magentaBright(`POSTGRES: SQL`) + '] ' + message);
}
module.exports.debug = function(message) {
let d = moment().format(dateFormat);
fs.appendFileSync(logPath, `[${d.toLocaleString()}] [DEBUG] ${message} \n`);

View File

@@ -11,7 +11,7 @@ function logRequest(req, res, next)
function listen(port) {
app.listen(port);
Logger.info(`LISTENING ON ${port}...`);
Logger.info(`Listening on ${port}...`);
Logger.info(`Setting up basic middleware...`);
app.use(logRequest);