lets try it out lmao

This commit is contained in:
2026-01-26 10:04:53 +01:00
parent 017ac4ca4c
commit 91546a1f0d

View File

@@ -29,7 +29,7 @@ export default router => {
const maxIdResult = await db`select max(id) as id from "items"`; const maxIdResult = await db`select max(id) as id from "items"`;
const maxId = maxIdResult[0].id || 0; const maxId = maxIdResult[0].id || 0;
const randomId = Math.floor(Math.random() * maxId);
let tagId = null; let tagId = null;
if (tag) { if (tag) {
@@ -57,24 +57,27 @@ export default router => {
${!hasSession && globalfilter ? db`and not exists (select 1 from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``} ${!hasSession && globalfilter ? db`and not exists (select 1 from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``}
`; `;
// Try seeking forward from random ID // Optimized Count + Offset Strategy
let rows = await db` const count = await db`
select count(*) as total
${baseQuery} ${baseQuery}
and "items".id >= ${randomId}
order by "items".id asc
limit 1
`; `;
// Fallback: wrap around if nothing found if (count[0].total == 0) {
if (rows.length === 0) { return res.json({
rows = await db` success: false,
${baseQuery} items: []
and "items".id >= 0 });
order by "items".id asc
limit 1
`;
} }
const offset = Math.floor(Math.random() * count[0].total);
const rows = await db`
select "items".*
${baseQuery}
limit 1 offset ${offset}
`;
return res.json({ return res.json({
success: rows.length > 0, success: rows.length > 0,
items: rows.length > 0 ? rows[0] : [] items: rows.length > 0 ? rows[0] : []