From 7acb19756b30d8b269b798b898a68d2f350629e9 Mon Sep 17 00:00:00 2001 From: Joe Dowd Date: Thu, 13 Feb 2020 00:03:09 +0000 Subject: [PATCH] Create DB form schema on start if config option is set --- config.json.example | 5 ++++- index.js | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config.json.example b/config.json.example index d771410..b90e11e 100644 --- a/config.json.example +++ b/config.json.example @@ -8,6 +8,9 @@ "behindProxy": true, "db": "./databases/sponsorTimes.db", "privateDB": "./databases/private.db", + "createDatabaseIfNotExist": true, //depends on mode='development' //This will run on startup every time - so ensure "create table if not exists" is used in the schema + "dbSchema": "./databases/_sponsorTimes.db.sql", + "privateDBSchema": "./databases/_private.db.sql", "mode": "development", "readOnly": false -} \ No newline at end of file +} diff --git a/index.js b/index.js index e0c993f..73727ba 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,11 @@ var db = new Sqlite3(config.db, options); //where the more sensitive data such as IP addresses are stored var privateDB = new Sqlite3(config.privateDB, options); +if (config.createDatabaseIfNotExist && (config.mode === "development")) { + db.exec(fs.readFileSync(config.dbSchema).toString()); + privateDB.exec(fs.readFileSync(config.privateDBSchema).toString()); +} + // Create an HTTP service. http.createServer(app).listen(config.port); @@ -1049,4 +1054,4 @@ function getFormattedTime(seconds) { let formatted = minutes+ ":" + secondsDisplay; return formatted; -} \ No newline at end of file +}