|
1 |
| -import { and, count, eq, inArray, type InferSelectModel } from 'drizzle-orm' |
2 |
| -import { compact, keyBy } from 'es-toolkit' |
| 1 | +import { and, count, eq } from 'drizzle-orm' |
3 | 2 | import { getContextData } from 'waku/middleware/context'
|
4 | 3 | import { romTable } from '../databases/library/schema.ts'
|
5 |
| -import { launchboxGameTable, libretroGameTable } from '../databases/metadata/schema.ts' |
6 |
| - |
7 |
| -async function getMetadata(romResults: InferSelectModel<typeof romTable>[]) { |
8 |
| - const { db } = getContextData() |
9 |
| - const { metadata } = db |
10 |
| - const launchboxGameIds = compact(romResults.map((romResult) => romResult.launchbox_game_id)) |
11 |
| - const launchboxResults = await metadata |
12 |
| - .select() |
13 |
| - .from(launchboxGameTable) |
14 |
| - .where(inArray(launchboxGameTable.database_id, launchboxGameIds)) |
15 |
| - const launchboxResultMap = keyBy(launchboxResults, (launchboxResult) => launchboxResult.database_id) |
16 |
| - |
17 |
| - const libretroGameIds = compact(romResults.map((romResult) => romResult.libretro_game_id)) |
18 |
| - const libretroResults = await metadata |
19 |
| - .select() |
20 |
| - .from(libretroGameTable) |
21 |
| - .where(inArray(libretroGameTable.id, libretroGameIds)) |
22 |
| - const libretroResultMap = keyBy(libretroResults, (libretroResult) => libretroResult.id) |
23 |
| - |
24 |
| - const results = romResults.map((romResult) => ({ |
25 |
| - ...romResult, |
26 |
| - launchboxGame: null as InferSelectModel<typeof launchboxGameTable> | null, |
27 |
| - libretroGame: null as InferSelectModel<typeof libretroGameTable> | null, |
28 |
| - })) |
29 |
| - |
30 |
| - for (const result of results) { |
31 |
| - if (result.libretro_game_id) { |
32 |
| - result.libretroGame = libretroResultMap[result.libretro_game_id] |
33 |
| - } |
34 |
| - if (result.launchbox_game_id) { |
35 |
| - result.launchboxGame = launchboxResultMap[result.launchbox_game_id] |
36 |
| - } |
37 |
| - } |
38 |
| - |
39 |
| - return results |
40 |
| -} |
| 4 | +import { getRomsMetadata } from './utils.ts' |
41 | 5 |
|
42 | 6 | type GetRomsReturning = Awaited<ReturnType<typeof getRoms>>
|
43 | 7 | export type Roms = GetRomsReturning['roms']
|
@@ -72,7 +36,7 @@ export async function getRoms({
|
72 | 36 |
|
73 | 37 | const [{ total }] = await library.select({ total: count() }).from(romTable).orderBy(romTable.file_name).where(where)
|
74 | 38 |
|
75 |
| - const results = await getMetadata(romResults) |
| 39 | + const results = await getRomsMetadata(romResults) |
76 | 40 |
|
77 | 41 | return { pagination: { current: page, pages: Math.ceil(total / pageSize), size: pageSize, total }, roms: results }
|
78 | 42 | }
|
0 commit comments