Files
klz-cables.com/.pnpm-store/v10/files/e1/61d79553dd81decf37bf38ffd59c1e2c6e66562fc8f1d51ee3eb2d562e47608d2fa5c38c560bd57976cc8bca8739d27aea26b4e344153398be7246782d9f8d
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

24 lines
1.2 KiB
Plaintext

import { count, sql } from 'drizzle-orm';
export const countDistinct = async function countDistinct({ column, db, joins, tableName, where }) {
// When we don't have any joins - use a simple COUNT(*) query.
if (joins.length === 0) {
const countResult = await db.select({
count: column ? count(sql`DISTINCT ${column}`) : count()
}).from(this.tables[tableName]).where(where);
return Number(countResult?.[0]?.count ?? 0);
}
let query = db.select({
count: sql`COUNT(1) OVER()`
}).from(this.tables[tableName]).where(where).groupBy(column ?? this.tables[tableName].id).limit(1).$dynamic();
joins.forEach(({ type, condition, table })=>{
query = query[type ?? 'leftJoin'](table, condition);
});
// When we have any joins, we need to count each individual ID only once.
// COUNT(*) doesn't work for this well in this case, as it also counts joined tables.
// SELECT (COUNT DISTINCT id) has a very slow performance on large tables.
// Instead, COUNT (GROUP BY id) can be used which is still slower than COUNT(*) but acceptable.
const countResult = await query;
return Number(countResult?.[0]?.count ?? 0);
};
//# sourceMappingURL=countDistinct.js.map