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; +}