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.

[Suggestion] JSON output for .find() with raw & include

See original GitHub issue

What you are doing?

Use .find() with raw & include.

Post.findAll({
  raw: true,
  include: [Tag]
});

What do you expect to happen?

{id: 1, ..., Tag: {id: 1, ...}}

What is actually happening?

{id: 1, ..., 'Tag.id': 1, ...}}

Suggestion

As postgres 9.2 added row_to_json function, raw query can be constructed as:

SELECT "Posts".*, row_to_json("Tags".*) AS "Tag" FROM "Posts" INNER JOIN "Tags" ON ("Posts".tid = "Tags".id);

which give the expected JSON result and should improve .find() performance.

Related issues: #1830 #3682 #3885 #4311 #5685 #5708 #6122

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:18
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tomoatcommented, Mar 30, 2022
Post.findAll({
  raw: true,
  nest: true,
  include: [Tag]
});

This problem should be as early does not exist

0reactions
dossincommented, Sep 16, 2020

For me on MySQL, it will be a major performance boost. Hope you will address it.

It seems that all major DB have this feature now. JS is slow compare to native DB function.

A first step could be to just use JSON_OBJECT at first level to give the the same results as now but on the DB side.

Exemple : SELECT JSON_OBJECT( ‘lastName’, lastName, ‘firstName’, firstName ) FROM users;

=>

[{“lastName”: “Cell”, “firstName”: “Ance”} {“lastName”: “Laure”, “firstName”: “Varner”}]

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I include raw JSON in an object using Jackson?
Object', since this should work ok: for serialization, String will be output as is, and for deserialization, it will be deserialized as a...
Read more >
JSON methods, toJSON
The JSON (JavaScript Object Notation) is a general format to represent values and objects. It is described as in RFC 4627 standard.
Read more >
JSON formatting with jq and CI/CD linting automation | GitLab
Learn how to filter in JSON data structures and interact with the REST API. Use the GitLab API to lint your CI/CD configuration...
Read more >
Format Query Results as JSON with FOR JSON - SQL Server
Use the FOR JSON clause to simplify client applications by delegating the formatting of JSON output from the app to SQL Server.
Read more >
Reshaping JSON with jq
After each query in this lesson, I will include the first few lines of the expected results, so that you can check your...
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