feat(app): add search and pagination (#1860)

This commit is contained in:
Meier Lukas
2025-01-04 23:06:34 +01:00
committed by GitHub
parent bf12944768
commit ccb19e06c1
7 changed files with 60 additions and 18 deletions

View File

@@ -10,6 +10,26 @@ import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure, publ
import { canUserSeeAppAsync } from "./app/app-access-control";
export const appRouter = createTRPCRouter({
getPaginated: protectedProcedure
.input(validation.common.paginated)
.output(z.object({ items: z.array(selectAppSchema), totalCount: z.number() }))
.meta({ openapi: { method: "GET", path: "/api/apps/paginated", tags: ["apps"], protect: true } })
.query(async ({ input, ctx }) => {
const whereQuery = input.search ? like(apps.name, `%${input.search.trim()}%`) : undefined;
const totalCount = await ctx.db.$count(apps, whereQuery);
const dbApps = await ctx.db.query.apps.findMany({
limit: input.pageSize,
offset: (input.page - 1) * input.pageSize,
where: whereQuery,
orderBy: asc(apps.name),
});
return {
items: dbApps,
totalCount,
};
}),
all: protectedProcedure
.input(z.void())
.output(z.array(selectAppSchema))

View File

@@ -1,3 +1,5 @@
import type { z } from "zod";
export type MaybePromise<T> = T | Promise<T>;
export type AtLeastOneOf<T> = [T, ...T[]];
@@ -16,3 +18,11 @@ export type Inverse<T extends Invertible> = {
};
type Invertible = Record<PropertyKey, PropertyKey>;
export type inferSearchParamsFromSchema<TSchema extends z.AnyZodObject> = inferSearchParamsFromSchemaInner<
z.infer<TSchema>
>;
type inferSearchParamsFromSchemaInner<TSchema extends Record<string, unknown>> = Partial<{
[K in keyof TSchema]: Exclude<TSchema[K], undefined> extends unknown[] ? string[] : string;
}>;

View File

@@ -489,6 +489,7 @@
}
},
"app": {
"search": "Find an app",
"page": {
"list": {
"title": "Apps",