Files
klz-cables.com/.pnpm-store/v10/files/18/25a26dd28da4cda27fa3a8d3f9b493e295bc28e16adcf8cdd7866be0adf234d4920dd5478521793ac88ef5bf32138d305e9c769d68b715b0a56bf6a3299b3c
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

71 lines
2.6 KiB
Plaintext

import { asc, desc } from 'drizzle-orm';
import { getNameFromDrizzleTable } from '../utilities/getNameFromDrizzleTable.js';
import { getTableColumnFromPath } from './getTableColumnFromPath.js';
/**
* Gets the order by column and direction constructed from the sort argument adds the column to the select fields and joins if necessary
*/ export const buildOrderBy = ({ adapter, aliasTable, fields, joins, locale, parentIsLocalized, rawSort, selectFields, sort, tableName })=>{
const orderBy = [];
const createdAt = adapter.tables[tableName]?.createdAt;
if (!sort) {
if (createdAt) {
sort = '-createdAt';
} else {
sort = '-id';
}
}
if (typeof sort === 'string') {
sort = [
sort
];
}
// In the case of Mongo, when sorting by a field that is not unique, the results are not guaranteed to be in the same order each time.
// So we add a fallback sort to ensure that the results are always in the same order.
let fallbackSort = '-id';
if (createdAt) {
fallbackSort = '-createdAt';
}
if (!(sort.includes(fallbackSort) || sort.includes(fallbackSort.replace('-', '')))) {
sort.push(fallbackSort);
}
for (const sortItem of sort){
let sortProperty;
let sortDirection;
if (sortItem[0] === '-') {
sortProperty = sortItem.substring(1);
sortDirection = 'desc';
} else {
sortProperty = sortItem;
sortDirection = 'asc';
}
try {
const { columnName: sortTableColumnName, table: sortTable } = getTableColumnFromPath({
adapter,
collectionPath: sortProperty,
fields,
joins,
locale,
parentIsLocalized,
pathSegments: sortProperty.replace(/__/g, '.').split('.'),
selectFields,
tableName,
value: sortProperty
});
if (sortTable?.[sortTableColumnName]) {
let order = sortDirection === 'asc' ? asc : desc;
if (rawSort) {
order = ()=>rawSort;
}
orderBy.push({
column: aliasTable && tableName === getNameFromDrizzleTable(sortTable) ? aliasTable[sortTableColumnName] : sortTable[sortTableColumnName],
order
});
selectFields[sortTableColumnName] = sortTable[sortTableColumnName];
}
} catch (_) {
// continue
}
}
return orderBy;
};
//# sourceMappingURL=buildOrderBy.js.map