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

feat(useIDBKeyval): new integration - Idb-keyval wrapper #2335

Merged
merged 8 commits into from Nov 8, 2022

Conversation

Harmony222
Copy link
Contributor

@Harmony222 Harmony222 commented Oct 18, 2022

Screen.Recording.2022-10-19.at.9.17.51.AM.mov

Description

Provides a composition wrapper around idb-keyval, which is a key value store api on top of Indexed DB. Gives you typed keys and values without the need of a serializer, adds web worker support, and lets you use more storage space when compared to using the local storage variant.

Additional context

This partially addresses issue #230, but does not add full support for indexed DB (just for typed keys and values).
closes #230


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Harmony222 Harmony222 changed the title Idb-keyval wrapper feat(useIDBKeyval): new integration - Idb-keyval wrapper Oct 19, 2022
Copy link
Contributor

@JessicaSachs JessicaSachs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs tests still. I'm happy to write them, but can I get some 👀 on the signature? Specifically I'm curious about the shallow option vs having the user pass in the ref/shallowRef/computed themselves.

Actually (@Harmony222 says) it seems IndexDB isn't defined for Vitest's environment. Do we want to polyfill IndexDB in Vitest core @antfu?

packages/integrations/useIDBKeyval/demo.vue Outdated Show resolved Hide resolved
*
* @default false
*/
shallow?: boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this option and the associated code by having the user pass in a computed, ref, or shallowRef?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the same pattern as useStorage and useStorageAsync with the shallowRef here, but can change that if needed.

Copy link
Member

@sxzz sxzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could reuse useStorage and implement StorageLike to avoid repeating code. Just like useLocalStorage and useSessionStorage.

return useStorage(key, initialValue, window?.localStorage, options)

@@ -151,6 +156,7 @@
"drauu": "^0.3.2",
"focus-trap": "^7.0.0",
"fuse.js": "^6.6.2",
"idb-keyval": "^6.2.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also requires to be added to peer deps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added to peer deps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sxzz Regarding the first comment, thank you for your patience as I looked into this.
I looked through useLocalStorage and useSessionStorage to understand how they reuse useStorage. I believe you are suggesting that we could use indexDB storage in place of window?.localStorage, the problem I see is that useStorage converts all values to strings, and a big reason our team wanted to use indexDB is so we can store values other than just strings.
I also took a closer look at StorageLike, its get and set functions get and return strings, so I am not sure that using indexDB in this way would provide any gains to using localStorage as is.
Please let me know if I am misunderstanding something.

@sxzz
Copy link
Member

sxzz commented Oct 20, 2022

@JessicaSachs I think we could mock functions of idb-keyval. Since there are unit tests on idb-keyval, we can believe it always works fine. It's easier than mocking IndexDB.

ping @antfu Is this function necessary and acceptable?

@sxzz
Copy link
Member

sxzz commented Oct 20, 2022

By the way, the keyword closes #pr_number needs to be added to the PR body for referencing the related issues.

if (data.value == null)
await del(key)
else
await update(key, () => ({ ...data.value }))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, did this have the same issue that you were messaging me about? The issue with ...someString etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah looks like that one is having an issue too. This is what jimmy and I added when we worked through the bug the other day, it had something to do with proxy objects not able to be stored in index db if I remember correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed this logic to only make copy with spread syntax if data.value is an object or array

@Harmony222
Copy link
Contributor Author

hi @sxzz the contributor generation script is failing

@sxzz
Copy link
Member

sxzz commented Oct 24, 2022

There are some TS type errors

@Harmony222
Copy link
Contributor Author

Thank you @sxzz, I have addressed the TS type error.

Copy link
Contributor

@JessicaSachs JessicaSachs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and the types are now fixed. Once we start using Vitest's browser mode, I think this test will be a good candidate for the browser environment and we can remove the fake-indexeddb module + polyfill

@sxzz
Copy link
Member

sxzz commented Oct 27, 2022

@JessicaSachs @Harmony222
#2335 (review)

@Harmony222
Copy link
Contributor Author

@sxzz
Reposting my comment here as it may have gotten lost.
This is in regards to reusing useStorage.
I believe you are suggesting that we could use indexDB storage in place of window?.localStorage, the problem I see is that useStorage converts all values to strings, and our team wants to use indexDB so we can store values other than just strings.
I also took a closer look at StorageLike, its get and set functions get and return strings, so I am not sure that using indexDB in this way would provide any gains to using localStorage as is.
Please let me know if I am misunderstanding something.

@antfu
Copy link
Member

antfu commented Nov 8, 2022

I am fine to have this as-is for now. Later we could see how we can refactor this with useStorageAsync

@antfu antfu merged commit acd1699 into vueuse:main Nov 8, 2022
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.

useIndexedDb / useIDB
4 participants