0

I'm having some troubles getting where() statements to work in Dexie. My attempts to use where result in the following error:

  console.warn node_modules/dexie/dist/dexie.js:1273
    Unhandled rejection: TypeError: Cannot read property 'bound' of undefined
        at makeIDBKeyRange (node_modules/dexie/src/dbcore/dbcore-indexeddb.ts:112:21)
        at node_modules/dexie/src/dbcore/dbcore-indexeddb.ts:314:31
...

Here's the code that I used:

import Dexie from "dexie";
// @ts-ignore there is not a type for the fake indexeddb
import indexedDB from 'fake-indexeddb';

class TestDatabase extends Dexie {
  constructor() {
    super("test");
    this.version(1).stores({
      data: "id, name"
    });
  }
}

test("dexie", async ()=>{
  Dexie.dependencies.indexedDB = indexedDB;
  const db = new TestDatabase();
  await db.table("data").put({id: "x", name: "xname"});
  const x=await db.table("data").get("x");
  expect (x.name).toEqual("xname");

  const x2=await db.table("data").where("id").equals("x").toArray()
  expect (x2[0].name).toEqual("xname");
})

The test appears to be failing at the call to toArray(). How can I get this test to pass?

1
  • Try removing .toArray() it looks like that's for passing a callback. Commented Oct 22, 2020 at 5:30

2 Answers 2

1

I think you're missing to set Dexie.dependencies.IDBKeyRange also.

Fake-indexeddb recommends to do the following to integrate fake-indexeddb in node:

const Dexie = require("dexie");
require("fake-indexeddb/auto");

const db = new Dexie("MyDatabase");

See their docs

Sign up to request clarification or add additional context in comments.

Comments

1

Thanks @David. The docs landed me to this solution. It runs without error:

import Dexie from "dexie";
// @ts-ignore there is not a type for the fake indexeddb
import indexedDB from 'fake-indexeddb';

class TestDatabase extends Dexie {
    constructor() {
        const IDBKeyRange=require("fake-indexeddb/lib/FDBKeyRange");
        super("test",{
            indexedDB: indexedDB,
            IDBKeyRange: IDBKeyRange,
        });
        this.version(1).stores({
            data: "id, name"
        });
    }
}


test("dexie", async ()=>{
    const db = new TestDatabase();
    await db.table("data").put({id: "x", name: "xname"});
    const x=await db.table("data").get("x");
    expect (x.name).toEqual("xname");

    const x2=await db.table("data").where("id").equals("x").toArray();
    expect (x2[0].name).toEqual("xname");
})

1 Comment

Great. Your solution is less obtrusive than require("fake-indexeddb/auto").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.