Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run liveQuery with a custom indexedDb [request] #2046

Open
andymitchell opened this issue Jul 30, 2024 · 1 comment
Open

Run liveQuery with a custom indexedDb [request] #2046

andymitchell opened this issue Jul 30, 2024 · 1 comment

Comments

@andymitchell
Copy link

Goal

I use fake-indexeddb for testing, which works great with Dexie except for problems with liveQuery's apparent* dependence on a global (*I may have misunderstood something!).

I believe it'll be fixed by being able to give liveQuery a reference to a specific indexedDb instance (like new Dexie can with DexieOptions).

How my FakeIndexedDb tests work

I create a new FakeIndexedDb instance per test, and pass it to Dexie.

import { IDBFactory, IDBKeyRange } from "fake-indexeddb";
const _indexedDB = new IDBFactory();
const db = new Dexie('name', {indexedDB: _indexedDB, IDBKeyRange})

I then use live query as normal on db.

What goes wrong

As I add more tests, FakeIndexedDb/Dexie run slower and slower. E.g. several seconds to run 'toArray' on an empty table - but that same test runs in 400ms when run in isolation.

To prove it's some kind of leak somewhere:

  • I'm forced to use import "fake-indexeddb/auto" at the top of the test file, or liveQuery won't work. This is FakeIndexedDb's global. Only liveQuery needs it, as Dexie itself it passed an individual FakeIndexedDb instance. I suspect that global is being marshalled across tests by the test runner, causing the slow down.
  • If I split the tests across separate test files, the Dexie/IDB performance is fine.

Request

I want to pass liveQuery something equivalent to DexieOptions, to give it the FakeIndexedDb instance.

I'd happily make a fork/PR myself, but some pointers of where to start from in ./src/live-query would help enormously (I've not succeeded at even finding any reference to the global indexedDb so far).

@dfahlander
Copy link
Collaborator

liveQuery() is not dependant on the indexedDB being present on the global object actually. It depends on globalEvents which is basically an event set with one single event "x-storagemutated-1". This static variable is part of the "dexie" module so it's also not dependant on anything on the global object. Not sure why it slows down though.

The liveQuery will find the indexedDB implementation anyway because the actual queriers will always use a db instance to do the query. liveQuery can monitory queries from different databases in a single observable as long as all of them are dexie's but they can actually use different indexedDB implementations under the hood.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants