Invental/Review lessons/Code that silently does the wrong thing

Code that silently does the wrong thing

Paging results merged from several queries

When one “page” is the merge of several paged queries, page every query with the same offset. “Is there more?” is true if any source has more. Paging only the main query silently drops the rest.

01 · Before and afterThe pattern

TypeScript
const opts = { skip: offset, take: limit + 1 };            // one extra row = "has more"
const [byName, byType, byTag] = await Promise.all([q.name(opts), q.type(opts), q.tag(opts)]);
const items = dedupe([...byType, ...byTag, ...byName].map(trim(limit)));
return { items: sortBy(items, 'updatedAt'), hasMore: [byName, byType, byTag].some(p => p.length > limit) };

02 · Run it on your appThe check

If your search merges results from several queries, can page 2 still show a match that only the second or third query finds?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.