Search code examples
javascriptarraysfieldindexeddbdexie

With Dexie, can I get all objects in a table where an array field has a specific value as one of its elements?


I have a table where each object has a field that is an array of strings: for example, { people: ['John', 'Bob', 'Sue'] }. I need all objects in the table that have 'Sue' in the people array.

Can Dexie do this?


Solution

  • Yes, using MultiEntry indexes you can do exactly that.

    const db = new Dexie("testdb");
    db.version(2).stores({
      groups: 'id, *people'
    });
    
    async function addRow() {
      await db.groups.add({id: 1, people: ['John', 'Bob', 'Sue']});
    }
    
    async function findSuesGroups() (
      return await db.groups.where('people').equals('Sue').toArray();
    }
    

    See other examples at https://dexie.org/docs/MultiEntry-Index