mirror of
https://github.com/logto-io/logto.git
synced 2025-03-31 22:51:25 -05:00
feat(core): set roles pagination to optional (#2865)
This commit is contained in:
parent
d675cf26a8
commit
8b179c0567
2 changed files with 12 additions and 6 deletions
|
@ -33,7 +33,7 @@ export const countRoles = async (search: Search = defaultUserSearch) =>
|
|||
${buildRoleConditions(search)}
|
||||
`);
|
||||
|
||||
export const findRoles = async (limit: number, offset: number, search: Search) =>
|
||||
export const findRoles = async (search: Search, limit?: number, offset?: number) =>
|
||||
envSet.pool.any<Role>(
|
||||
sql`
|
||||
select ${sql.join(
|
||||
|
@ -42,8 +42,8 @@ export const findRoles = async (limit: number, offset: number, search: Search) =
|
|||
)}
|
||||
from ${table}
|
||||
${buildRoleConditions(search)}
|
||||
limit ${limit}
|
||||
offset ${offset}
|
||||
${conditionalSql(limit, (value) => sql`limit ${value}`)}
|
||||
${conditionalSql(offset, (value) => sql`offset ${value}`)}
|
||||
`
|
||||
);
|
||||
|
||||
|
|
|
@ -38,17 +38,23 @@ import type { AuthedRouter } from './types.js';
|
|||
const roleId = buildIdGenerator(21);
|
||||
|
||||
export default function roleRoutes<T extends AuthedRouter>(router: T) {
|
||||
router.get('/roles', koaPagination(), async (ctx, next) => {
|
||||
const { limit, offset } = ctx.pagination;
|
||||
router.get('/roles', koaPagination({ isOptional: true }), async (ctx, next) => {
|
||||
const { limit, offset, disabled } = ctx.pagination;
|
||||
const { searchParams } = ctx.request.URL;
|
||||
|
||||
return tryThat(
|
||||
async () => {
|
||||
const search = parseSearchParamsForSearch(searchParams);
|
||||
|
||||
if (disabled) {
|
||||
ctx.body = await findRoles(search);
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
const [{ count }, roles] = await Promise.all([
|
||||
countRoles(search),
|
||||
findRoles(limit, offset, search),
|
||||
findRoles(search, limit, offset),
|
||||
]);
|
||||
|
||||
// Return totalCount to pagination middleware
|
||||
|
|
Loading…
Add table
Reference in a new issue