Implement Get Lucky sidebar button

This commit is contained in:
Jorge Vargas 2025-03-24 14:00:44 -06:00
parent 5c8f8be791
commit 91a91cb6a3
4 changed files with 39 additions and 20 deletions

18
src/utils/queries.ts Normal file
View file

@ -0,0 +1,18 @@
import prismaClient from './prisma-client'
export async function getRandomAlbum(): Promise<{ id: number; title: string }> {
const res: { id: number; title: string }[] = await prismaClient.$queryRawUnsafe(`
SELECT r1.id as id, r1.title as title
FROM albums AS r1 JOIN (
SELECT (
RAND() * (
SELECT MAX(id) FROM albums
)
) AS id
) AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 1;`)
return res[0]
}