Skip to content

Commit 20abb59

Browse files
ulemonsclaude
andcommitted
fix: return correct total when pagination goes beyond last page (CM-1220)
When a request page has no rows, COUNT(*) OVER() returns nothing and total fell back to 0. Add a fallback COUNT query for this case so the caller always receives the real matching-row count. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>
1 parent 90f5681 commit 20abb59

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

  • services/libs/data-access-layer/src/osspckgs

services/libs/data-access-layer/src/osspckgs/api.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export async function listPackagesForApi(
103103
else sortExpr = 'LOWER(p.name)'
104104
const sortDir = opts.sortDir === 'desc' ? 'DESC' : 'ASC'
105105

106+
const filterParams = { ...params }
106107
params.limit = opts.pageSize
107108
params.offset = (opts.page - 1) * opts.pageSize
108109

@@ -129,7 +130,19 @@ export async function listPackagesForApi(
129130
params,
130131
)
131132

132-
const total = rows.length > 0 ? parseInt(rows[0].total, 10) : 0
133+
let total: number
134+
if (rows.length > 0) {
135+
total = parseInt(rows[0].total, 10)
136+
} else {
137+
// Window function returns no rows when the page is beyond the result set.
138+
// Fall back to a separate COUNT so the caller always gets the real total.
139+
const countRow: { count: string } = await qx.selectOne(
140+
`SELECT COUNT(*)::text AS count FROM packages p LEFT JOIN stewardships s ON s.package_id = p.id ${where}`,
141+
filterParams,
142+
)
143+
total = parseInt(countRow.count, 10)
144+
}
145+
133146
return { rows, total }
134147
}
135148

0 commit comments

Comments
 (0)