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
29 lines
1.3 KiB
Plaintext
29 lines
1.3 KiB
Plaintext
/**
|
|
* Selects distinct records from a table only if there are joins that need to be used, otherwise return null
|
|
*/ export const selectDistinct = ({ adapter, db, forceRun, hasAggregates, joins, query: queryModifier = ({ query })=>query, selectFields, tableName, where })=>{
|
|
if (forceRun || Object.keys(joins).length > 0) {
|
|
let query;
|
|
const table = adapter.tables[tableName];
|
|
// With hasAggregate we use groupBy so we don't need to use selectDistinct in that case
|
|
// @ts-expect-error - Drizzle types are not accurate here
|
|
let selectFunc = hasAggregates ? db.select : db.selectDistinct;
|
|
// bind this otherwise we get TypeError: Cannot read properties of undefined (reading 'session')
|
|
selectFunc = selectFunc.bind(db);
|
|
query = selectFunc(selectFields).from(table).$dynamic();
|
|
if (where) {
|
|
query = query.where(where);
|
|
}
|
|
joins.forEach(({ type, condition, table })=>{
|
|
query = query[type ?? 'leftJoin'](table, condition);
|
|
});
|
|
if (hasAggregates && '_selected' in selectFields) {
|
|
// @ts-expect-error - Drizzle types are not accurate here
|
|
query = query.groupBy(selectFields['_selected']);
|
|
}
|
|
return queryModifier({
|
|
query
|
|
});
|
|
}
|
|
};
|
|
|
|
//# sourceMappingURL=selectDistinct.js.map |