Created
November 18, 2020 22:10
-
-
Save renatosousafilho/8d96663f2fe50481ceb13cc137560a04 to your computer and use it in GitHub Desktop.
Sql Join com XDEVAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getPostWithCommentsById = async (postId) => { | |
const session = await connection(); | |
const result = await session.sql( | |
`SELECT c.title as title_comment, p.title as title_post | |
FROM comments AS c | |
INNER JOIN posts AS p ON p.id = c.post_id | |
WHERE p.id=?, | |
) | |
.bind(postId) | |
.bind(postId) | |
.execute() | |
.then((results) => results.fetchAll()) | |
.then((comments) => comments.map( | |
([title_comment, title_post]) => ({ title_comment, title_post }), | |
)); | |
if (!result.length) return null; | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment