From 7995285034ee14903235e37e3dc5c908383b937c Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 15 Nov 2018 19:18:41 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Don't=20hardcode=20DB=20settings=20?= =?UTF-8?q?but=20supply=20them=20to=20init=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbSettings.d.ts | 4 ---- src/database/database.ts | 8 +++++--- src/index.ts | 2 +- src/init.ts | 5 +++-- src/interfaces/IDatabaseSettings.ts | 6 ++++++ 5 files changed, 15 insertions(+), 10 deletions(-) delete mode 100644 dbSettings.d.ts create mode 100644 src/interfaces/IDatabaseSettings.ts diff --git a/dbSettings.d.ts b/dbSettings.d.ts deleted file mode 100644 index 473a57c..0000000 --- a/dbSettings.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -export const host: string; -export const user: string; -export const password: string; -export const database: string; diff --git a/src/database/database.ts b/src/database/database.ts index f065853..fd781a4 100644 --- a/src/database/database.ts +++ b/src/database/database.ts @@ -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) { diff --git a/src/index.ts b/src/index.ts index ed9a081..fac3d4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,6 @@ import init from './init'; //Exports various functions as endpoints export default Object.freeze({ getManifest, + init, productList, - ready: init(), }); diff --git a/src/init.ts b/src/init.ts index 5a12937..b417448 100644 --- a/src/init.ts +++ b/src/init.ts @@ -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'); diff --git a/src/interfaces/IDatabaseSettings.ts b/src/interfaces/IDatabaseSettings.ts new file mode 100644 index 0000000..ac367c6 --- /dev/null +++ b/src/interfaces/IDatabaseSettings.ts @@ -0,0 +1,6 @@ +export default interface IDatabaseSettings { + host: string; + user: string; + password: string; + database: string; +}