dumbmatter / fakeIndexedDB

A pure JS in-memory implementation of the IndexedDB API
Apache License 2.0
585 stars 70 forks source link

`DataError: Data provided to an operation does not meet requirements.` when making a simple dexie query #38

Closed stutrek closed 4 years ago

stutrek commented 4 years ago

Thank you for making this library, we've been using it since we started using indexeddb.

This codesandbox succeeds with in-browser IndexedDB but fails with fake-indexeddb. I found it while looking into an issue where not all results are returned from a table with a compound primary index.

https://codesandbox.io/s/dexie-bug-tr8ml

I don't know how to reduce this to a more minimal case for fake-indexeddb, but I have also opened a ticket on dexie. https://github.com/dfahlander/Dexie.js/issues/954

dumbmatter commented 4 years ago

I tried to reproduce your error in Node.js, with this script:

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

const db = new Dexie("MyDatabase", { indexedDB: indexedDB });

db.version(1).stores({
  friends: "++id, firstName, age"
});

async function doStuff() {
  await db.open();

  await db.friends.add({
    firstName: "Camilla",
    lastName: "Smith",
    age: 25,
    street: "East 13:th Street"
  });

  await db.friends.add({
    firstName: "Camilla",
    lastName: "Jones",
    age: 25,
    street: "East 13:th Street"
  });

  console.log(await db.friends.toArray());

  await db.friends
    .where("firstName")
    .equals("Camilla")
    .toArray()
    .then(console.log);
}

doStuff().catch(console.error);

That produces a different error, TypeError: Cannot read property 'only' of undefined

You can fix that by changing the first few lines either to this:

const Dexie = require("dexie");
const indexedDB = require("fake-indexeddb");
const IDBKeyRange = require("fake-indexeddb/lib/FDBKeyRange");

const db = new Dexie("MyDatabase", { indexedDB: indexedDB, IDBKeyRange: IDBKeyRange });

or to this:

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

const db = new Dexie("MyDatabase");

To make it work in CodeSandbox I had to use different variable names because it doesn't seem to like overriding the browser built-ins: https://codesandbox.io/s/dexie-bug-y1cjl