♻ Don't hardcode DB settings but supply them to init function

This commit is contained in:
C-3PO 2018-11-15 19:18:41 +01:00
parent d2c5d9ccb2
commit 7995285034
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
5 changed files with 15 additions and 10 deletions

4
dbSettings.d.ts vendored
View file

@ -1,4 +0,0 @@
export const host: string;
export const user: string;
export const password: string;
export const database: string;

View file

@ -1,9 +1,11 @@
import * as mysql from 'mysql';
import * as dbSettings from '../../dbSettings';
import IDatabaseSettings from '../interfaces/IDatabaseSettings';
const connection = mysql.createConnection(dbSettings);
let connection: mysql.Connection;
export function init(dbSettings: IDatabaseSettings) {
connection = mysql.createConnection(dbSettings);
export function init() {
return new Promise((resolve, reject) => {
connection.connect(function(err) {
if (err) {

View file

@ -5,6 +5,6 @@ import init from './init';
//Exports various functions as endpoints
export default Object.freeze({
getManifest,
init,
productList,
ready: init(),
});

View file

@ -4,13 +4,14 @@ import createStatement from './database/createStatement';
import * as database from './database/database';
import products from './exports/productList';
import IDatabaseRow from './interfaces/IDatabaseRow';
import IDatabaseSettings from './interfaces/IDatabaseSettings';
import IProductData from './interfaces/IProductData';
import writeLog from './logger/writeLog';
export default async function init(): Promise<{ [key in Product]?: IProductData }> {
export default async function init(dbSettings: IDatabaseSettings): Promise<{ [key in Product]?: IProductData }> {
const output: { [key in Product]?: IProductData } = {};
await database.init();
await database.init(dbSettings);
//Only create table if it does not exist yet
const createIfNotExists = createStatement.replace(/^CREATE TABLE/, 'CREATE TABLE IF NOT EXISTS');

View file

@ -0,0 +1,6 @@
export default interface IDatabaseSettings {
host: string;
user: string;
password: string;
database: string;
}