@@ -2172,3 +2172,80 @@ describe(`Query2 OrderBy Compiler`, () => {
2172
2172
createOrderByTests ( `off` )
2173
2173
createOrderByTests ( `eager` )
2174
2174
} )
2175
+
2176
+ describe ( `OrderBy with collection alias conflicts` , ( ) => {
2177
+ type EmailSchema = {
2178
+ email : string
2179
+ createdAt : Date
2180
+ }
2181
+
2182
+ const date1 = new Date ( `2024-01-01` )
2183
+ const date2 = new Date ( `2024-01-02` )
2184
+ const date3 = new Date ( `2024-01-03` )
2185
+
2186
+ const emailCollection = createCollection < EmailSchema > ( {
2187
+ ...mockSyncCollectionOptions ( {
2188
+ id : `emails` ,
2189
+ getKey : ( item ) => item . email ,
2190
+ initialData : [
2191
+ { email :
`[email protected] ` , createdAt :
date1 } ,
2192
+ { email :
`[email protected] ` , createdAt :
date2 } ,
2193
+ { email :
`[email protected] ` , createdAt :
date3 } ,
2194
+ ] ,
2195
+ } ) ,
2196
+ } )
2197
+
2198
+ it ( `should work when alias does not conflict with field name` , ( ) => {
2199
+ // This should work fine - alias "t" doesn't conflict with any field
2200
+ const liveCollection = createLiveQueryCollection ( {
2201
+ startSync : true ,
2202
+ query : ( q ) =>
2203
+ q . from ( { t : emailCollection } ) . orderBy ( ( { t } ) => t . createdAt , `desc` ) ,
2204
+ } )
2205
+
2206
+ const result = liveCollection . toArray
2207
+
2208
+ expect ( result ) . toHaveLength ( 3 )
2209
+ expect ( result [ 0 ] ?. email ) . toBe ( `[email protected] ` )
2210
+ expect ( result [ 1 ] ?. email ) . toBe ( `[email protected] ` )
2211
+ expect ( result [ 2 ] ?. email ) . toBe ( `[email protected] ` )
2212
+ } )
2213
+
2214
+ it ( `should work when alias DOES conflict with field name` , ( ) => {
2215
+ // This breaks - alias "email" conflicts with field "email"
2216
+ const liveCollection = createLiveQueryCollection ( {
2217
+ startSync : true ,
2218
+ query : ( q ) =>
2219
+ q
2220
+ . from ( { email : emailCollection } )
2221
+ . orderBy ( ( { email } ) => email . createdAt , `desc` ) ,
2222
+ } )
2223
+
2224
+ const result = liveCollection . toArray
2225
+
2226
+ expect ( result ) . toHaveLength ( 3 )
2227
+ // The sorting should work - most recent first
2228
+ expect ( result [ 0 ] ?. email ) . toBe ( `[email protected] ` )
2229
+ expect ( result [ 1 ] ?. email ) . toBe ( `[email protected] ` )
2230
+ expect ( result [ 2 ] ?. email ) . toBe ( `[email protected] ` )
2231
+ } )
2232
+
2233
+ it ( `should also work for createdAt alias conflict` , ( ) => {
2234
+ // This should also work - alias "createdAt" conflicts with field "createdAt"
2235
+ const liveCollection = createLiveQueryCollection ( {
2236
+ startSync : true ,
2237
+ query : ( q ) =>
2238
+ q
2239
+ . from ( { createdAt : emailCollection } )
2240
+ . orderBy ( ( { createdAt } ) => createdAt . email , `asc` ) ,
2241
+ } )
2242
+
2243
+ const result = liveCollection . toArray as Array < EmailSchema >
2244
+
2245
+ expect ( result ) . toHaveLength ( 3 )
2246
+ // The sorting should work - alphabetically by email
2247
+ expect ( result [ 0 ] ?. email ) . toBe ( `[email protected] ` )
2248
+ expect ( result [ 1 ] ?. email ) . toBe ( `[email protected] ` )
2249
+ expect ( result [ 2 ] ?. email ) . toBe ( `[email protected] ` )
2250
+ } )
2251
+ } )
0 commit comments