Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useIDBKeyval.md
1---2category: '@Integrations'3---45# useIDBKeyval67Wrapper for [`idb-keyval`](https://www.npmjs.com/package/idb-keyval).89## Install idb-keyval as a peer dependency1011```bash12npm install idb-keyval@^613```1415## Usage1617```ts18import { useIDBKeyval } from '@vueuse/integrations/useIDBKeyval'1920// bind object21const { data: storedObject, isFinished } = useIDBKeyval('my-idb-keyval-store', { hello: 'hi', greeting: 'Hello' })2223// update object24storedObject.value.hello = 'hola'2526// bind boolean27const flag = useIDBKeyval('my-flag', true) // returns Ref<boolean>2829// bind number30const count = useIDBKeyval('my-count', 0) // returns Ref<number>3132// awaiting IDB transaction33await count.set(10)34console.log('IDB transaction finished!')3536// delete data from idb storage37storedObject.value = null38```3940## Type Declarations4142```ts43interface Serializer<T> {44read: (raw: unknown) => T45write: (value: T) => unknown46}47export interface UseIDBOptions<T> extends ConfigurableFlush {48/**49* Watch for deep changes50*51* @default true52*/53deep?: boolean54/**55* On error callback56*57* Default log error to `console.error`58*/59onError?: (error: unknown) => void60/**61* Use shallow ref as reference62*63* @default false64*/65shallow?: boolean66/**67* Write the default value to the storage when it does not exist68*69* @default true70*/71writeDefaults?: boolean72/**73* Custom data serialization74*/75serializer?: Serializer<T>76}77export interface UseIDBKeyvalReturn<T> {78data: RemovableRef<T>79isFinished: ShallowRef<boolean>80set: (value: T) => Promise<void>81}82/**83*84* @param key85* @param initialValue86* @param options87*/88export declare function useIDBKeyval<T>(89key: IDBValidKey,90initialValue: MaybeRefOrGetter<T>,91options?: UseIDBOptions<T>,92): UseIDBKeyvalReturn<T>93```94