Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexeddb: search using wildcards

I was wondering if it is possible to perform a search on an indexeddb object store using wildcards. It would be handy to find all object with a key beginning with '555' for example

like image 697
Jeanluca Scaljeri Avatar asked Mar 20 '12 16:03

Jeanluca Scaljeri


3 Answers

This is possible out of the box using either compound keys or key fragments. The way keys work in IndexedDB is that you generate a "keyRange" object and pass it to your cursor invocation. The keyrange passes info like "start at A and end at Z, inclusive."

By nature, there is partial matching built into this; the downside is that your cursor will return any keys that come between your keys and you might have to filter down those results further.

Say you have these words as keys in a object store:

  • Aardvark
  • Apple
  • Google
  • Microsoft

The key range "A to Z, inclusive" would return all of these but "Ap to Z, inclusive" would return just the last three.

Another technique I've used to implement this is by passing a "filter" function to my methods that invoke IndexedDB. Inside the methods onsuccess callback, pass the result (event.target.result) through your filter function and if it returns true then call your method invoker's onsuccess callback.

like image 101
buley Avatar answered Nov 05 '22 23:11

buley


Yes it is feasible to use wildcards, sort of.

I can't yet vote or even comment on previous answers (hmmm...) so I'll just repeat user2025527's answer as it totally worked for my needs.

Use the bounds method and specify the base value for the 1st argument and the same value plus an extra character for the 2nd argument.

In most cases the extra character character should be the last one in your charset: \uffff

But you're free to decide what constitutes the limit, especially when dealing with localization.

Lest say you have the following values in your index:

  • A
  • AB
  • B
  • BA
  • BB
  • C

To find everything stating with "BA" you should use

var range = IDBKeyRange.bound("BA", "BA" + '\uffff');
like image 5
SVDB Avatar answered Nov 05 '22 23:11

SVDB


It isn't possible by default, but my library i wrote for the indexeddb supports it. Try linq2indexeddb.

like image 2
Kristof Degrave Avatar answered Nov 05 '22 22:11

Kristof Degrave