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.

Unflatten results of raw queries

See original GitHub issue

Sequelize will not process the results of raw queries. This becomes a problem with eager loading as the data returned from the underlying db engine looks like this:

{
  'id: 1,
  'foo': 'bar',
  'user.id': 222,
  'user.name': 'David'
}

This should be converted into a nested structure:

{
  'id: 1,
  'foo': 'bar',
  'user': {
    'id': 222,
    'name': 'David'
  }
}

Unfortunately, the method which does the nesting is “_private”, and the techniques used within the method will not work on “raw” data. Thus, there are two solutions:

  1. Set raw: false whenever eager loading is happening. And then serialize the results into a POJO.
    • pros: will let sequelize do the nesting
    • cons: sequelize is instantiating all of the data just for the sake of nesting it. This is not very efficient and loses the performance gains of “raw” queries.
  2. We write a nesting function ourselves.
    • pros: maintains the performance gains of nested queries.
    • cons: will use slightly different technique than sequelize internals and could cause unforeseen problems?

My vote is for #2, as I think it will work for 99% of cases. Those who experience the unexpected can use raw: false. I would make a note about this in the docs.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
dafflcommented, Aug 16, 2017

I’m starting to feel that at least with Sequelize we seem to be having just as many issues with raw: true as we had with raw: false.

0reactions
dafflcommented, Apr 10, 2019

This is actually not an issue anymore and the solution has been documented in the working with model instances section

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fastest way to flatten / un-flatten nested JavaScript objects
I wrote two functions to flatten and unflatten a JSON object. ... function unflatten(table) { var result = {}; for (var path in...
Read more >
Data Flattening and Data Unflattening | Firebolt Gloassary
Data flattening usually refers to the act of flattening semi-structured data, such as name-value pairs in JSON, into separate columns where the name...
Read more >
How to Unflatten a JSON Object with JavaScript?
To unflatten a flattened JavaScript object, we can split each property path and add the nested properties into the unflatten object.
Read more >
How to Unpivot Data in Excel using Power Query (aka Get ...
Unpivot Data Using Power Query · Select any cell in the dataset. · Go to the Insert Tab. · Click on the Table...
Read more >
[Solved]-How do I flatten results from my SQL Query?-oracle
SELECT t.Year, MAX(t.IsFunded) AS IsFunded, MAX(t.NotFunded) AS NotFunded FROM ( --myQuery that returns unflattened results ) AS t GROUP BY t.Year;.
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