Skip to content

Add support for the direction option in getAll and getAllKeys#350

Open
ghazi-git wants to merge 1 commit intojakearchibald:mainfrom
ghazi-git:direction-for-getAll-getAllKeys
Open

Add support for the direction option in getAll and getAllKeys#350
ghazi-git wants to merge 1 commit intojakearchibald:mainfrom
ghazi-git:direction-for-getAll-getAllKeys

Conversation

@ghazi-git
Copy link
Copy Markdown

The indexedDB spec added support for calling the getAll and getAllKeys methods using an options objects containing query, count and (the new) direction. Here are the links to the updated spec for store.getAll and store.getAllKeys and the updated spec for index.getAll and index.getAllKeys. Notice how direction's type is next|prev for the store methods, while it is next|prev|nextunique|prevunique for the index methods.

For this PR, I chose to add new overloads to make it clear that the separate count argument is ignored when passing an options object. I also added usage examples in the tests, mostly to make sure the new options object works as expected (tested the functionality with chrome 141 beta). In tests, feature detection using if ('getAllRecords' in store) is based on the recommendation in the original proposal.

Now the problem: I expected type assertions (like the one below) for the first argument of the getAll and getAllKeys to fail, but that didn't happen.

idb/test/main.ts

Lines 1200 to 1205 in 77dd8be

typeAssert<
IsExact<
Parameters<typeof store1.getAll>[0],
string | IDBKeyRange | undefined | null
>
>(true);

Specifically, I expected that I need to append | { query?: string | IDBKeyRange | null; count?: number; direction?: 'next' | 'prev'} since the first argument can be an options object. However, it turns out typescript keeps inferring the type as before since the new overload is added first in the interface.
So, @jakearchibald what do you think we should do? one option is to leave things as they are now, especially that the new overloads avoid library users specifying the separate count argument by mistake. The other option is to extend the union type of query (and maybe rename query to queryOrOptions like in the spec) instead of using function overloads. This should lead to updating the type assertions in tests, but does not prevent users from passing count by mistake. Or maybe there is another option.

add overloads of getAll and getAllKeys to enable calling the methods by passing an options object containing query, count and (the new) direction. Also, update the tests to include an example of calling the methods by passing an options object.
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

Successfully merging this pull request may close these issues.

1 participant