1- import { v , Infer } from "convex/values" ;
1+ import { v , Infer , Validator } from "convex/values" ;
22import { internalMutation , mutation , query , QueryCtx } from "./_generated/server" ;
33import { paginator } from "convex-helpers/server/pagination" ;
44import schema from "./schema.js" ;
@@ -85,16 +85,23 @@ export const update = mutation({
8585 } ,
8686} ) ;
8787
88+ function paginationResultValidator < T > ( itemValidator : Validator < T , "required" , string > ) {
89+ return v . object ( {
90+ continueCursor : v . string ( ) ,
91+ isDone : v . boolean ( ) ,
92+ page : v . array ( itemValidator ) ,
93+ pageStatus : v . optional ( v . union ( v . null ( ) , v . literal ( "SplitRequired" ) , v . literal ( "SplitRecommended" ) ) ) ,
94+ splitCursor : v . optional ( v . union ( v . null ( ) , v . string ( ) ) ) ,
95+ } ) ;
96+ }
97+ export type PaginationResult < T > = Infer < ReturnType < typeof paginationResultValidator < T > > > ;
98+
8899export const listHistory = query ( {
89100 args : {
90101 maxTs : v . number ( ) ,
91102 paginationOpts : paginationOptsValidator ,
92103 } ,
93- returns : v . object ( {
94- continueCursor : v . string ( ) ,
95- isDone : v . boolean ( ) ,
96- page : v . array ( historyEntryValidator ) ,
97- } ) ,
104+ returns : paginationResultValidator ( historyEntryValidator ) ,
98105 handler : async ( ctx , args ) => {
99106 const results = await paginator ( ctx . db , schema )
100107 . query ( "history" )
@@ -124,11 +131,7 @@ export const listDocumentHistory = query({
124131 maxTs : v . number ( ) ,
125132 paginationOpts : paginationOptsValidator ,
126133 } ,
127- returns : v . object ( {
128- continueCursor : v . string ( ) ,
129- isDone : v . boolean ( ) ,
130- page : v . array ( historyEntryValidator ) ,
131- } ) ,
134+ returns : paginationResultValidator ( historyEntryValidator ) ,
132135 handler : async ( ctx , args ) => {
133136 const results = await paginator ( ctx . db , schema )
134137 . query ( "history" )
@@ -151,13 +154,7 @@ export const listSnapshot = query({
151154 currentTs : v . number ( ) ,
152155 paginationOpts : paginationOptsValidator ,
153156 } ,
154- returns : v . object ( {
155- continueCursor : v . string ( ) ,
156- isDone : v . boolean ( ) ,
157- page : v . array ( historyEntryValidator ) ,
158- splitCursor : v . optional ( v . string ( ) ) ,
159- pageStatus : v . optional ( v . literal ( "SplitRecommended" ) ) ,
160- } ) ,
157+ returns : paginationResultValidator ( historyEntryValidator ) ,
161158 handler : async ( ctx , args ) => {
162159 const pageSize = args . paginationOpts . numItems ;
163160 const page : HistoryEntry [ ] = [ ] ;
@@ -234,12 +231,13 @@ export const listSnapshot = query({
234231 page . push ( extractHistoryEntry ( revision ) ) ;
235232 }
236233 }
237- return {
234+ const output : PaginationResult < HistoryEntry > = {
238235 continueCursor : allIdsBeforeCurrentTs [ allIdsBeforeCurrentTs . length - 1 ] ,
239236 isDone : false ,
240237 page,
241238 ...maybeSplit ( allIdsSeen , pageSize ) ,
242239 } ;
240+ return output ;
243241 } ,
244242} ) ;
245243
0 commit comments