-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Pooya Parsa <[email protected]>
- Loading branch information
Showing
5 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Vercel KV | ||
|
||
Store data in a [Vercel KV Store](https://vercel.com/docs/storage/vercel-kv). | ||
|
||
```js | ||
import { createStorage } from "unstorage"; | ||
import vercelKVDriver from "unstorage/drivers/vercel-kv"; | ||
|
||
const storage = createStorage({ | ||
driver: vercelKVDriver({ | ||
// url: "https://<your-project-slug>.kv.vercel-storage.com", // KV_REST_API_URL | ||
// token: "<your secret token>", // KV_REST_API_TOKEN | ||
// base: "test", | ||
// env: "KV", | ||
}), | ||
}); | ||
``` | ||
|
||
To use, you will need to install `@vercel/kv` dependency in your project: | ||
|
||
```json | ||
{ | ||
"dependencies": { | ||
"@vercel/kv": "latest" | ||
} | ||
} | ||
``` | ||
|
||
**Note:** For driver options type support, you might need to install `@upstash/redis` dev dependency as well. | ||
|
||
**Options:** | ||
|
||
- `url`: Rest API URL to use for connecting to your Vercel KV store. Default is `KV_REST_API_URL`. | ||
- `token`: Rest API Token to use for connecting to your Vercel KV store. Default is `KV_REST_API_TOKEN`. | ||
- `base`: [optional] Prefix to use for all keys. Can be used for namespacing. | ||
- `env`: [optional] Flag to customzize environment variable prefix (Default is `KV`). Set to `false` to disable env inference for `url` and `token` options. | ||
|
||
See [@upstash/redis](https://docs.upstash.com/redis/sdks/javascriptsdk/advanced) for all available options. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,8 @@ | |
"@types/jsdom": "^21.1.1", | ||
"@types/mri": "^1.1.1", | ||
"@types/node": "^18.16.3", | ||
"@upstash/redis": "^1.20.5", | ||
"@vercel/kv": "^0.1.1", | ||
"@vitejs/plugin-vue": "^4.2.1", | ||
"@vitest/coverage-c8": "^0.31.0", | ||
"@vue/compiler-sfc": "^3.2.47", | ||
|
@@ -97,7 +99,9 @@ | |
"@azure/identity": "^3.1.4", | ||
"@azure/keyvault-secrets": "^4.7.0", | ||
"@azure/storage-blob": "^12.14.0", | ||
"@planetscale/database": "^1.7.0" | ||
"@planetscale/database": "^1.7.0", | ||
"@upstash/redis": "^1.20.5", | ||
"@vercel/kv": "^0.1.1" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@azure/app-configuration": { | ||
|
@@ -120,7 +124,13 @@ | |
}, | ||
"@planetscale/database": { | ||
"optional": true | ||
}, | ||
"@upstash/redis": { | ||
"optional": true | ||
}, | ||
"@vercel/kv": { | ||
"optional": true | ||
} | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { createClient } from "@vercel/kv"; | ||
import type { VercelKV } from "@vercel/kv"; | ||
import type { RedisConfigNodejs } from "@upstash/redis"; | ||
|
||
import { defineDriver, normalizeKey, joinKeys } from "./utils"; | ||
|
||
export interface VercelKVOptions extends Partial<RedisConfigNodejs> { | ||
base?: string; | ||
env?: false | string; | ||
} | ||
|
||
export default defineDriver<VercelKVOptions>((opts) => { | ||
const base = normalizeKey(opts?.base); | ||
const r = (...keys: string[]) => joinKeys(base, ...keys); | ||
|
||
let _client: VercelKV; | ||
const getClient = () => { | ||
if (!_client) { | ||
const envPrefix = | ||
typeof process !== "undefined" && opts.env !== false | ||
? `${opts.env || "KV"}_` | ||
: ""; | ||
if (!opts.url) { | ||
const envName = envPrefix + "REST_API_URL"; | ||
if (envPrefix && process.env[envName]) { | ||
opts.url = process.env[envName]; | ||
} else { | ||
throw new Error( | ||
`[unstorage] [vercel-kv] missing required 'url' option or '${envName}' env.` | ||
); | ||
} | ||
} | ||
if (!opts.token) { | ||
const envName = envPrefix + "REST_API_TOKEN"; | ||
if (envPrefix && process.env[envName]) { | ||
opts.token = process.env[envName]; | ||
} else { | ||
throw new Error( | ||
`[unstorage] [vercel-kv] missing required 'token' option or '${envName}' env.` | ||
); | ||
} | ||
} | ||
_client = createClient(opts); | ||
} | ||
return _client; | ||
}; | ||
|
||
return { | ||
hasItem(key) { | ||
return getClient().exists(r(key)).then(Boolean); | ||
}, | ||
getItem(key) { | ||
return getClient().get(r(key)); | ||
}, | ||
setItem(key, value) { | ||
return getClient() | ||
.set(r(key), value) | ||
.then(() => {}); | ||
}, | ||
removeItem(key) { | ||
return getClient() | ||
.del(r(key)) | ||
.then(() => {}); | ||
}, | ||
getKeys(base) { | ||
return getClient().keys(r(base, "*")); | ||
}, | ||
async clear(base) { | ||
const keys = await getClient().keys(r(base, "*")); | ||
if (keys.length === 0) { | ||
return; | ||
} | ||
return getClient() | ||
.del(...keys) | ||
.then(() => {}); | ||
}, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters