Skip to content

Commit 03b2432

Browse files
committed
fix: sort general supabase queries
This sort function was only built to work with a few very specific types from the Caching DB. However, it was used on data that wasn't meant for the caching DB. Namely the Blueprints and perhaps others. This implements basic sorting for all database types.
1 parent 15a1ab0 commit 03b2432

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/graphql/schemas/utils/sorting.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PostgrestTransformBuilder } from "@supabase/postgrest-js";
22

3+
import { Database as DataDatabase } from "../../../types/supabaseData.js";
34
import type { Database as CachingDatabase } from "../../../types/supabaseCaching.js";
45
import type { OrderOptions } from "../inputs/orderOptions.js";
56
import {
@@ -15,7 +16,7 @@ import { SortOrder } from "../enums/sortEnums.js";
1516
interface ApplySorting<
1617
T extends object,
1718
QueryType extends PostgrestTransformBuilder<
18-
CachingDatabase["public"],
19+
CachingDatabase["public"] | DataDatabase["public"],
1920
Record<string, unknown>,
2021
unknown,
2122
unknown,
@@ -26,10 +27,16 @@ interface ApplySorting<
2627
sort?: OrderOptions<T>;
2728
}
2829

30+
type ColumnOpts = {
31+
ascending?: boolean;
32+
nullsFirst?: boolean;
33+
referencedTable?: string;
34+
};
35+
2936
export const applySorting = <
3037
T extends object,
3138
QueryType extends PostgrestTransformBuilder<
32-
CachingDatabase["public"],
39+
CachingDatabase["public"] | DataDatabase["public"],
3340
Record<string, unknown>,
3441
unknown,
3542
unknown,
@@ -41,17 +48,18 @@ export const applySorting = <
4148
}: ApplySorting<T, QueryType>) => {
4249
if (!sort) return query;
4350

44-
const sorting: [
45-
string,
46-
(
47-
| { ascending?: boolean; nullsFirst?: boolean; referencedTable?: string }
48-
| undefined
49-
),
50-
][] = [];
51-
for (const [, value] of Object.entries(sort)) {
51+
const sorting: [string, ColumnOpts][] = [];
52+
for (const [key, value] of Object.entries(sort.by || {})) {
5253
if (!value) continue;
5354

54-
// If the value is an object, recursively apply sorting
55+
// Handle direct sorting parameters
56+
if (typeof value === "string") {
57+
sorting.push([key, { ascending: value !== SortOrder.descending }]);
58+
continue;
59+
}
60+
61+
// Handle nested sorting options
62+
// FIXME: This is brittle. We should find a way to generalize this
5563
if (
5664
value instanceof HypercertSortOptions ||
5765
value instanceof FractionSortOptions ||
@@ -60,23 +68,13 @@ export const applySorting = <
6068
value instanceof MetadataSortOptions ||
6169
value instanceof AttestationSchemaSortOptions
6270
) {
63-
const nestedSorting: [
64-
string,
65-
{
66-
ascending?: boolean;
67-
nullsFirst?: boolean;
68-
referencedTable?: string;
69-
},
70-
][] = [];
71-
for (const [_column, _direction] of Object.entries(value)) {
72-
if (!_column || !_direction) continue;
73-
// TODO resolve hacky workaround for hypercerts <> claims alias
74-
nestedSorting.push([
75-
_column,
76-
{ ascending: _direction !== SortOrder.descending },
71+
for (const [column, direction] of Object.entries(value)) {
72+
if (!column || !direction) continue;
73+
sorting.push([
74+
`${key}.${column}`,
75+
{ ascending: direction !== SortOrder.descending },
7776
]);
7877
}
79-
sorting.push(...nestedSorting);
8078
}
8179
}
8280

0 commit comments

Comments
 (0)