1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-02-03 09:57:24 +02:00

normalized active sort on collection schema change

This commit is contained in:
Gani Georgiev 2023-06-09 12:56:07 +03:00
parent e4a90f6605
commit 7f06816008

View File

@ -46,6 +46,10 @@
reset();
}
$: if ($activeCollection?.id) {
normalizeSort();
}
// keep the url params in sync
$: if (sort || filter || $activeCollection?.id) {
const query = new URLSearchParams({
@ -63,12 +67,31 @@
filter = "";
sort = "-created";
// clear default sort if created field is not available
if (
$activeCollection?.$isView &&
!CommonHelper.extractColumnsFromQuery($activeCollection.options.query).includes("created")
) {
sort = "";
normalizeSort();
}
// ensures that the sort fields exist in the collection
async function normalizeSort() {
if (!sort) {
return; // nothing to normalize
}
const collectionFields = CommonHelper.getAllCollectionIdentifiers($activeCollection);
const sortFields = sort.split(",").map((f) => {
if (f.startsWith("+") || f.startsWith("-")) {
return f.substring(1);
}
return f;
});
// invalid sort expression or missing sort field
if (sortFields.filter((f) => collectionFields.includes(f)).length != sortFields.length) {
if (collectionFields.includes("created")) {
sort = "-created";
} else {
sort = "";
}
}
}