Usage
Use the KV SDK to set, get, delete, clear, and list key-value pairs.
ViteHub exposes the KV store through an Unstorage instance.
Importing the KV storage
Use @vitehub/kv to import the KV storage:
import { kv } from '@vitehub/kv'
kv is also auto-imported on the server side, so you can use it directly in Nitro routes and handlers.Set an item
import { kv } from '@vitehub/kv'
await kv.set('vue', { year: 2014 })
await kv.set('vue:nuxt', { year: 2016 })
Expiration
You can set a TTL in seconds when the active driver supports it.
import { kv } from '@vitehub/kv'
await kv.set('vue:nuxt', { year: 2016 }, { ttl: 60 })
Get an item
import { kv } from '@vitehub/kv'
const vue = await kv.get('vue')
Has an item
import { kv } from '@vitehub/kv'
const hasAngular = await kv.has('angular')
const hasVue = await kv.has('vue')
Delete an item
import { kv } from '@vitehub/kv'
await kv.del('react')
Clear the KV namespace
import { kv } from '@vitehub/kv'
await kv.clear()
Pass a prefix when you want to clear only one subset of keys.
import { kv } from '@vitehub/kv'
await kv.clear('react')
List all keys
import { kv } from '@vitehub/kv'
const keys = await kv.keys()
Pass a prefix when you want to list only matching keys.
import { kv } from '@vitehub/kv'
const vueKeys = await kv.keys('vue')