{"size": 1020, "ext": "js", "max_stars_repo_path": "functions/database/system_settings_array.js", "max_stars_repo_name": "peter-stuart-turner/iot-wastewater-management-backend", "max_stars_repo_stars_event_min_datetime": "2020-03-23T03:38:28.000Z", "max_stars_repo_stars_event_max_datetime": "2020-03-23T03:38:28.000Z", "max_issues_repo_path": "functions/database/system_settings_array.js", "max_issues_repo_name": "peter-stuart-turner/iot-wastewater-management-backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "functions/database/system_settings_array.js", "max_forks_repo_name": "peter-stuart-turner/iot-wastewater-management-backend", "max_forks_repo_forks_event_min_datetime": "2020-01-05T16:01:49.000Z", "max_forks_repo_forks_event_max_datetime": "2020-01-05T16:01:49.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 32.9032258065, "max_line_length": 84, "alphanum_fraction": 0.7441176471}
const HashMap = require('hashmap');

function initializeSystemSettingsArray() {

  var databaseReference = FIREBASE_ADMIN.getDatabaseReference();

  // Get system settings from database on startup
  var system_settings_db = databaseReference.child(FIREBASE_SYSTEMS_SETTINGS_TABLE);

  // Update on item addition
  system_settings_db.on(FIREBASE_CHILD_ADDED, function(snapshot, prevChildKey) {
    const key = snapshot.key;
    const value = snapshot.val().settings;
    var valueMap = new HashMap();
    valueMap.set('settings', value);

    FIREBASE_SYSTEM_SETTINGS_ARRAY.set(key, valueMap);
    LOGGER.info("Added system settings " + key + " to table");
  });

  // Update on item deletion
  system_settings_db.on(FIREBASE_CHILD_REMOVED, function(snapshot, prevChildKey) {
    const key = snapshot.key;
    FIREBASE_SYSTEM_SETTINGS_ARRAY.remove(key);
    LOGGER.info("Removed setting " + key + " from table");
  });

  LOGGER.info("Initialized system settings array")
}
module.exports = initializeSystemSettingsArray;
