How to directly access array elements by index in the Data Storage?

Hi all,
Please help me in the following issue i am not able to achieve on data storage,

  1. when we do a client.db.set like for example,
    client.db.set(“records”, { “101”: {id:1,name:“James Bond”}, “102”:{id:2,name:“Rocky Balboa”} });
    which will set the record in the instance as
    {“agent-fdk:records_freshdesk”:{“101”:{“id”:1,“name”:“James Bond”},“102”:{“id”:2,“name”:“Rocky Balboa”},“createdAt”:1624631649144,“updatedAt”:1624631649144}}

  2. Now if i want to retrieve the db using db.get i have to perform as

client.db.get([“records”]).then(function (res) {
console.log(‘res’,res);
})
.catch(function(err){
console.log(‘err’,err);
})

it gives the record in console as
res {101: {…}, 102: {…}}101: {id: 1, name: “James Bond”}102: {id: 2, name: “Rocky Balboa”}proto: Object

Now my question is if i want to access the second record directly using the client.db.get, how can i achieve this?
i mean i dont want to get whole db in an varivable and find/map my required record, but instead i do wish to get it directly using like client.db.get(“records”)[“2”]
where id is 2

is it possible?

2 Likes

Hell Bhima,

Welcome to dev community.

You can’t directly get the data as you have mentioned in your query.

What my suggestion is to

EITHER:

Get the values in array by using:

client.db.get([“records”]).then(function (res) {
    var response = Object.values(res);
    console.log(response[0]) //whichever index you want to access
   //output: {id: 1, name: "James Bond"}
})

OR

Save data with respect to key:

client.db.set(“records_101”, {id:1,name:“James Bond”})
client.db.set(“records_102”,;{id:2,name:“Rocky Balboa”} );

In this way, you can retrieve the record as per your need by:

client.db.get([“records_101”]).then(function (res) {
   console.log(‘res’,res);
})
client.db.get([“records_102”]).then(function (res) {
   console.log(‘res’,res);
})

Hope this helps! :slight_smile:

3 Likes

Thanks Yusra,
I do understand that by creating different set of records we can retrieve the particular record directly as you suggested in the second option. but when i want to delete the records created for this purpose then i need to know the numbers like records_101 or 102… but the numbers are in not in order but instead it can be a random one and of any length.

but in the first option when we get the db.get(“records”) its will get the whole data in the memory… as our data are very huge or large number of records… its consume more amount of dynamic memory… but surely i want to use first option to store the records… but want to retrive the particular record/data directly.
is it possible

1 Like

Hey @bhima_plan8. As @yusrakhatri said, it can be achieved by setting individual records as record_{number}. If that doesn’t work, you can write a helper function similar to this

function getRecord(targetKey) {
return client.db.get('records').then(function (res) {
   return res[targetKey]
});
}
4 Likes

Hi @shravan.balasubraman i have done this as well… but here my concerns are when i do client.db.get(‘records’)…
the whole bunch of records are stored in the stack… by this there could be a lot of memory consumption but… i would like to use less of memory utilization and get the direct result of the required data

1 Like

Hey @bhima_plan8

For that, you can consider @yusrakhatri’s suggestion to split the keys. record_101, records_102.
That way memory consumption is split across keys.

2 Likes

Yup Sure, i guess thatz the only way even i feel…

2 Likes