list_document.sql 888 B

12345678910111213141516171819202122232425262728
  1. SELECT * from (
  2. SELECT
  3. "document".*,
  4. to_json("document"."meta") as meta,
  5. to_json("document"."status_meta") as status_meta,
  6. (SELECT "count"("id") FROM "paragraph" WHERE document_id = "document"."id") as "paragraph_count",
  7. tag_agg.tag_count as "tag_count",
  8. COALESCE(tag_agg.tags, '[]'::json) as "tags"
  9. FROM
  10. "document" "document"
  11. LEFT JOIN LATERAL (
  12. SELECT
  13. COUNT(*)::int as tag_count,
  14. json_agg(
  15. json_build_object(
  16. 'id', "tag"."id",
  17. 'key', "tag"."key",
  18. 'value', "tag"."value"
  19. )
  20. ORDER BY "tag"."key", "tag"."value"
  21. ) as tags
  22. FROM "document_tag" "document_tag"
  23. INNER JOIN "tag" "tag" ON "tag"."id" = "document_tag"."tag_id"
  24. WHERE "document_tag"."document_id" = "document"."id"
  25. ) tag_agg ON TRUE
  26. ${document_custom_sql}
  27. ) temp
  28. ${order_by_query}