Redux multilevel nested entities - javascript

I've got some data that looks like:
videoGameCharacter:
----data
----actionSequence:
--------data
--------actionsArray:
------------data
and so on.. So my corresponding characterReducer holds an array of videoGameCharacter objects.
The question :
a) would you recommend i broke this up into a less nested format making it more flat and referencing everything with keys? I dont know if it should make a difference at all. Its the first time im dealing with data that belongs together so strictly having this many nested entitities with nested entities.
b) if not, should the one api call retrieve data in this format or should i keep the data in the corresponding seperate api calls? currently iv e structured my api call to return data as displayed above and even though this saves me some extra calls to the server it makes things a bit more complex.

Related

Dynamically creating tables with indexedDb

On my web-app, the user can request different data lines. Those data lines have an unique "statusID" each, say "18133". When the user requests to load the data, it is either loaded from the server or from indexedDB(that part im trying to figure out). In order to make it as fast as possible, I want the index to be the timestamp of the data, as I will request ranges which are smaller than the actual data in the indexedDB. However, I am trying to figure out how to create the stores and store the data properly. I tried to dynamically create stores everytime data with a new Id is requested, but creating stores is only possible in "onupradeneeded". I also thought about storing everything in the same store, but I fear that the performance will make that bad. I do not really now how to approach this thing.
What I do know: If you index a value, it means that the data is sorted, which is exactly what I want. I dont know if the following is possible but this would solve my issue too: store everthing in the same store, index by "statusID" and index by "timestamp". This way, it would be fast too i guess.
Note that I am talking about many many datapoints, possible in the millions.
You can index by multiple values, allowing you to get all by statusID and restricting to a range for your timestamp. So I'd go with the one datastore solution. Performance should not be an issue.
This earlier post may be helpful: Javascript: Searching indexeddb using multiple indexes

LiveCycle: Load JSON data into pre-existing objects

A quick overview of the situation:
For simplicity of complicated data, I've got a series of JavaScript objects (built using function Object Factories) that handle auditing data, so for example, you have a child object inside a parent object inside a user object (these are basically arrays). These objects contain functions that help process the data they contain, both in and out, when in the UI.
On the top level, a single JSON function call pulls the data the objects contain out, without the functions (which is what I want to happen, as only the data is stored in an external database).
The problem
However, on pulling the information back from the database, using JSON.parse and assigning the result into the top level object holder obviously only contains just the data, no functions.
Bearing in mind the object arrays won't be initialised (as they won't know the length until extracted from the JSON data), how can I reinsert the JSON data back into the objects, functions included?
Is there some way of saying to the parser 'for this level of data, use this object, for this level, use this one'?
[Note: due to the nature of the organisation and security, external libraries aren't permitted. No jQuery suggestions etc.]

Should arbitrarily-sized arrays be stored under a single key in localStorage?

I would like to persist a list of similarly-typed data on the client using localStorage. Each piece of data is a high-score and other information about a game session. Accessing by key isn't necessary here; I'm more likely going to iterate the list and display either the full list or sections of it.
It would look like there are two choices for me:
Stringify the whole array somehow and store them under the same key (perhaps "gamedata" or something similar).
Store each piece of data under a separate key (perhaps using a timestamp).
Here are the downsides for each choice:
Adding one item into the list (the most common operation) will require reading the whole list, adding the item, and writing the data back to localStorage. I'd expect about a hundred items in the list, but there might be outliers that play the game more often, and I don't want it to lag for them. The data might get corrupted if the user closes the page or shuts down their computer while writing to localStorage, thereby losing all their game data. It also doesn't really feel right to read the whole thing and write most of it back untouched.
I have to iterate the whole localStorage to find all my data. Doesn't feel like the right use of an associative array. Also, this method would probably be less efficient in terms of storage space due to the storage of keys.
Which method is the preferred method for storing my data?

Can you store a reference to a mongo collection?

At the top of a file can I put something like...
var collection = db.mongo.collection('test', function(err, collection){return collection});
and then in any of the files functions use collection.find() etc
I guess my question is... is collection a reference to the collection or a copy of the data?
If data in the collection changes will i still get up to date data by querying the collection variable?
Thanks!!
Collection is a reference for the collection object. Until you issue a find (or findOne) you don't have real data in your hands. And even then, it returns a Cursor object leaving the collection object always untouched.
Storing both Collections or cursors will not store your data. remember that you could be dealing with millions of records. dealing with data itself could be overwhelming for the server memory. Instead, mongo returns cursors and references for you to filter away. In PHP you have a function called iterator_to_array that you can pass it the cursor and it converts to an array of data. In javascript I don't know if there is such functions. But I guess it doesn't makes sense to be such functions. Filter the information until you have manageable data size, then iterate over the cursor and do your thing. If you have something like a config array or such, intead of several documents, try to store everything on one and fetch it with the findOne() function.
But in the end I guess that's just a design question whether your data is possible to filter or not.

How to create a tree representation of generic JSON data in HTML?

I have some data stored in JSON format. I would like to be able to display it in a browser as a dynamic tree structure in a similar way MongoVUE presents the mondodb documents:
Screenshot
I have found a very nice jquery plugin, called jsTree. Unfortunately, in order to process JSON documents it requires data to have a very specific verbose (and redundant, in my opinion) structure: link. Using it means significant modifications of my json documents. I am rather searching for a tool that is able to build the tree automagically, without making severe manual adjustments to the data, yet allowing me to potentially apply some modifications to the view, if I would need to.
The tool at json.bloople.net makes something similar using a table, but because I have several levels of nested documents, the output looks very bloated. Moreover, the structure is not dynamically collapsible.
I would appreciate any hints regarding the right tools to do the job, including both those that might require (automated!) pre-processing of JSON data in Java/Groovy or pure JavaScript-based solution.
This is just a simple example of how you could output a tree like JSON structure in html. http://jsfiddle.net/K2ZQQ/1/ (see here for browser support for white-space). Note that the second parameter to JSON.stringify is a replacer function:
From http://msdn.microsoft.com/en-us/library/ie/cc836459(v=vs.94).aspx
If replacer is a function, JSON.stringify calls the function, passing
in the key and value of each member. The return value is used instead
of the original value. If the function returns undefined, the member
is excluded. The key for the root object is an empty string: "".
So if you need to add any further modifications to the dislpay of your JSON tree the replacer function may be of help.

Categories

Resources