-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathlist_document.sql
More file actions
28 lines (28 loc) · 888 Bytes
/
list_document.sql
File metadata and controls
28 lines (28 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
SELECT * from (
SELECT
"document".*,
to_json("document"."meta") as meta,
to_json("document"."status_meta") as status_meta,
(SELECT "count"("id") FROM "paragraph" WHERE document_id = "document"."id") as "paragraph_count",
tag_agg.tag_count as "tag_count",
COALESCE(tag_agg.tags, '[]'::json) as "tags"
FROM
"document" "document"
LEFT JOIN LATERAL (
SELECT
COUNT(*)::int as tag_count,
json_agg(
json_build_object(
'id', "tag"."id",
'key', "tag"."key",
'value', "tag"."value"
)
ORDER BY "tag"."key", "tag"."value"
) as tags
FROM "document_tag" "document_tag"
INNER JOIN "tag" "tag" ON "tag"."id" = "document_tag"."tag_id"
WHERE "document_tag"."document_id" = "document"."id"
) tag_agg ON TRUE
${document_custom_sql}
) temp
${order_by_query}