BETTERZON-38: Enabling DB access

This commit is contained in:
Patrick Müller 2020-11-25 22:21:29 +01:00
parent 6b8acdab46
commit 82b3521f9e
2 changed files with 25 additions and 1 deletions

View File

@ -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 * Data Model Interfaces
*/ */
@ -61,6 +73,18 @@ const products: Products = {
*/ */
export const findAll = async (): Promise<Products> => { export const findAll = async (): Promise<Products> => {
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; return products;
}; };

View File

@ -4,7 +4,7 @@ const nodeExternals = require("webpack-node-externals");
module.exports = { module.exports = {
entry: ["webpack/hot/poll?100", "./src/index.ts"], entry: ["webpack/hot/poll?100", "./src/index.ts"],
watch: true, watch: false,
target: "node", target: "node",
externals: [ externals: [
nodeExternals({ nodeExternals({