2
\$\begingroup\$

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: http://repl.it/BE5N

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;
 })[0];
};
// 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;
 }
}
console.log(JSON.stringify(output_json));
200_success
146k22 gold badges190 silver badges478 bronze badges
asked Aug 30, 2015 at 21:53
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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:

  • FIRST
  • SECOND
  • FIRST_ID
  • SECOND_ID

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

for (second in input_json) {
 ...run 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:

SELECT * FROM CONFIGS WHERE SECOND_ID <> ""

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

answered Aug 30, 2015 at 22:16
\$\endgroup\$
2
  • \$\begingroup\$ using even simple database is bit out of scope of this particular project. Setting up sql database just for few name:id pairs feels like an overkill. \$\endgroup\$ Commented Aug 30, 2015 at 22:43
  • \$\begingroup\$ That's why I said it all depends on how many configs you have. If you only have a few configs then iterating over all of them is not a big deal. How many configs are you going to be working with? In any case, it illustrates the fact that JSON in a file can only be processed sequentially and in its entirety - there is no random access and partial updates. \$\endgroup\$ Commented Aug 30, 2015 at 22:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.