diff --git a/config.json.example b/config.json.example index d771410..b8a9d79 100644 --- a/config.json.example +++ b/config.json.example @@ -1,4 +1,6 @@ { + // Remove all comments from the config when using it! + "port": 80, "globalSalt": "[global salt (pepper) that is added to every ip before hashing to make it even harder for someone to decode the ip]", "adminUserID": "[the hashed id of the user who can perform admin actions]", @@ -8,6 +10,9 @@ "behindProxy": true, "db": "./databases/sponsorTimes.db", "privateDB": "./databases/private.db", + "createDatabaseIfNotExist": true, //This will run on startup every time (unless readOnly is true) - 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 c3b00f7..2e2b80e 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,8 @@ YouTubeAPI.authenticate({ var Sqlite3 = require('better-sqlite3'); let options = { - readonly: config.readOnly + readonly: config.readOnly, + fileMustExist: !config.createDatabaseIfNotExist }; //load database @@ -29,6 +30,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.readOnly) { + if (fs.existsSync(config.dbSchema)) db.exec(fs.readFileSync(config.dbSchema).toString()); + if (fs.existsSync(config.privateDBSchema)) privateDB.exec(fs.readFileSync(config.privateDBSchema).toString()); +} + // Create an HTTP service. http.createServer(app).listen(config.port); @@ -1055,4 +1061,4 @@ function getFormattedTime(seconds) { let formatted = minutes+ ":" + secondsDisplay; return formatted; -} \ No newline at end of file +}