@@ -166,12 +166,11 @@ export function getStorageEstimate() {
166
166
}
167
167
168
168
export async function where ( dbName , storeName , jsonQueries , jsonQueryAdditions , uniqueResults = true ) {
169
-
170
169
const orConditionsArray = jsonQueries . map ( query => JSON . parse ( query ) ) ;
171
170
const QueryAdditions = JSON . parse ( jsonQueryAdditions ) ;
172
171
173
172
let db = await getDb ( dbName ) ;
174
- let table = db . table ( storeName ) ; // Corrected: Use Dexie’s table() method
173
+ let table = db . table ( storeName ) ;
175
174
176
175
let results = [ ] ;
177
176
@@ -192,22 +191,28 @@ export async function where(dbName, storeName, jsonQueries, jsonQueryAdditions,
192
191
if ( ! ( record [ condition . property ] <= parsedValue ) ) return false ;
193
192
break ;
194
193
case 'Equal' :
194
+ if ( record [ condition . property ] === null && condition . value === null ) {
195
+ return true ;
196
+ }
195
197
if ( condition . isString ) {
196
198
if ( condition . caseSensitive ) {
197
199
if ( record [ condition . property ] !== condition . value ) return false ;
198
200
} else {
199
- if ( record [ condition . property ] . toLowerCase ( ) !== condition . value . toLowerCase ( ) ) return false ;
201
+ if ( record [ condition . property ] ? .toLowerCase ( ) !== condition . value ? .toLowerCase ( ) ) return false ;
200
202
}
201
203
} else {
202
204
if ( record [ condition . property ] !== parsedValue ) return false ;
203
205
}
204
206
break ;
205
207
case 'NotEqual' :
208
+ if ( record [ condition . property ] === null && condition . value === null ) {
209
+ return false ;
210
+ }
206
211
if ( condition . isString ) {
207
212
if ( condition . caseSensitive ) {
208
213
if ( record [ condition . property ] === condition . value ) return false ;
209
214
} else {
210
- if ( record [ condition . property ] . toLowerCase ( ) === condition . value . toLowerCase ( ) ) return false ;
215
+ if ( record [ condition . property ] ? .toLowerCase ( ) === condition . value ? .toLowerCase ( ) ) return false ;
211
216
}
212
217
} else {
213
218
if ( record [ condition . property ] === parsedValue ) return false ;
@@ -231,23 +236,19 @@ export async function where(dbName, storeName, jsonQueries, jsonQueryAdditions,
231
236
232
237
async function processWithCursor ( conditions ) {
233
238
return new Promise ( ( resolve , reject ) => {
234
- // Dynamically detect the primary key
235
239
let primaryKey = table . schema . primKey . name ;
236
-
240
+ let cursorResults = [ ] ;
237
241
let request = table . orderBy ( primaryKey ) . each ( ( record ) => {
238
242
if ( applyConditionsToRecord ( record , conditions ) ) {
239
- results . push ( record ) ;
243
+ cursorResults . push ( record ) ;
240
244
}
241
245
} ) ;
242
-
243
- request . then ( resolve ) . catch ( reject ) ;
246
+ request . then ( ( ) => resolve ( cursorResults ) ) . catch ( reject ) ;
244
247
} ) ;
245
248
}
246
249
247
-
248
250
async function processIndexedQuery ( conditions ) {
249
- let localResults = [ ] ; // Store local query results
250
-
251
+ let localResults = [ ] ;
251
252
for ( const condition of conditions ) {
252
253
if ( table . schema . idxByName [ condition . property ] ) {
253
254
let indexQuery = null ;
@@ -271,96 +272,66 @@ export async function where(dbName, storeName, jsonQueries, jsonQueryAdditions,
271
272
indexQuery = table . where ( condition . property ) . anyOf ( condition . value ) ;
272
273
break ;
273
274
}
274
-
275
275
if ( indexQuery ) {
276
276
let indexedResults = await indexQuery . toArray ( ) ;
277
277
localResults . push ( ...indexedResults . filter ( record => applyConditionsToRecord ( record , conditions ) ) ) ;
278
278
}
279
279
}
280
280
}
281
-
282
281
if ( localResults . length === 0 ) {
283
- await processWithCursor ( conditions ) ; // Fallback to cursor-based filtering
284
- } else {
285
- results . push ( ...localResults ) ; // Append instead of overwriting
282
+ localResults = await processWithCursor ( conditions ) ;
286
283
}
284
+ results . push ( ...localResults ) ;
287
285
}
288
286
289
-
290
287
function applyArrayQueryAdditions ( results , queryAdditions ) {
291
- if ( queryAdditions != null ) {
292
- for ( let i = 0 ; i < queryAdditions . length ; i ++ ) {
293
- const queryAddition = queryAdditions [ i ] ;
294
-
295
- switch ( queryAddition . Name ) {
296
- case 'skip' :
297
- results = results . slice ( queryAddition . IntValue ) ;
298
- break ;
299
- case 'take' :
300
- results = results . slice ( 0 , queryAddition . IntValue ) ;
301
- break ;
302
- case 'takeLast' :
303
- results = results . slice ( - queryAddition . IntValue ) . reverse ( ) ;
304
- break ;
305
- case 'orderBy' :
306
- results = results . sort ( ( a , b ) => a [ queryAddition . StringValue ] - b [ queryAddition . StringValue ] ) ;
307
- break ;
308
- case 'orderByDescending' :
309
- results = results . sort ( ( a , b ) => b [ queryAddition . StringValue ] - a [ queryAddition . StringValue ] ) ;
310
- break ;
311
- default :
312
- console . error ( 'Unsupported query addition for array:' , queryAddition . Name ) ;
313
- break ;
314
- }
288
+ if ( queryAdditions ) {
289
+ if ( queryAdditions . some ( q => q . Name === 'orderBy' ) ) {
290
+ const orderBy = queryAdditions . find ( q => q . Name === 'orderBy' ) ;
291
+ results . sort ( ( a , b ) => a [ orderBy . StringValue ] - b [ orderBy . StringValue ] ) ;
292
+ }
293
+ if ( queryAdditions . some ( q => q . Name === 'orderByDescending' ) ) {
294
+ const orderByDescending = queryAdditions . find ( q => q . Name === 'orderByDescending' ) ;
295
+ results . sort ( ( a , b ) => b [ orderByDescending . StringValue ] - a [ orderByDescending . StringValue ] ) ;
296
+ }
297
+ if ( queryAdditions . some ( q => q . Name === 'skip' ) ) {
298
+ results = results . slice ( queryAdditions . find ( q => q . Name === 'skip' ) . IntValue ) ;
299
+ }
300
+ if ( queryAdditions . some ( q => q . Name === 'take' ) ) {
301
+ results = results . slice ( 0 , queryAdditions . find ( q => q . Name === 'take' ) . IntValue ) ;
302
+ }
303
+ if ( queryAdditions . some ( q => q . Name === 'takeLast' ) ) {
304
+ const takeLastValue = queryAdditions . find ( q => q . Name === 'takeLast' ) . IntValue ;
305
+ results = results . slice ( - takeLastValue ) . reverse ( ) ;
315
306
}
316
307
}
317
308
return results ;
318
309
}
319
310
320
311
async function combineQueries ( ) {
321
- const allQueries = [ ] ;
322
-
323
312
for ( const conditions of orConditionsArray ) {
324
-
325
- const query = await processIndexedQuery ( conditions [ 0 ] ) ; // Ensure it executes fully
326
-
327
-
328
- if ( query ) {
329
- allQueries . push ( query ) ;
330
- }
313
+ await processIndexedQuery ( conditions [ 0 ] ) ;
331
314
}
332
-
333
-
334
- if ( allQueries . length > 0 ) {
335
- // Execute all queries in parallel
336
- await Promise . all ( allQueries ) ;
337
-
338
-
339
- // Apply query additions to the combined results
340
- results = applyArrayQueryAdditions ( results , QueryAdditions ) ;
341
-
342
-
343
- if ( allQueries . length > 1 && uniqueResults ) {
344
- // Make sure the objects in the array are unique
345
- const uniqueObjects = new Set ( results . map ( obj => JSON . stringify ( obj ) ) ) ;
346
- results = Array . from ( uniqueObjects ) . map ( str => JSON . parse ( str ) ) ;
347
- }
315
+ results = applyArrayQueryAdditions ( results , QueryAdditions ) ;
316
+ if ( uniqueResults ) {
317
+ const uniqueObjects = new Set ( results . map ( obj => JSON . stringify ( obj ) ) ) ;
318
+ results = Array . from ( uniqueObjects ) . map ( str => JSON . parse ( str ) ) ;
348
319
}
349
-
350
320
return results ;
351
321
}
352
322
353
-
354
- if ( orConditionsArray . length > 0 )
323
+ if ( orConditionsArray . length > 0 ) {
355
324
return await combineQueries ( ) ;
356
- else
325
+ } else {
357
326
return [ ] ;
327
+ }
358
328
}
359
329
360
330
361
331
362
332
363
333
334
+
364
335
async function getDb ( dbName ) {
365
336
if ( databases . find ( d => d . name == dbName ) === undefined ) {
366
337
console . warn ( "Blazor.IndexedDB.Framework - Database doesn't exist" ) ;
0 commit comments