@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it } from "vitest"
22import { createCollection } from "../../src/collection.js"
33import { mockSyncCollectionOptions } from "../utls.js"
44import { createLiveQueryCollection } from "../../src/query/live-query-collection.js"
5- import { eq , gt } from "../../src/query/builder/functions.js"
5+ import { eq , gt , max } from "../../src/query/builder/functions.js"
66
77type Person = {
88 id : string
@@ -711,6 +711,126 @@ function createOrderByTests(autoIndex: `off` | `eager`): void {
711711 } )
712712 } )
713713
714+ describe ( `OrderBy with GroupBy` , ( ) => {
715+ it ( `should order grouped results correctly` , async ( ) => {
716+ type VehicleDocument = {
717+ id : number
718+ vin : string
719+ updatedAt : number
720+ }
721+
722+ const vehicleDocumentsData = [
723+ { id : 1 , vin : `1` , updatedAt : new Date ( `2023-01-01` ) . getTime ( ) } ,
724+ { id : 2 , vin : `2` , updatedAt : new Date ( `2023-01-02` ) . getTime ( ) } ,
725+ { id : 3 , vin : `1` , updatedAt : new Date ( `2023-01-05` ) . getTime ( ) } ,
726+ ]
727+
728+ const vehicleDocumentCollection = createCollection (
729+ mockSyncCollectionOptions < VehicleDocument > ( {
730+ id : `vehicle-document-collection` ,
731+ getKey : ( doc ) => doc . id ,
732+ autoIndex : `eager` ,
733+ initialData : vehicleDocumentsData ,
734+ } )
735+ )
736+
737+ const liveQuery = createLiveQueryCollection ( {
738+ query : ( q ) =>
739+ q
740+ . from ( { vehicleDocuments : vehicleDocumentCollection } )
741+ . groupBy ( ( q ) => q . vehicleDocuments . vin )
742+ . orderBy ( ( q ) => q . vehicleDocuments . vin , `asc` )
743+ . select ( ( q ) => ( {
744+ vin : q . vehicleDocuments . vin ,
745+ } ) ) ,
746+ startSync : true ,
747+ } )
748+
749+ await liveQuery . stateWhenReady ( )
750+ expect ( liveQuery . toArray ) . toEqual ( [ { vin : `1` } , { vin : `2` } ] )
751+
752+ // Insert a vehicle document
753+ vehicleDocumentCollection . utils . begin ( )
754+ vehicleDocumentCollection . utils . write ( {
755+ type : `insert` ,
756+ value : {
757+ id : 4 ,
758+ vin : `3` ,
759+ updatedAt : new Date ( `2023-01-03` ) . getTime ( ) ,
760+ } ,
761+ } )
762+ vehicleDocumentCollection . utils . commit ( )
763+
764+ expect ( liveQuery . toArray ) . toEqual ( [
765+ { vin : `1` } ,
766+ { vin : `2` } ,
767+ { vin : `3` } ,
768+ ] )
769+ } )
770+
771+ it ( `should order groups based on aggregates correctly` , async ( ) => {
772+ type VehicleDocument = {
773+ id : number
774+ vin : string
775+ updatedAt : number
776+ }
777+
778+ const vehicleDocumentsData = [
779+ { id : 1 , vin : `1` , updatedAt : new Date ( `2023-01-01` ) . getTime ( ) } ,
780+ { id : 2 , vin : `2` , updatedAt : new Date ( `2023-01-02` ) . getTime ( ) } ,
781+ { id : 3 , vin : `1` , updatedAt : new Date ( `2023-01-05` ) . getTime ( ) } ,
782+ ]
783+
784+ const vehicleDocumentCollection = createCollection (
785+ mockSyncCollectionOptions < VehicleDocument > ( {
786+ id : `vehicle-document-collection` ,
787+ getKey : ( doc ) => doc . id ,
788+ autoIndex : `eager` ,
789+ initialData : vehicleDocumentsData ,
790+ } )
791+ )
792+
793+ const liveQuery = createLiveQueryCollection ( {
794+ query : ( q ) =>
795+ q
796+ . from ( { vehicleDocuments : vehicleDocumentCollection } )
797+ . groupBy ( ( q ) => q . vehicleDocuments . vin )
798+ . orderBy ( ( q ) => max ( q . vehicleDocuments . updatedAt ) , `desc` )
799+ . select ( ( q ) => ( {
800+ vin : q . vehicleDocuments . vin ,
801+ updatedAt : max ( q . vehicleDocuments . updatedAt ) ,
802+ } ) )
803+ . offset ( 0 )
804+ . limit ( 10 ) ,
805+ startSync : true ,
806+ } )
807+
808+ await liveQuery . stateWhenReady ( )
809+ expect ( liveQuery . toArray ) . toEqual ( [
810+ { vin : `1` , updatedAt : new Date ( `2023-01-05` ) . getTime ( ) } ,
811+ { vin : `2` , updatedAt : new Date ( `2023-01-02` ) . getTime ( ) } ,
812+ ] )
813+
814+ // Insert a vehicle document
815+ vehicleDocumentCollection . utils . begin ( )
816+ vehicleDocumentCollection . utils . write ( {
817+ type : `insert` ,
818+ value : {
819+ id : 4 ,
820+ vin : `3` ,
821+ updatedAt : new Date ( `2023-01-03` ) . getTime ( ) ,
822+ } ,
823+ } )
824+ vehicleDocumentCollection . utils . commit ( )
825+
826+ expect ( liveQuery . toArray ) . toEqual ( [
827+ { vin : `1` , updatedAt : new Date ( `2023-01-05` ) . getTime ( ) } ,
828+ { vin : `3` , updatedAt : new Date ( `2023-01-03` ) . getTime ( ) } ,
829+ { vin : `2` , updatedAt : new Date ( `2023-01-02` ) . getTime ( ) } ,
830+ ] )
831+ } )
832+ } )
833+
714834 describe ( `OrderBy with Where Clauses` , ( ) => {
715835 it ( `orders filtered results correctly` , async ( ) => {
716836 const collection = createLiveQueryCollection ( ( q ) =>
0 commit comments