Menu

Twelve SELECTs for the list and six filtered counts for the page, and the only shared code is three SQL fragments
📰

Twelve SELECTs for the list and six filtered counts for the page, and the only shared code is three SQL fragments

DEV Community: typescript·Daniel Pertu·4 days ago
#ReItDIwL
#devtotypescript#devto#dev#campaign#select
Reading 0:00
15s threshold

Nakodo has two screens that count the same rows. The campaigns list shows a card per campaign with found, fit, contacted and replied on it. A campaign's own page shows a funnel: found, fit, waiting, contacted, plus how many results are still being checked and, on the free plan, how many are scored but hidden.

Same words, same table, two completely different queries, because one needs a number per campaign in a list and the other needs six numbers for one campaign. That is the situation where a codebase grows two definitions of "fit" and nobody notices for a month. Our defence is that neither screen contains a definition. Both import the same SQL fragments out of src/lib/queries.ts , and the fragments are functions of a table alias:

const fitSql = ( alias : string , cap : number | null , scope : SQL ) => sql ` ${ shownSql ( alias , cap , scope )} and ${ sql . raw ( fitCondition ( alias ))} ` ;

// Fits Nakodo can still write to: a published email, not skipped, and no // conversation in this campaign yet.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.