mirror of
https://github.com/logto-io/logto.git
synced 2025-02-17 22:04:19 -05:00
feat(applications): add pagination middleware to findAllApplications (#170)
* feat(applications): add pagination middleware to findAllApllications add pagination middleware to findAllApllications * fix(core): remove get applications offset check remove get applications offset check. use db query error * fix(application): cr fix use buildFindMany use buildFindMany for paginated find applications query
This commit is contained in:
parent
c37354d42d
commit
3690265122
3 changed files with 30 additions and 9 deletions
|
@ -1,8 +1,9 @@
|
|||
import { SchemaValuePrimitive, SchemaValue } from '@logto/schemas';
|
||||
import { Falsy, notFalsy } from '@silverhand/essentials';
|
||||
import dayjs from 'dayjs';
|
||||
import { sql, SqlSqlTokenType, SqlTokenType } from 'slonik';
|
||||
import { sql, SqlSqlTokenType, SqlTokenType, IdentifierSqlTokenType } from 'slonik';
|
||||
|
||||
import pool from './pool';
|
||||
import { FieldIdentifiers, Table } from './types';
|
||||
|
||||
export const conditionalSql = <T>(
|
||||
|
@ -70,3 +71,9 @@ export const convertToIdentifiers = <T extends Table>(
|
|||
});
|
||||
|
||||
export const convertToTimestamp = (time = dayjs()) => sql`to_timestamp(${time.valueOf() / 1000})`;
|
||||
|
||||
export const getTotalRowCount = async (table: IdentifierSqlTokenType) =>
|
||||
pool.one<{ count: number }>(sql`
|
||||
select count(*)
|
||||
from ${table}
|
||||
`);
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
import { Application, ApplicationDBEntry, Applications } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildFindMany } from '@/database/find-many';
|
||||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import pool from '@/database/pool';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
import { convertToIdentifiers, OmitAutoSetFields } from '@/database/utils';
|
||||
import { convertToIdentifiers, OmitAutoSetFields, getTotalRowCount } from '@/database/utils';
|
||||
import RequestError from '@/errors/RequestError';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(Applications);
|
||||
|
||||
export const findAllApplications = async () =>
|
||||
pool.many<Application>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
`);
|
||||
export const findTotalNumberOfApplications = async () => getTotalRowCount(table);
|
||||
|
||||
const findApplicationMany = buildFindMany<ApplicationDBEntry, Application>(pool, Applications);
|
||||
|
||||
export const findAllApplications = async (limit: number, offset: number) =>
|
||||
findApplicationMany({ limit, offset });
|
||||
|
||||
export const findApplicationById = async (id: string) =>
|
||||
pool.one<Application>(sql`
|
||||
|
|
|
@ -2,6 +2,7 @@ import { Applications } from '@logto/schemas';
|
|||
import { object, string } from 'zod';
|
||||
|
||||
import koaGuard from '@/middleware/koa-guard';
|
||||
import koaPagination from '@/middleware/koa-pagination';
|
||||
import { buildOidcClientMetadata } from '@/oidc/utils';
|
||||
import {
|
||||
deleteApplicationById,
|
||||
|
@ -9,6 +10,7 @@ import {
|
|||
findAllApplications,
|
||||
insertApplication,
|
||||
updateApplicationById,
|
||||
findTotalNumberOfApplications,
|
||||
} from '@/queries/application';
|
||||
import { buildIdGenerator } from '@/utils/id';
|
||||
|
||||
|
@ -17,8 +19,18 @@ import { AuthedRouter } from './types';
|
|||
const applicationId = buildIdGenerator(21);
|
||||
|
||||
export default function applicationRoutes<T extends AuthedRouter>(router: T) {
|
||||
router.get('/applications', async (ctx, next) => {
|
||||
ctx.body = await findAllApplications();
|
||||
router.get('/applications', koaPagination(), async (ctx, next) => {
|
||||
const { limit, offset } = ctx.pagination;
|
||||
|
||||
const [{ count }, applications] = await Promise.all([
|
||||
findTotalNumberOfApplications(),
|
||||
findAllApplications(limit, offset),
|
||||
]);
|
||||
|
||||
// Return totalCount to pagination middleware
|
||||
ctx.pagination.totalCount = count;
|
||||
ctx.body = applications;
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue