Global set objects from csv Rows of configuration file

I am sure this is a basic coding principle but i am unable to find a forum thread for the problem
I receive (tcp) data with a device number and need to correlate it to an ip and a name
We want to load the parameters to memory to speed up the process because mysql is slower then we would like

Imagine 3 devices a,d,g
We have a csv with parameters for devices
For example
number,ip,name
a,b,c
d,e,f
g,h,i

flow is currently
Inject=> file-in=> csv=> change
(Set global.number to msg.payload)
This gives me an object in global context with all the parameters for g,h,i only
Makes sense as each line would over ride the previous

What is the syntax for iterating something like
From csv node into function node
var i = msg.payload.col1;
global.set(“$col1”,i);
// above would only give me global.$col1 of “g”

I imagine it is a forEach but so far way above my pay grade

Use case has 22 columns of parameters and and 100 rows of devices (up to 500 max) so individual entries would not be viable

One way to organise it would be a sparse array where the index is the number and the ip and name are in the array element. So to initialise it, if you were writing code, you might do something like

let table = []
table[735] = {ip: "192.168.16.4", name: "192.168.16.5}
table[832] = {ip: "192.168.16.4", name: "192.168.16.5}

and so on. Then when you have a device number and want the ip and name you can use table[number].ip and table[number].name.

You could read your csv file on startup and build the global table by picking up each line of the file and adding them into the table.
As for how to do that then read in the file a line at a time and send it to a function node, in the function node then pick up the table from global context and add the new element in.

1 Like

Thank you Colin
I appreciate the insight
Will test and come back to results

You have answered what would have been a question later . How to retrieve the table entries

With ...table[832] = ...
Can I replace “832“ with a variable like msg.payload.col1 ?
To start a loop to read in entire csv , one message per line

Yes, that is what I was attempting to suggest.

[Edit] But if it is coming in as a string use table[Number(msg.payload.col1)] = ...

your solution works nicely
just need to put table into global context but that is another thread
Thanks Again

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.