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

Index on Date type does not work #101

Closed
asarbu opened this issue Jun 13, 2024 · 7 comments
Closed

Index on Date type does not work #101

asarbu opened this issue Jun 13, 2024 · 7 comments

Comments

@asarbu
Copy link

asarbu commented Jun 13, 2024

I have the following use case that does not work in Fake IndexedDb:

  1. Create a normal FakeIndexedDb
  2. Create an Object Store with an index on a field of type "Date"
    static upgradeSpendingsDb(db, oldVersion, newVersion, objectStores) { if (!newVersion) { return; } if (oldVersion < newVersion) { objectStores.forEach((objectStore) => { const store = db.createObjectStore(objectStore, { autoIncrement: true }); store.createIndex('byCreatedOn', 'createdOn', { unique: false }); }); } }
  3. Create a class that contains a Date field:
    class CustomObject() { id = undefined; createdOn = undefined; constructor(id, createdOn) { this.id = id; this.createdOn = createdOn; } }
  4. Store an object in that store
    store.put(new CustomObject(1, new Date()));
  5. Retrieve the object using an index:
    const fromDate = new Date(forYear, forMonth, 1); const toDate = new Date(forYear, forMonth + 1, 1); const keyRange = IDBKeyRange.bound(fromDate, toDate, false, true);
  6. Result from cursor is always empty.
  7. The fetch works if I use strings/numbers instead of dates

Is there any other way to build the range or is this a problem in the library?

@dumbmatter
Copy link
Owner

Can you give a minimal reproduction I can actually run? Otherwise it's hard to know if the bug is where you think it is, or possibly caused by something else in your code. So it's often difficult to try to reproduce bugs without code I can run.

Also, if you want to know if this is a difference in behavior between IndexedDB and fake-indexeddb, you could run the same code in the browser and see if it behaves differently there.

@asarbu
Copy link
Author

asarbu commented Jun 13, 2024

Hello!

Sure, no problem.
I am putting the Visual Studio Code project attached here.
Please run npm install and run the Jest tests. For debugging I used the provided launch,json.
Please let me know if there is anything else I can help you with.

PS: The code I have attached works in browser.

Best regards,
Sebastian

IndexedDB_Bug.zip

@dumbmatter
Copy link
Owner

Same bug as #98, if you upgrade to the latest version then it works.

Well, you also have to uncomment those useFakeTimers lines or it hangs forever, I'm not sure what they are doing, but messing with how Node's internal scheduling functions work is tricky business with fake-indexeddb.

@asarbu
Copy link
Author

asarbu commented Jun 13, 2024

It is strange that fake indexeddb halts. This did not happen in the past, when dates were not working.
I suggest analyzing the behaviour further

@asarbu
Copy link
Author

asarbu commented Jun 13, 2024

But thank you for solving my problem.

@dumbmatter
Copy link
Owner

dumbmatter commented Jun 13, 2024

It's kind of a nightmare to handle every possible case of different ways people modify the built-in Node.js timers. v6 changed how that worked #92 which maybe helped in some situations and hurt in others? idk.

At the end of the day, I have to use something to schedule async events. Best solution is for people to just not modify the built-in timers. I know sometimes that's impossible to avoid, but when you're doing stuff like that, you have to be aware that you might break things.

@asarbu
Copy link
Author

asarbu commented Jun 14, 2024

Hello!
Just in case somebody sees this in the future,
If you want the Jest test to stop halting, add the advanceTimers flag.
jest.useFakeTimers({ advanceTimers: true }).setSystemTime(new Date(2002, 0, 2));

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