feat(app): add search and pagination (#1860)
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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;
|
||||
}>;
|
||||
|
||||
@@ -489,6 +489,7 @@
|
||||
}
|
||||
},
|
||||
"app": {
|
||||
"search": "Find an app",
|
||||
"page": {
|
||||
"list": {
|
||||
"title": "Apps",
|
||||
|
||||
Reference in New Issue
Block a user