Making database resources return data as indexed arrays

Database resource return data like this:

{
  "date": [
    "2023-04-08",
    "2023-04-09",
    "2023-04-10"
  ],
  "dl": [
    "3",
    "8",
    "9"
  ],
}

How do I make them return data like this instead:

[
  {"date": "2023-04-08", "dl": "3"},
  {"date": "2023-04-09", "dl": "8"},
  {"date": "2023-04-10", "dl": "9"},
]

You can run a transformer when the data is being returned from your api.
You can set this up in your database query here:

With javascript you can map the data to the new layout, eg:

const returnedData = {{ query.data }};

const newData= returnedData.date.map((date, index) =>
  Object.assign({}, { date, dl: apiData.dl[index] })
);

return newData;

This is an example using static data, but in your case

1 Like

Thanks :blush: I was hoping for something simpler, that I can easily use on multiple (or all) db resources, but it's nice that this is also an option.

Hey @erusev! I wonder if the formatDataAsArray() method would also be helpful for you :slight_smile:

https://docs.retool.com/reference/javascript-api-methods-array-object#formatdataasarray

1 Like

@victoria This is exactly the type of thing I was hoping for :blush: Thanks!

Very useful indeed.

ya!