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 { SchemaValuePrimitive, SchemaValue } from '@logto/schemas';
|
||||||
import { Falsy, notFalsy } from '@silverhand/essentials';
|
import { Falsy, notFalsy } from '@silverhand/essentials';
|
||||||
import dayjs from 'dayjs';
|
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';
|
import { FieldIdentifiers, Table } from './types';
|
||||||
|
|
||||||
export const conditionalSql = <T>(
|
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 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 { Application, ApplicationDBEntry, Applications } from '@logto/schemas';
|
||||||
import { sql } from 'slonik';
|
import { sql } from 'slonik';
|
||||||
|
|
||||||
|
import { buildFindMany } from '@/database/find-many';
|
||||||
import { buildInsertInto } from '@/database/insert-into';
|
import { buildInsertInto } from '@/database/insert-into';
|
||||||
import pool from '@/database/pool';
|
import pool from '@/database/pool';
|
||||||
import { buildUpdateWhere } from '@/database/update-where';
|
import { buildUpdateWhere } from '@/database/update-where';
|
||||||
import { convertToIdentifiers, OmitAutoSetFields } from '@/database/utils';
|
import { convertToIdentifiers, OmitAutoSetFields, getTotalRowCount } from '@/database/utils';
|
||||||
import RequestError from '@/errors/RequestError';
|
import RequestError from '@/errors/RequestError';
|
||||||
|
|
||||||
const { table, fields } = convertToIdentifiers(Applications);
|
const { table, fields } = convertToIdentifiers(Applications);
|
||||||
|
|
||||||
export const findAllApplications = async () =>
|
export const findTotalNumberOfApplications = async () => getTotalRowCount(table);
|
||||||
pool.many<Application>(sql`
|
|
||||||
select ${sql.join(Object.values(fields), sql`, `)}
|
const findApplicationMany = buildFindMany<ApplicationDBEntry, Application>(pool, Applications);
|
||||||
from ${table}
|
|
||||||
`);
|
export const findAllApplications = async (limit: number, offset: number) =>
|
||||||
|
findApplicationMany({ limit, offset });
|
||||||
|
|
||||||
export const findApplicationById = async (id: string) =>
|
export const findApplicationById = async (id: string) =>
|
||||||
pool.one<Application>(sql`
|
pool.one<Application>(sql`
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { Applications } from '@logto/schemas';
|
||||||
import { object, string } from 'zod';
|
import { object, string } from 'zod';
|
||||||
|
|
||||||
import koaGuard from '@/middleware/koa-guard';
|
import koaGuard from '@/middleware/koa-guard';
|
||||||
|
import koaPagination from '@/middleware/koa-pagination';
|
||||||
import { buildOidcClientMetadata } from '@/oidc/utils';
|
import { buildOidcClientMetadata } from '@/oidc/utils';
|
||||||
import {
|
import {
|
||||||
deleteApplicationById,
|
deleteApplicationById,
|
||||||
|
@ -9,6 +10,7 @@ import {
|
||||||
findAllApplications,
|
findAllApplications,
|
||||||
insertApplication,
|
insertApplication,
|
||||||
updateApplicationById,
|
updateApplicationById,
|
||||||
|
findTotalNumberOfApplications,
|
||||||
} from '@/queries/application';
|
} from '@/queries/application';
|
||||||
import { buildIdGenerator } from '@/utils/id';
|
import { buildIdGenerator } from '@/utils/id';
|
||||||
|
|
||||||
|
@ -17,8 +19,18 @@ import { AuthedRouter } from './types';
|
||||||
const applicationId = buildIdGenerator(21);
|
const applicationId = buildIdGenerator(21);
|
||||||
|
|
||||||
export default function applicationRoutes<T extends AuthedRouter>(router: T) {
|
export default function applicationRoutes<T extends AuthedRouter>(router: T) {
|
||||||
router.get('/applications', async (ctx, next) => {
|
router.get('/applications', koaPagination(), async (ctx, next) => {
|
||||||
ctx.body = await findAllApplications();
|
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();
|
return next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue