isundil 1 anno fa
parent
commit
19bc7f347e
3 ha cambiato i file con 16 aggiunte e 9 eliminazioni
  1. 1 1
      package.json
  2. 10 3
      src/config.js
  3. 5 5
      src/databaseHelper.js

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "craftlabhttpserver",
-  "version": "1.0.1",
+  "version": "20240122.2",
   "description": "",
   "main": "main.js",
   "scripts": {

+ 10 - 3
src/config.js

@@ -20,12 +20,19 @@ function pickConfig(defaultConfig, configContent) {
 }
 
 let configEntries = {};
+let initialContent = null;
 
 module.exports = configEntries;
+module.exports.getInitialContent = function() {
+    if (!initialContent) {
+        let configFile = (process?.argv?.[2] || FILENAME);
+        console.log("CONFIG: Loading configuration from "+configFile);
+        initialContent = JSON.parse(fs.readFileSync(configFile));
+    }
+    return initialContent;
+}
 module.exports.Initialize = function(defaultConfig) {
-    let configFile = (process?.argv?.[2] || FILENAME);
-    console.log("CONFIG: Loading configuration from "+configFile);
-    let configContent = JSON.parse(fs.readFileSync(configFile));
+    let configContent = module.exports.getInitialContent();
 
     let out = pickConfig(defaultConfig, configContent);
     for (let i in out)

+ 5 - 5
src/databaseHelper.js

@@ -2,20 +2,20 @@
 const sqlite3 = require('sqlite3');
 const CONFIG = require('./config.js');
 
-let ALL_MODELS = [ ];
-
 function DatabaseHelper() {
     this.db = null;
+    this.models = [];
 }
 
-DatabaseHelper.prototype.init = function() {
+DatabaseHelper.prototype.init = function(allModels) {
+    this.models = allModels;
     return new Promise((ok, ko) => {
         this.db = new sqlite3.Database(CONFIG.database, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE | sqlite3.OPEN_FULLMUTEX, async (err) => {
             if (err) {
                 ko(err);
                 return;
             }
-            let types = ALL_MODELS;
+            let types = this.models;
             for (let i =0; i < types.length; ++i) {
                 let instance = new types[i]();
                 await instance.createOrUpdateBase(this);
@@ -129,7 +129,7 @@ DatabaseHelper.prototype.buildWhere = function(whereObj, columns, args) {
 
 DatabaseHelper.prototype.maxVersion = async function(instanceName) {
     let queryParts = [];
-    for (let i of ALL_MODELS)
+    for (let i of this.models)
         queryParts.push("select max(" +i.prototype.versionColumn.call(null) +") as v from " +i.prototype.getTableName.call(null) +" where instance=:0");
     let version = await this.runSql("select max(v) as v from (" +queryParts.join(" union ") +")", [ instanceName ]);
     return (version?.[0]?.v) || 0;