NORY-45: prepare custom query for pagination

This commit is contained in:
Markus Thielker 2025-01-04 13:30:14 +01:00
parent 5e01a7f50f
commit 2d2c021446
No known key found for this signature in database
2 changed files with 27 additions and 7 deletions

View file

@ -16,9 +16,9 @@ export default async function UserPage(
const page = params.page ? Number(params.page) : 1;
const query = params.query ? params.query as string : '';
let pageSize = 250;
let pageSize = 50;
const initialData = await queryIdentities({ page, pageSize, query });
const { data, pageCount } = await queryIdentities({ page, pageSize, query });
return (
<div className="space-y-4">
@ -34,7 +34,7 @@ export default async function UserPage(
queryParamKey="query"
placeholder="Search for identifiers (Email, Username...)"/>
<IdentityDataTable
data={initialData}
data={data}
page={page}
query={query}/>
</div>

View file

@ -15,14 +15,14 @@ import { eq, ilike, or, sql } from 'drizzle-orm';
interface QueryIdentitiesProps {
page: number,
pageSize?: number,
pageSize: number,
query?: string,
}
export async function queryIdentities({ page, pageSize, query }: QueryIdentitiesProps) {
const db = await getDB();
const results = await db.select()
const result = await db.select()
.from(identities)
.leftJoin(identityVerifiableAddresses, eq(identities.id, identityVerifiableAddresses.identityId))
.leftJoin(identityRecoveryAddresses, eq(identities.id, identityRecoveryAddresses.identityId))
@ -30,14 +30,34 @@ export async function queryIdentities({ page, pageSize, query }: QueryIdentities
sql`${identities.traits}::text ILIKE
${`%${query}%`}`,
ilike(identityVerifiableAddresses.value, `%${query}%`),
));
))
.limit(pageSize)
.offset((page - 1) * pageSize);
return results.map((it) => {
const resultCount = await db.$count(
db.select()
.from(identities)
.leftJoin(identityVerifiableAddresses, eq(identities.id, identityVerifiableAddresses.identityId))
.leftJoin(identityRecoveryAddresses, eq(identities.id, identityRecoveryAddresses.identityId))
.where(or(
sql`${identities.traits}::text ILIKE
${`%${query}%`}`,
ilike(identityVerifiableAddresses.value, `%${query}%`),
))
.as('subquery'),
);
const resultTyped = result.map((it) => {
const typed = it.identities as unknown as Identity;
typed.verifiable_addresses = [it.identity_verifiable_addresses] as unknown as VerifiableIdentityAddress[];
typed.recovery_addresses = [it.identity_verifiable_addresses] as unknown as RecoveryIdentityAddress[];
return typed;
});
return {
data: resultTyped,
pageCount: Math.ceil(resultCount / pageSize),
};
}