Skip to content

Commit

Permalink
chore(kv): use binding in local
Browse files Browse the repository at this point in the history
  • Loading branch information
smarroufin committed Feb 5, 2024
1 parent dcd1e38 commit e6fa288
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions _nuxthub/server/utils/kv.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import type { Storage } from 'unstorage'
import { createStorage } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs'
import httpDriver from 'unstorage/drivers/http'
import cloudflareKVBindingDriver from 'unstorage/drivers/cloudflare-kv-binding'
import { join } from 'pathe'
import { joinURL } from 'ufo'

let _kv: Storage

export function useKV () {
if (!_kv) {
// TODO: same as database
if (process.env.KV) {
// kv in production
_kv = createStorage({
driver: cloudflareKVBindingDriver({
binding: process.env.KV
})
})
} else if (import.meta.dev && process.env.NUXT_HUB_URL) {
if (import.meta.dev && process.env.NUXT_HUB_URL) {
console.log('Using KV remote namespace...')
// Use https://unstorage.unjs.io/drivers/http
_kv = createStorage({
Expand All @@ -29,14 +19,17 @@ export function useKV () {
}
})
})
} else if (import.meta.dev) {
// local kv in development
console.log('Using KV local namespace...')
_kv = createStorage({
driver: fsDriver({ base: join(process.cwd(), './.hub/kv') })
})
} else {
throw new Error('No KV configured for production')
const binding = process.env.KV || globalThis.__env__?.KV || globalThis.KV
if (binding) {
_kv = createStorage({
driver: cloudflareKVBindingDriver({
binding
})
})
} else {
throw createError('Missing Cloudflare binding KV')
}
}
}

Expand Down

0 comments on commit e6fa288

Please sign in to comment.