Indexeddb: Wildcard Search

I was wondering if it is possible to search the indexeddb object store using wildcards. It would be convenient to find the whole object with a key starting with "555", for example

+6
source share
4 answers

This is possible out of the box using either composite keys or key fragments. The way keys work in IndexedDB is that you create a keyRange object and pass it to the cursor call. The key transmits information like "start with A and end with Z, inclusive."

By nature, this is a partial coincidence; the disadvantage is that your cursor will return any keys that are between your keys, and you may have to filter these results further.

Say that you have these words as keys in the object store:

  • Aardvark
  • Apple
  • Google
  • Microsoft

The range of keys "from A to Z inclusive" will return all of them, but "Ap to Z inclusive" will return only to the last three.

Another method I used to implement this is to pass the filter function to my methods that call IndexedDB. Inside the onsuccess callback methods, pass the result ( event.target.result ) through the filter function, and if it returns true, call the invoke invocation of the onsuccess method.

+5
source

Yes, you can use wildcards, sort of.

I still can not vote or even comment on the previous answers (hmmm ...), so I just repeat the answer of user2025527 as it worked completely for my needs.

Use the bounds method and specify the base value for the first argument and the same value plus an extra character for the second argument.

In most cases, the extra character of the character should be the last in your encoding: \ uffff

But you can decide what the limit is, especially when it comes to localization.

Suppose you have the following values ​​in your index:

  • A
  • Ab
  • IN
  • BA
  • BB
  • FROM

To find everything that is indicated in "BA", you should use

 var range = IDBKeyRange.bound("BA", "BA" + '\uffff'); 
+3
source

This is not possible by default, but my library that I wrote for indexeddb supports it. Try linq2indexeddb .

+2
source

You can also search using wildcards in indexeddb, see the link Fuzzy Search IndexedDB

for wildcards below should work: var range = IDBKeyRange.bound ("555", "555" + '\ uffff');

Or else, you can use the Linq2indexeddb library to use this.

+1
source

Source: https://habr.com/ru/post/911222/


All Articles