1
- import { and , eq , inArray , type InferSelectModel } from 'drizzle-orm'
1
+ import { and , count , eq , inArray , type InferSelectModel } from 'drizzle-orm'
2
2
import { compact , keyBy } from 'es-toolkit'
3
3
import { getContextData } from 'waku/middleware/context'
4
4
import { romTable } from '../databases/library/schema.ts'
@@ -39,7 +39,16 @@ async function getMetadata(romResults: InferSelectModel<typeof romTable>[]) {
39
39
return results
40
40
}
41
41
42
- export async function getRoms ( { id, platform } : { id ?: string ; platform ?: string } = { } ) {
42
+ type GetRomsReturning = Awaited < ReturnType < typeof getRoms > >
43
+ export type Roms = GetRomsReturning [ 'roms' ]
44
+ export type RomsPagination = GetRomsReturning [ 'pagination' ]
45
+
46
+ export async function getRoms ( {
47
+ id,
48
+ page = 1 ,
49
+ pageSize = 100 ,
50
+ platform,
51
+ } : { id ?: string ; page ?: number ; pageSize ?: number ; platform ?: string } = { } ) {
43
52
const { currentUser, db } = getContextData ( )
44
53
const { library } = db
45
54
@@ -51,8 +60,19 @@ export async function getRoms({ id, platform }: { id?: string; platform?: string
51
60
conditions . push ( eq ( romTable . platform , platform ) )
52
61
}
53
62
const where = and ( ...conditions )
54
- const romResults = await library . select ( ) . from ( romTable ) . orderBy ( romTable . file_name ) . where ( where ) . limit ( 100 )
63
+
64
+ const offset = ( page - 1 ) * pageSize
65
+ const romResults = await library
66
+ . select ( )
67
+ . from ( romTable )
68
+ . orderBy ( romTable . file_name )
69
+ . where ( where )
70
+ . offset ( offset )
71
+ . limit ( pageSize )
72
+
73
+ const [ { total } ] = await library . select ( { total : count ( ) } ) . from ( romTable ) . orderBy ( romTable . file_name ) . where ( where )
55
74
56
75
const results = await getMetadata ( romResults )
57
- return results
76
+
77
+ return { pagination : { current : page , pages : Math . ceil ( total / pageSize ) , size : pageSize , total } , roms : results }
58
78
}
0 commit comments