Search code examples
dexie

How can I index and query nested arrays in Dexie.js?


The docs for MultiEntry indexes say any Indexable Type can be used.

Arrays are indexable types. Therefore it should be possible to index arrays of arrays and so on, right?

However, I couldn't find any examples or figure out how to query these.

Basically, what I want is something like:

var db = new Dexie('dbname');

db.version(1).stores({
  books: 'id, author, name, *properties'
});

db.books.put({
  id: 1,
  name: 'Human Action', 
  author: 'Ludwig von Mises',
  properties: [['language', 'english'], ['subject', 'philosophy'], ['subject', 'economics']]
});

Then be able to find a book that has an economics subject.


Solution

  • That's right. Each entry in the properties array is an array of two strings - and that inner array itself is indexable and may act as an indexable entry.

    So to find all books that has subject economics, do

    db.books.where({properties: ['subject', 'economics']}).toArray()
    

    or the equivalent form:

    db.books
      .where('properties')
      .equals(['subject', 'economics'])
      .toArray();