Skip to content

Commit e6fa288

Browse files
committed
chore(kv): use binding in local
1 parent dcd1e38 commit e6fa288

File tree

1 file changed

+11
-18
lines changed
  • _nuxthub/server/utils

1 file changed

+11
-18
lines changed

_nuxthub/server/utils/kv.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
import type { Storage } from 'unstorage'
22
import { createStorage } from 'unstorage'
3-
import fsDriver from 'unstorage/drivers/fs'
43
import httpDriver from 'unstorage/drivers/http'
54
import cloudflareKVBindingDriver from 'unstorage/drivers/cloudflare-kv-binding'
6-
import { join } from 'pathe'
75
import { joinURL } from 'ufo'
86

97
let _kv: Storage
108

119
export function useKV () {
1210
if (!_kv) {
13-
// TODO: same as database
14-
if (process.env.KV) {
15-
// kv in production
16-
_kv = createStorage({
17-
driver: cloudflareKVBindingDriver({
18-
binding: process.env.KV
19-
})
20-
})
21-
} else if (import.meta.dev && process.env.NUXT_HUB_URL) {
11+
if (import.meta.dev && process.env.NUXT_HUB_URL) {
2212
console.log('Using KV remote namespace...')
2313
// Use https://unstorage.unjs.io/drivers/http
2414
_kv = createStorage({
@@ -29,14 +19,17 @@ export function useKV () {
2919
}
3020
})
3121
})
32-
} else if (import.meta.dev) {
33-
// local kv in development
34-
console.log('Using KV local namespace...')
35-
_kv = createStorage({
36-
driver: fsDriver({ base: join(process.cwd(), './.hub/kv') })
37-
})
3822
} else {
39-
throw new Error('No KV configured for production')
23+
const binding = process.env.KV || globalThis.__env__?.KV || globalThis.KV
24+
if (binding) {
25+
_kv = createStorage({
26+
driver: cloudflareKVBindingDriver({
27+
binding
28+
})
29+
})
30+
} else {
31+
throw createError('Missing Cloudflare binding KV')
32+
}
4033
}
4134
}
4235

0 commit comments

Comments
 (0)