Skip to content

Instantly share code, notes, and snippets.

@renatosousafilho
Created November 18, 2020 22:10
Show Gist options
  • Save renatosousafilho/8d96663f2fe50481ceb13cc137560a04 to your computer and use it in GitHub Desktop.
Save renatosousafilho/8d96663f2fe50481ceb13cc137560a04 to your computer and use it in GitHub Desktop.
Sql Join com XDEVAPI
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