@@ -166,12 +166,11 @@ export function getStorageEstimate() {
166166}
167167
168168export async function where ( dbName , storeName , jsonQueries , jsonQueryAdditions , uniqueResults = true ) {
169-
170169 const orConditionsArray = jsonQueries . map ( query => JSON . parse ( query ) ) ;
171170 const QueryAdditions = JSON . parse ( jsonQueryAdditions ) ;
172171
173172 let db = await getDb ( dbName ) ;
174- let table = db . table ( storeName ) ; // Corrected: Use Dexie’s table() method
173+ let table = db . table ( storeName ) ;
175174
176175 let results = [ ] ;
177176
@@ -192,22 +191,28 @@ export async function where(dbName, storeName, jsonQueries, jsonQueryAdditions,
192191 if ( ! ( record [ condition . property ] <= parsedValue ) ) return false ;
193192 break ;
194193 case 'Equal' :
194+ if ( record [ condition . property ] === null && condition . value === null ) {
195+ return true ;
196+ }
195197 if ( condition . isString ) {
196198 if ( condition . caseSensitive ) {
197199 if ( record [ condition . property ] !== condition . value ) return false ;
198200 } else {
199- if ( record [ condition . property ] . toLowerCase ( ) !== condition . value . toLowerCase ( ) ) return false ;
201+ if ( record [ condition . property ] ? .toLowerCase ( ) !== condition . value ? .toLowerCase ( ) ) return false ;
200202 }
201203 } else {
202204 if ( record [ condition . property ] !== parsedValue ) return false ;
203205 }
204206 break ;
205207 case 'NotEqual' :
208+ if ( record [ condition . property ] === null && condition . value === null ) {
209+ return false ;
210+ }
206211 if ( condition . isString ) {
207212 if ( condition . caseSensitive ) {
208213 if ( record [ condition . property ] === condition . value ) return false ;
209214 } else {
210- if ( record [ condition . property ] . toLowerCase ( ) === condition . value . toLowerCase ( ) ) return false ;
215+ if ( record [ condition . property ] ? .toLowerCase ( ) === condition . value ? .toLowerCase ( ) ) return false ;
211216 }
212217 } else {
213218 if ( record [ condition . property ] === parsedValue ) return false ;
@@ -231,23 +236,19 @@ export async function where(dbName, storeName, jsonQueries, jsonQueryAdditions,
231236
232237 async function processWithCursor ( conditions ) {
233238 return new Promise ( ( resolve , reject ) => {
234- // Dynamically detect the primary key
235239 let primaryKey = table . schema . primKey . name ;
236-
240+ let cursorResults = [ ] ;
237241 let request = table . orderBy ( primaryKey ) . each ( ( record ) => {
238242 if ( applyConditionsToRecord ( record , conditions ) ) {
239- results . push ( record ) ;
243+ cursorResults . push ( record ) ;
240244 }
241245 } ) ;
242-
243- request . then ( resolve ) . catch ( reject ) ;
246+ request . then ( ( ) => resolve ( cursorResults ) ) . catch ( reject ) ;
244247 } ) ;
245248 }
246249
247-
248250 async function processIndexedQuery ( conditions ) {
249- let localResults = [ ] ; // Store local query results
250-
251+ let localResults = [ ] ;
251252 for ( const condition of conditions ) {
252253 if ( table . schema . idxByName [ condition . property ] ) {
253254 let indexQuery = null ;
@@ -271,96 +272,66 @@ export async function where(dbName, storeName, jsonQueries, jsonQueryAdditions,
271272 indexQuery = table . where ( condition . property ) . anyOf ( condition . value ) ;
272273 break ;
273274 }
274-
275275 if ( indexQuery ) {
276276 let indexedResults = await indexQuery . toArray ( ) ;
277277 localResults . push ( ...indexedResults . filter ( record => applyConditionsToRecord ( record , conditions ) ) ) ;
278278 }
279279 }
280280 }
281-
282281 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 ) ;
286283 }
284+ results . push ( ...localResults ) ;
287285 }
288286
289-
290287 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 ( ) ;
315306 }
316307 }
317308 return results ;
318309 }
319310
320311 async function combineQueries ( ) {
321- const allQueries = [ ] ;
322-
323312 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 ] ) ;
331314 }
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 ) ) ;
348319 }
349-
350320 return results ;
351321 }
352322
353-
354- if ( orConditionsArray . length > 0 )
323+ if ( orConditionsArray . length > 0 ) {
355324 return await combineQueries ( ) ;
356- else
325+ } else {
357326 return [ ] ;
327+ }
358328}
359329
360330
361331
362332
363333
334+
364335async function getDb ( dbName ) {
365336 if ( databases . find ( d => d . name == dbName ) === undefined ) {
366337 console . warn ( "Blazor.IndexedDB.Framework - Database doesn't exist" ) ;
0 commit comments