♻ Don't hardcode DB settings but supply them to init function
This commit is contained in:
parent
d2c5d9ccb2
commit
7995285034
5 changed files with 15 additions and 10 deletions
4
dbSettings.d.ts
vendored
4
dbSettings.d.ts
vendored
|
@ -1,4 +0,0 @@
|
||||||
export const host: string;
|
|
||||||
export const user: string;
|
|
||||||
export const password: string;
|
|
||||||
export const database: string;
|
|
|
@ -1,9 +1,11 @@
|
||||||
import * as mysql from 'mysql';
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
connection.connect(function(err) {
|
connection.connect(function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
|
@ -5,6 +5,6 @@ import init from './init';
|
||||||
//Exports various functions as endpoints
|
//Exports various functions as endpoints
|
||||||
export default Object.freeze({
|
export default Object.freeze({
|
||||||
getManifest,
|
getManifest,
|
||||||
|
init,
|
||||||
productList,
|
productList,
|
||||||
ready: init(),
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,13 +4,14 @@ import createStatement from './database/createStatement';
|
||||||
import * as database from './database/database';
|
import * as database from './database/database';
|
||||||
import products from './exports/productList';
|
import products from './exports/productList';
|
||||||
import IDatabaseRow from './interfaces/IDatabaseRow';
|
import IDatabaseRow from './interfaces/IDatabaseRow';
|
||||||
|
import IDatabaseSettings from './interfaces/IDatabaseSettings';
|
||||||
import IProductData from './interfaces/IProductData';
|
import IProductData from './interfaces/IProductData';
|
||||||
import writeLog from './logger/writeLog';
|
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 } = {};
|
const output: { [key in Product]?: IProductData } = {};
|
||||||
|
|
||||||
await database.init();
|
await database.init(dbSettings);
|
||||||
|
|
||||||
//Only create table if it does not exist yet
|
//Only create table if it does not exist yet
|
||||||
const createIfNotExists = createStatement.replace(/^CREATE TABLE/, 'CREATE TABLE IF NOT EXISTS');
|
const createIfNotExists = createStatement.replace(/^CREATE TABLE/, 'CREATE TABLE IF NOT EXISTS');
|
||||||
|
|
6
src/interfaces/IDatabaseSettings.ts
Normal file
6
src/interfaces/IDatabaseSettings.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
export default interface IDatabaseSettings {
|
||||||
|
host: string;
|
||||||
|
user: string;
|
||||||
|
password: string;
|
||||||
|
database: string;
|
||||||
|
}
|
Loading…
Reference in a new issue