question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FIX] strapi-v4 - data provider - deep data normalization

See original GitHub issue

Is your feature request related to a problem? Please describe. Populated data from strapi-v4 data provider are not normalized, which makes hard to work with them via ts interfaces.

Describe the solution you’d like Use deep traversal like the one proposed here : https://forum.strapi.io/t/discussion-regarding-the-complex-response-structure-for-rest-graphql-developer-experience/13400/9

const normalize = (data) => {
  const isObject = (data) =>
    Object.prototype.toString.call(data) === '[object Object]'
  const isArray = (data) =>
    Object.prototype.toString.call(data) === '[object Array]'

  const flatten = (data) => {
    if (!data.attributes) return data

    return {
      id: data.id,
      ...data.attributes
    }
  }

  if (isArray(data)) {
    return data.map((item) => normalize(item))
  }

  if (isObject(data)) {
    if (isArray(data.data)) {
      data = [...data.data]
    } else if (isObject(data.data)) {
      data = flatten({ ...data.data })
    } else if (data.data === null) {
      data = null
    } else {
      data = flatten(data)
    }

    for (const key in data) {
      data[key] = normalize(data[key])
    }

    return data
  }

  return data
}

Describe alternatives you’ve considered Alternative would be to normalize data after you get query data. This might be too late if you load it as useTable which expect slightly modified data already.

Additional context Read more at : https://forum.strapi.io/t/discussion-regarding-the-complex-response-structure-for-rest-graphql-developer-experience/13400

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
salihozdemircommented, Feb 11, 2022

Hey @lukassos 👋 ,

Thanks for your contribution. After this point, we will make a new release by making additions to what you have done. When finished, we would appreciate it if you could review the changes and make sure they are correct.

2reactions
omeraplakcommented, Feb 4, 2022

Hey @lukassos , I forgot we already normalized data. Thanks a lot for your warning. Now everything is clearer to me. Thanks so much for your passion!

What we have to do now is

  • Fixing tests (I’ll do that)
  • Updating our documentation

You can create a new section for the document. I’ll also try to set the “plugin::users-permissions.user” plugin today as you suggested. Is there anything I missed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Improve Data Normalization in Healthcare
Healthcare data normalization begins when patient records are collected from various sources, including EHRs, lab systems, billing systems, ...
Read more >
Data Normalization: Converting Raw Numbers Into Revenue
Data normalization is a rock-solid way to turn raw data into actionable insights. We explain why normalization is essential and how to ...
Read more >
Normalization | Machine Learning - Google Developers
The goal of normalization is to transform features to be on a similar scale. This improves the performance and training stability of the ......
Read more >
How, When, and Why Should You Normalize / Standardize ...
Normalization (Min-Max Scalar) : In this approach, the data is scaled to a fixed range — usually 0 to 1. In contrast to...
Read more >
4 Levels of Data Normalization
Data normalization is a process in the development of clean data. Diving deeper, however, the meaning or goal of data normalization is twofold:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found