From 82b3521f9e456623038f4c1670ac0a373fd156d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Wed, 25 Nov 2020 22:21:29 +0100 Subject: [PATCH] BETTERZON-38: Enabling DB access --- Backend/src/products/products.service.ts | 24 ++++++++++++++++++++++++ Backend/webpack.config.ts | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Backend/src/products/products.service.ts b/Backend/src/products/products.service.ts index ad44598..f8281de 100644 --- a/Backend/src/products/products.service.ts +++ b/Backend/src/products/products.service.ts @@ -1,3 +1,15 @@ +import * as dotenv from 'dotenv'; +dotenv.config(); + +const mariadb = require('mariadb'); +const pool = mariadb.createPool({ + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + connectionLimit: 5 +}); + /** * Data Model Interfaces */ @@ -61,6 +73,18 @@ const products: Products = { */ export const findAll = async (): Promise => { + let conn; + try { + conn = await pool.getConnection(); + const rows = await conn.query("SELECT * FROM products"); + console.log(rows); //[ {val: 1}, meta: ... ] + + } catch (err) { + throw err; + } finally { + if (conn) return conn.end(); + } + return products; }; diff --git a/Backend/webpack.config.ts b/Backend/webpack.config.ts index 5f03c7d..adb85fd 100644 --- a/Backend/webpack.config.ts +++ b/Backend/webpack.config.ts @@ -4,7 +4,7 @@ const nodeExternals = require("webpack-node-externals"); module.exports = { entry: ["webpack/hot/poll?100", "./src/index.ts"], - watch: true, + watch: false, target: "node", externals: [ nodeExternals({