HN-like selfhosted forums
-- name: GetCommentsByPost :manySELECT c.*, u.username AS author_usernameFROM comments cJOIN users u ON u.id = c.author_idWHERE c.post_id = $1ORDER BY c.created_at DESC, c.id ASC;-- name: CreateComment :oneINSERT INTO comments(post_id, parent_id, author_id, body)SELECT $1, $2, $3, $4 -- creates a custom row hereWHERE $2::BIGINT IS NULL -- stop here if parent commentOR EXISTS ( SELECT 1 -- discard, returns true if succeeds FROM comments WHERE id = $2::BIGINT -- check if the parent comment exists on the post AND post_id = $1)RETURNING *;