Indexes consistently fail to be created correctly #43
NanoSQL is looking great, congrats. I've plugged it into my app to replace Dexie because it was significantly quicker in testing.
However, I'm upserting some records into IndexedDB through NanoSQL and I'm finding that some rows are not being indexed as expected.
In my test, I have a collection of 4 records that I'm upserting and every time I perform the operation, the 3rd record, no matter what it is, is not indexed.
Here's the model:
This is the data:
And this is the code containing the upsert operation:
Here's the resulting index in _personnelLocation_idx_personnel_id:
Am I losing my marbles or is there some bizarre issue at play here?
The text was updated successfully, but these errors were encountered:
You're probably losing your marbles, but I doubt that's related to this issue.
So I took everything in your examples above and wasn't able to recreate the issue in a codepen using the latest nanoSQL:
Even enabling IndexedDB didn't cause the issue to come up. Could you help me recreate the issue? Once we see it happening in a codepen fixing it should be easy.
Yeah, who am I kidding? Lost my marbles years back.
This pen seems to replicate the issue reliably in Chrome Version 66.0.3359.181:
I changed the upsert to be within a forEach loop as I had it and modified the mode to IDB and the issue is occurring for me consistently. Also changed the database id so that it's not referencing a previously indexed table on an existing db.
I'd work around the issue by dropping the data array into the upsert, but this is part of an orm where each record can save itself to the db, so that won't work so well.
Not sure if this is worth mentioning, but this issue does not occur in Firefox DE in a standard tab. However, in a private Firefox window the codepen throws an error. This same error also appears in Firefox (no private window this time) when testing my app on localhost:
The secondary index issue is happening because you're running the upserts in parallel. After each upsert the database goes through and makes the needed changes to the indexes before completing the query, when you run all the queries at once you run the risk of the secondary index reads/writes getting out of sync.
Maybe I should setup some sort of queue on the secondary index reads/writes so that if two writes happen at exactly the same time these kinds of issues don't show up.
So temporarily the issue can be resolved by just making sure your writes are chained one after another, instead of done all at once. When you pass an array into the upsert command nanoSQL does this for you.
The IndexedDB issue seems like it's having trouble/unable to open the IndexedDB database. Wha'ts the rest of the debug object at the first line? That might help us figure out what's going on.
A queue is probably a good idea. This scenario is likely to pop up again in the future.
Given that NanoSQL was acting as an adapter for my orm where the upserts are called from within the save method of a record, chaining isn't possible without an architecture overhaul and I'm operating on a tight timeframe.
I've switched to RxDB as my adapter and it's working fine.
Regarding that Firefox error - apparently Firefox blocks IndexedDB from within a private window.
Thanks for your assistance, has been much appreciated.
Awesome, glad you got your project working!
I just pushed
Since you've moved to another project I'll mark this resolved, thanks for the heads up!