Unflatten results of raw queries
See original GitHub issueSequelize 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:
- 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.
- 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:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 withraw: false
.This is actually not an issue anymore and the solution has been documented in the working with model instances section