Saving and loading parts of configuration

Posted on


I have a piece of code where I have string pairs (eg. ‘a’ and ‘AA’), where both have their own unique ids. Id for first object is configured by user and second id is received at some point during runtime.

Example code:

Is there smarter way to load/save second ids from/to a file?
It feels somewhat unnecessary to loop through all configs every time new id is found (even if it doesn’t happen that often)

And if I added completely new pair-configs during runtime, how should I add them to user’s config file while still keeping it human readable?

Initial setup:

var fs = require('fs');

// config pair for first and second
function config (first, second, first_id) {
    this.first    = first;
    this.second   = second;
    // use name as an id for first if none is given
    this.first_id = first_id ? first_id : first;

var lookup = function (type, name, arr) {
    return arr.filter(function (obj) {
        return obj[type] === name;

// initial config file (few lines out of many)
// user doesn't know second ids
configs = [
    new config('a', 'AA', 'a_id'),
    new config('b', 'BB'),
    new config('c', 'C'),
    new config('d', 'D', 'd_id'),
    new config('e', 'EEE')

// read ids for seconds from file if it exists
// read json from file
input_json = {"AA":123,"C":321,"EEE":456};
for (var i=0; i<configs.length; ++i) {
    var key = configs[i].second;
    if (key in input_json) {
        configs[i].second_id = input_json[key];

While program is running and new id’s are found:

// add new id for 'BB'
BB_config = lookup('second', 'BB', configs);
BB_config.second_id = 654;

// write edited json to file
var output_json = {};
for (var i=0; i<configs.length; ++i) {
    if (configs[i].second_id) {
        output_json[configs[i].second] = configs[i].second_id;



How about storing your data in sqlite database? All of your operations can be implemented as relational database queries/commands.

Your table will have columns:


To perform updates on the second_id based on an object, just use (this is pseudo-code):

for (second in input_json) { UPDATE CONFIGS SET SECOND_ID = {input_json[second]} WHERE SECOND = {second} ...

You can make this run efficiently by creating an index on the SECOND column.

Lookup are just SELECT * FROM CONFIGS WHERE ...

For instance, to select only those configs which have second id:


It all depends on how many configs you have. For a large number using a sqlite database could be a win.

Leave a Reply

Your email address will not be published. Required fields are marked *