/** * 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