11import { PostgrestTransformBuilder } from "@supabase/postgrest-js" ;
22
3+ import { Database as DataDatabase } from "../../../types/supabaseData.js" ;
34import type { Database as CachingDatabase } from "../../../types/supabaseCaching.js" ;
45import type { OrderOptions } from "../inputs/orderOptions.js" ;
56import {
@@ -15,7 +16,7 @@ import { SortOrder } from "../enums/sortEnums.js";
1516interface 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+
2936export 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