mirror of
https://github.com/umami-software/umami.git
synced 2026-02-06 13:47:15 +01:00
add paging for relational raw query
This commit is contained in:
parent
04e0b33622
commit
0220091cff
3 changed files with 107 additions and 37 deletions
|
|
@ -261,6 +261,32 @@ async function pagedQuery<T>(model: string, criteria: T, pageParams: PageParams)
|
|||
return { data, count, page: +page, pageSize: size, orderBy };
|
||||
}
|
||||
|
||||
async function pagedRawQuery(
|
||||
query: string,
|
||||
queryParams: { [key: string]: any },
|
||||
pageParams: PageParams = {},
|
||||
) {
|
||||
const { page = 1, pageSize, orderBy, sortDescending = false } = pageParams;
|
||||
const size = +pageSize || DEFAULT_PAGE_SIZE;
|
||||
const offset = +size * (page - 1);
|
||||
const direction = sortDescending ? 'desc' : 'asc';
|
||||
|
||||
const statements = [
|
||||
orderBy && `order by ${orderBy} ${direction}`,
|
||||
+size > 0 && `limit ${+size} offset ${offset}`,
|
||||
]
|
||||
.filter(n => n)
|
||||
.join('\n');
|
||||
|
||||
const count = await rawQuery(`select count(*) as num from (${query}) t`, queryParams).then(
|
||||
res => res[0].num,
|
||||
);
|
||||
|
||||
const data = await rawQuery(`${query}${statements}`, queryParams);
|
||||
|
||||
return { data, count, page: +page, pageSize: size, orderBy };
|
||||
}
|
||||
|
||||
function getQueryMode(): { mode?: Prisma.QueryMode } {
|
||||
const db = getDatabaseType();
|
||||
|
||||
|
|
@ -311,6 +337,7 @@ export default {
|
|||
getSearchSQL,
|
||||
getQueryMode,
|
||||
pagedQuery,
|
||||
pagedRawQuery,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue