Comments queries

Helper that turns a comments table into a threaded list query for one (resource, row). Returns a SQLAlchemy Select and registers nothing on metadata — run it directly against a connection and feed the rows to fsh_lib.comments.build_comment_thread() to nest them.

Pair with fsh_lib.comments.CommentMixin for the storage columns (resource_type, resource_id, parent_comment_id, body, author_id).

What is exposed

One function, re-exported at codegen_database.ext.comments:

How to use

from sqlalchemy import create_engine
from codegen_database.ext.comments import construct_comments_thread_query
from fsh_lib.comments import build_comment_thread

engine = create_engine(DATABASE_URL)
with engine.connect() as conn:
    rows = conn.execute(
        construct_comments_thread_query(
            Comment.__table__,
            resource_type="category",
            resource_id=str(category.id),
        )
    ).scalars().all()
    tree = build_comment_thread(rows)

The query filters to the one (resource_type, resource_id) and orders top-level comments (parent_comment_id IS NULL) ahead of replies, each group by created_at. fsh_lib.comments.build_comment_thread() nests the flat stream from an id-keyed dict, so the only ordering that matters for the tree is the order of a parent’s replies — which the sort keeps chronological. No recursive CTE for the common one-level-deep thread shape; add one if arbitrary-depth nesting is needed.

Full signature lives in the API reference reference.