diff options
| author | Mohamed Bassem <me@mbassem.com> | 2024-12-22 16:17:22 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2024-12-22 23:38:38 +0000 |
| commit | 353e5d6374c77a2744590a5bfd3329672009b281 (patch) | |
| tree | 5a64f13ddc930fbcf699101248b35f72cda6fc6b /packages/trpc | |
| parent | 71d7490d0a647e9254bf15b38201177057d88f95 (diff) | |
| download | karakeep-353e5d6374c77a2744590a5bfd3329672009b281.tar.zst | |
feat: Add basic pagination to searchBookmarks tRPC
Diffstat (limited to 'packages/trpc')
| -rw-r--r-- | packages/trpc/routers/bookmarks.ts | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index 9708706a..8a4170cd 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -515,9 +515,25 @@ export const bookmarksAppRouter = router({ .input( z.object({ text: z.string(), + cursor: z + .object({ + offset: z.number(), + limit: z.number(), + }) + .nullish(), + }), + ) + .output( + z.object({ + bookmarks: z.array(zBookmarkSchema), + nextCursor: z + .object({ + offset: z.number(), + limit: z.number(), + }) + .nullable(), }), ) - .output(zGetBookmarksResponseSchema) .query(async ({ input, ctx }) => { const client = await getSearchIdxClient(); if (!client) { @@ -531,6 +547,12 @@ export const bookmarksAppRouter = router({ showRankingScore: true, attributesToRetrieve: ["id"], sort: ["createdAt:desc"], + ...(input.cursor + ? { + offset: input.cursor.offset, + limit: input.cursor.limit, + } + : {}), }); if (resp.hits.length == 0) { @@ -562,7 +584,16 @@ export const bookmarksAppRouter = router({ }); results.sort((a, b) => idToRank[b.id] - idToRank[a.id]); - return { bookmarks: results.map(toZodSchema), nextCursor: null }; + return { + bookmarks: results.map(toZodSchema), + nextCursor: + resp.hits.length + resp.offset >= resp.estimatedTotalHits + ? null + : { + offset: resp.hits.length + resp.offset, + limit: resp.limit, + }, + }; }), getBookmarks: authedProcedure .input(zGetBookmarksRequestSchema) |
