Open rastym opened 7 months ago
I have been exploring this feature in TypeSQL (a similar library but for mysql). The approach I have used is to annotate the queries with -- @nested
as below:
-- @nested
SELECT
*
FROM users u
INNER JOIN posts p on p.user_id = u.id
WHERE u.id = :id
Then TypeSQL will generate the function selectUserPostsNested
:
const user = await selectUserPostsNested(conn, {id: 1});
The result type will have the structure:
{ id: 1, username: 'user1', email: 'user1@example.com', posts: [{ title: 'Post 1', content: 'Content of post 1', user_id: 1 }] }
You can either use:
@wsporto , @lorefnon thanks for your response!
For example, let's consider the following structure. If we perform a JOIN on these tables, we'll end up with two records for one user.
How can we process the data to obtain such a structure?:
User { id, username, email, posts: [{ title: 'Post 1', content: 'Content of post 1', user_id: 1 }] }
Of course, we can write parsers each time, use helper functions from lodash, but is there a more fundamental approach? Thank you!