Skip to content

Storage Backends

useIndexedDB

Promise-based key-value access to an IndexedDB object store. The store is created automatically if it does not exist.

ts
useIndexedDB<T>(dbName: string, storeName: string, onError?): UseIndexedDBReturn<T>

Methods

MethodSignatureDescription
get(key: IDBValidKey) => Promise<T | null>Read a value by key
set(key: IDBValidKey, value: T) => Promise<void>Write a value
delete(key: IDBValidKey) => Promise<void>Remove a key
keys() => Promise<IDBValidKey[]>All keys in the store
getAll() => Promise<T[]>All values
clear() => Promise<void>Delete everything in the store
count() => Promise<number>Number of entries
transaction<R>(fn: (store: IDBObjectStore) => IDBRequest<R>) => Promise<R>Raw IDB transaction

Example

ts
import { useIndexedDB } from 'vue-storage-kit'

interface Blob {
  id: number
  data: ArrayBuffer
}

const idb = useIndexedDB<Blob>('my-db', 'blobs', (err) => console.error(err))

await idb.set(1, { id: 1, data: buffer })
const blob = await idb.get(1)
console.log(await idb.count())

// Raw transaction
await idb.transaction((store) => store.put({ id: 2, data: buffer2 }, 2))

await idb.delete(1)
await idb.clear()

useIDBRef

A reactive Ref that reads from and writes to a single IndexedDB key. Useful when you want the same reactive API as useStorage but backed by IndexedDB.

ts
useIDBRef<T>(
  dbName: string,
  storeName: string,
  key: IDBValidKey,
  defaultValue: T,
): { value: Ref<T>; isReady: Ref<boolean>; error: Ref<StorageError | null> }
ts
import { useIDBRef } from 'vue-storage-kit'

const { value: draft, isReady } = useIDBRef('editor-db', 'drafts', 'post-42', '')

// Once isReady.value === true, draft reflects the stored value
draft.value = 'Hello, world!' // writes back to IDB automatically

useCookie

A reactive Ref backed by document.cookie. Assigning to the ref sets the cookie. JSON serialization with Date, Map, Set support is included by default.

Imported directly from vue-storage-kit, this is client-only (SSR reads return defaultValue, since there's no document on the server). Inside a Nuxt app with the vue-storage-kit/nuxt module registered, auto-imported useCookie calls resolve to an SSR-aware version instead — same signature, but backed by the H3 request/response on the server. See Nuxt module.

ts
useCookie<T>(name: string, options: CookieOptions<T>): Ref<T>

Options

OptionTypeDefaultDescription
defaultValueTValue returned when the cookie is absent
expiresDate | numberExpiry as a Date or number of days
pathstring'/'Cookie path
domainstringCookie domain
securebooleanAdd Secure flag
sameSite'strict' | 'lax' | 'none'SameSite attribute
httpOnlybooleanSSR only — passed to H3 setCookie; ignored by browsers
serializerSerializer<T>JSONCustom serializer

Examples

Session cookie (expires when browser closes):

ts
const consent = useCookie('cookie-consent', { defaultValue: false })
consent.value = true

Persistent cookie — 30 days:

ts
const locale = useCookie('locale', {
  defaultValue: 'en',
  expires: 30,
  sameSite: 'lax',
})

Nuxt SSR — same API works on server and client:

vue
<script setup lang="ts">
// With the vue-storage-kit/nuxt module registered, this auto-import resolves
// to the SSR-aware useCookie — reads/writes via H3 on the server.
const token = useCookie('auth-token', {
  defaultValue: '',
  secure: true,
  httpOnly: true, // honored server-side via H3 setCookie
  sameSite: 'strict',
})
</script>