Skip to content

Commit 2182efe

Browse files
committed
fix(api): remove duplicates in contents
1 parent 3677a62 commit 2182efe

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/main/api/routes/snippets.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,21 @@ app
6666
const whereCondition = WHERE.length ? `WHERE ${WHERE.join(' AND ')}` : ''
6767

6868
const stmt = db.prepare(`
69-
WITH snippet_data AS (
69+
WITH snippet_contents_data AS (
70+
SELECT
71+
snippetId,
72+
json_group_array(
73+
json_object(
74+
'id', id,
75+
'label', label,
76+
'value', value,
77+
'language', language
78+
)
79+
) as contents
80+
FROM snippet_contents
81+
GROUP BY snippetId
82+
),
83+
snippet_data AS (
7084
SELECT
7185
s.id,
7286
s.name,
@@ -88,19 +102,12 @@ app
88102
'name', t.name
89103
)
90104
) FILTER (WHERE t.id IS NOT NULL) as tags,
91-
json_group_array(
92-
json_object(
93-
'id', sc.id,
94-
'label', sc.label,
95-
'value', sc.value,
96-
'language', sc.language
97-
)
98-
) FILTER (WHERE sc.id IS NOT NULL) as contents
105+
COALESCE(scd.contents, '[]') as contents
99106
FROM snippets s
100107
LEFT JOIN folders f ON s.folderId = f.id
101108
LEFT JOIN snippet_tags st ON s.id = st.snippetId
102109
LEFT JOIN tags t ON st.tagId = t.id
103-
LEFT JOIN snippet_contents sc ON s.id = sc.snippetId
110+
LEFT JOIN snippet_contents_data scd ON s.id = scd.snippetId
104111
${whereCondition}
105112
GROUP BY s.id
106113
)

0 commit comments

Comments
 (0)