@@ -179,35 +179,47 @@ function runIndexedQuery(table, indexedConditions, queryAdditions = []) {
179
179
debugLog ( "Executing runIndexedQuery" , { indexedConditions, queryAdditions } ) ;
180
180
181
181
let query ;
182
- let firstCondition = indexedConditions [ 0 ] ;
182
+ const firstCondition = indexedConditions [ 0 ] ;
183
183
184
- // **Handle Compound Index Query**
185
- if ( Array . isArray ( firstCondition . properties ) ) {
186
- debugLog ( "Detected Compound Index Query!" , { properties : firstCondition . properties } ) ;
184
+ const isUniversalFilter = (
185
+ firstCondition &&
186
+ firstCondition . operation === QUERY_OPERATIONS . GREATER_THAN_OR_EQUAL &&
187
+ ( firstCondition . value === - Infinity || firstCondition . value === Number . NEGATIVE_INFINITY )
188
+ ) ;
189
+
190
+ const orderAddition = queryAdditions . find ( q =>
191
+ q . additionFunction === QUERY_ADDITIONS . ORDER_BY ||
192
+ q . additionFunction === QUERY_ADDITIONS . ORDER_BY_DESCENDING
193
+ ) ;
194
+
195
+ // === Handle Universal Filter Shortcut (full table scan with orderBy) ===
196
+ if ( isUniversalFilter && orderAddition ?. property ) {
197
+ debugLog ( "Detected universal filter with orderBy!" , { orderBy : orderAddition . property } ) ;
198
+
199
+ query = table . orderBy ( orderAddition . property ) ;
200
+ if ( orderAddition . additionFunction === QUERY_ADDITIONS . ORDER_BY_DESCENDING ) {
201
+ query = query . reverse ( ) ;
202
+ }
203
+ }
187
204
188
- // **Extract values in the correct order**
189
- const valuesInCorrectOrder = firstCondition . properties . map ( ( _ , index ) => firstCondition . value [ index ] ) ;
205
+ // === Handle Compound Index Query ===
206
+ else if ( Array . isArray ( firstCondition . properties ) ) {
207
+ debugLog ( "Detected Compound Index Query!" , { properties : firstCondition . properties } ) ;
190
208
191
- debugLog ( "Compound Index Query - Ordered Properties & Values" , {
192
- properties : firstCondition . properties ,
193
- values : valuesInCorrectOrder
194
- } ) ;
209
+ const valuesInCorrectOrder = firstCondition . properties . map ( ( _ , i ) => firstCondition . value [ i ] ) ;
195
210
196
211
query = table . where ( firstCondition . properties ) ;
197
212
198
213
if ( firstCondition . operation === QUERY_OPERATIONS . EQUAL ) {
199
214
query = query . equals ( valuesInCorrectOrder ) ;
200
- }
201
- else if ( firstCondition . operation === QUERY_OPERATIONS . IN ) {
215
+ } else if ( firstCondition . operation === QUERY_OPERATIONS . IN ) {
202
216
query = query . anyOf ( firstCondition . value ) ;
203
- }
204
- else {
217
+ } else {
205
218
throw new Error ( `Unsupported operation for compound indexes: ${ firstCondition . operation } ` ) ;
206
219
}
207
220
}
208
221
209
-
210
- // **Handle Single Indexed Query**
222
+ // === Handle Single Indexed Query ===
211
223
else if ( firstCondition . property ) {
212
224
debugLog ( "Detected Single-Index Query!" , { property : firstCondition . property } ) ;
213
225
@@ -233,45 +245,30 @@ function runIndexedQuery(table, indexedConditions, queryAdditions = []) {
233
245
case QUERY_OPERATIONS . STARTS_WITH :
234
246
query = table . where ( firstCondition . property ) . startsWith ( firstCondition . value ) ;
235
247
break ;
236
-
237
248
case "between" :
238
249
if ( Array . isArray ( firstCondition . value ) && firstCondition . value . length === 2 ) {
239
250
const [ lower , upper ] = firstCondition . value ;
240
- const includeLower = firstCondition . includeLower !== false ; // default to true
241
- const includeUpper = firstCondition . includeUpper !== false ; // default to true
242
-
251
+ const includeLower = firstCondition . includeLower !== false ;
252
+ const includeUpper = firstCondition . includeUpper !== false ;
243
253
query = table . where ( firstCondition . property ) . between ( lower , upper , includeLower , includeUpper ) ;
244
254
} else {
245
255
throw new Error ( "Invalid 'between' value format. Expected [min, max]" ) ;
246
256
}
247
257
break ;
248
-
249
-
250
258
default :
251
259
throw new Error ( `Unsupported indexed query operation: ${ firstCondition . operation } ` ) ;
252
260
}
253
- }
254
- else {
261
+ } else {
255
262
throw new Error ( "Invalid indexed conditionmissing `properties` or `property`." ) ;
256
263
}
257
264
258
- /*
259
- LINQ to IndexedDB sacrifices ordering of the return for performance.
260
- Therefore skip entirely even appending an order if that's all that's on there.
261
- As that means the order doesn't have anything to do with the end desired results.
262
- */
265
+ // === Apply Query Additions (take, skip, first, etc.) ===
263
266
if ( requiresQueryAdditions ( queryAdditions ) ) {
264
267
for ( const addition of queryAdditions ) {
265
268
switch ( addition . additionFunction ) {
266
269
case QUERY_ADDITIONS . ORDER_BY :
267
- if ( addition . property ) {
268
- query = query . orderBy ( addition . property ) ;
269
- }
270
- break ;
271
270
case QUERY_ADDITIONS . ORDER_BY_DESCENDING :
272
- if ( addition . property ) {
273
- query = query . orderBy ( addition . property ) . reverse ( ) ;
274
- }
271
+ // Already handled above (only valid without .where())
275
272
break ;
276
273
case QUERY_ADDITIONS . SKIP :
277
274
query = query . offset ( addition . intValue ) ;
@@ -295,6 +292,7 @@ function runIndexedQuery(table, indexedConditions, queryAdditions = []) {
295
292
return query ;
296
293
}
297
294
295
+
298
296
function requiresQueryAdditions ( queryAdditions = [ ] ) {
299
297
if ( ! queryAdditions || queryAdditions . length === 0 ) {
300
298
return false ; // No query additions at all, skip processing
0 commit comments