@@ -6,7 +6,7 @@ const schema = z.object({
66 name : z . string ( ) ,
77} )
88
9- it ( 'sorts the find results by a single key (asc)' , async ( ) => {
9+ it ( 'sorts the results by a single key (asc)' , async ( ) => {
1010 const users = new Collection ( { schema } )
1111
1212 await users . create ( { id : 1 , name : 'John' } )
@@ -33,7 +33,7 @@ it('sorts the find results by a single key (asc)', async () => {
3333 ] )
3434} )
3535
36- it ( 'sorts the find results by a single key (desc)' , async ( ) => {
36+ it ( 'sorts the results by a single key (desc)' , async ( ) => {
3737 const users = new Collection ( { schema } )
3838
3939 await users . create ( { id : 1 , name : 'John' } )
@@ -60,7 +60,7 @@ it('sorts the find results by a single key (desc)', async () => {
6060 ] )
6161} )
6262
63- it ( 'sorts the find results by multiple keys (mixed)' , async ( ) => {
63+ it ( 'sorts the results by multiple keys (mixed)' , async ( ) => {
6464 const users = new Collection ( { schema } )
6565 await users . create ( { id : 1 , name : 'John' } )
6666 await users . create ( { id : 2 , name : 'Alice' } )
@@ -89,7 +89,7 @@ it('sorts the find results by multiple keys (mixed)', async () => {
8989 ] )
9090} )
9191
92- it ( 'sorts the find results by a nested key' , async ( ) => {
92+ it ( 'sorts the results by a nested key' , async ( ) => {
9393 const users = new Collection ( {
9494 schema : schema . extend ( {
9595 address : z . object ( {
@@ -121,3 +121,89 @@ it('sorts the find results by a nested key', async () => {
121121 { id : 1 , name : 'John' , address : { street : 'C' } } ,
122122 ] )
123123} )
124+
125+ it ( 'sorts the results by a list of sort criteria' , async ( ) => {
126+ const schema = z . object ( {
127+ id : z . number ( ) ,
128+ name : z . string ( ) ,
129+ age : z . number ( ) ,
130+ } )
131+
132+ const users = new Collection ( { schema } )
133+
134+ await users . create ( { id : 1 , name : 'John' , age : 32 } )
135+ await users . create ( { id : 2 , name : 'Alice' , age : 24 } )
136+ await users . create ( { id : 3 , name : 'Bob' , age : 41 } )
137+ await users . create ( { id : 4 , name : 'Alice' , age : 41 } )
138+
139+ expect (
140+ users . findMany ( undefined , {
141+ orderBy : [ { age : 'asc' } , { name : 'desc' } ] ,
142+ } ) ,
143+ ) . toEqual ( [
144+ { id : 2 , name : 'Alice' , age : 24 } ,
145+ { id : 1 , name : 'John' , age : 32 } ,
146+ { id : 3 , name : 'Bob' , age : 41 } ,
147+ { id : 4 , name : 'Alice' , age : 41 } ,
148+ ] )
149+ } )
150+
151+ it ( 'sorts by a relational property' , async ( ) => {
152+ const userSchema = z . object ( {
153+ id : z . number ( ) ,
154+ name : z . string ( ) ,
155+ get posts ( ) {
156+ return z . array ( postSchema )
157+ } ,
158+ } )
159+ const postSchema = z . object ( {
160+ id : z . number ( ) ,
161+ title : z . string ( ) ,
162+ get author ( ) {
163+ return userSchema . optional ( )
164+ } ,
165+ } )
166+
167+ const users = new Collection ( { schema : userSchema } )
168+ const posts = new Collection ( { schema : postSchema } )
169+
170+ users . defineRelations ( ( { many } ) => ( {
171+ posts : many ( posts ) ,
172+ } ) )
173+ posts . defineRelations ( ( { one } ) => ( {
174+ author : one ( users , { unique : true } ) ,
175+ } ) )
176+
177+ const john = await users . create ( {
178+ id : 1 ,
179+ name : 'John' ,
180+ posts : await posts . createMany ( 2 , ( index ) => ( {
181+ id : index + 1 ,
182+ title : `Post ${ index + 1 } ` ,
183+ } ) ) ,
184+ } )
185+
186+ const alice = await users . create ( {
187+ id : 2 ,
188+ name : 'Alice' ,
189+ posts : await posts . createMany ( 2 , ( index ) => ( {
190+ id : index + 3 ,
191+ title : `Post ${ index + 3 } ` ,
192+ } ) ) ,
193+ } )
194+
195+ expect (
196+ posts . findMany ( undefined , {
197+ orderBy : {
198+ author : {
199+ name : 'asc' ,
200+ } ,
201+ } ,
202+ } ) ,
203+ ) . toEqual ( [
204+ { id : 3 , title : 'Post 3' , author : alice } ,
205+ { id : 4 , title : 'Post 4' , author : alice } ,
206+ { id : 1 , title : 'Post 1' , author : john } ,
207+ { id : 2 , title : 'Post 2' , author : john } ,
208+ ] )
209+ } )
0 commit comments