Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add vercel-kv driver #213

Merged
merged 18 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/content/6.drivers/vercel-kv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Vercel KV

Store data in a Vercel [Redis](https://redis.com/) KV store.

```js
import { createStorage } from "unstorage";
import vercelDriver from "unstorage/drivers/vercel-kv";

const storage = createStorage({
driver: vercelDriver({
base: "unstorage",
url: "https://<your-project-slug>.kv.vercel-storage.com",
token: "<your secret token>",
}),
});
```

**Options:**

- `base`: Optional prefix to use for all keys. Can be used for namespacing.
- `url`: Url to use for connecting to your Vercel KV store. It has the format `https://<your-project-slug>.kv.vercel-storage.com`

See [@upstash/redis](https://docs.upstash.com/redis/sdks/javascriptsdk/advanced) for all available options.

To use, you will need to install `@vercel/kv` in your project:

```json
{
"dependencies": {
"@vercel/kv": "latest"
}
}
```
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"@types/jsdom": "^21.1.1",
"@types/mri": "^1.1.1",
"@types/node": "^18.15.12",
"@upstash/redis": "^1.20.5",
"@vercel/kv": "^0.1.1",
"@vitejs/plugin-vue": "^4.1.0",
"@vitest/coverage-c8": "^0.30.1",
"@vue/compiler-sfc": "^3.2.47",
Expand Down Expand Up @@ -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": {
Expand All @@ -120,7 +124,13 @@
},
"@planetscale/database": {
"optional": true
},
"@upstash/redis": {
"optional": true
},
"@vercel/kv": {
"optional": true
}
},
"packageManager": "pnpm@8.3.1"
}
}
44 changes: 44 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/drivers/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ export function normalizeKey(key: string | undefined): string {
}
return key.replace(/[/\\]/g, ":").replace(/^:|:$/g, "");
}

export function joinKeys(...keys: string[]) {
return normalizeKey(keys.filter(Boolean).join(":"));
}
54 changes: 54 additions & 0 deletions src/drivers/vercel-kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { createClient } from "@vercel/kv";
import type { VercelKV } from "@vercel/kv";
import type { RedisConfigNodejs } from "@upstash/redis";
pi0 marked this conversation as resolved.
Show resolved Hide resolved

import { defineDriver, normalizeKey, joinKeys } from "./utils";

export interface RedisOptions extends RedisConfigNodejs {
base?: string;
}

export default defineDriver<RedisOptions>((opts) => {
const base = normalizeKey(opts?.base);
const r = (...keys: string[]) => joinKeys(base, ...keys);

let _connection: VercelKV;
const getConnection = () => {
if (!_connection) {
// `connect` configures a connection class rather than initiating a connection
_connection = createClient(opts);
}
return _connection;
};

return {
hasItem(key) {
return getConnection().exists(r(key)).then(Boolean);
},
getItem(key) {
return getConnection().get(r(key));
},
setItem(key, value) {
return getConnection()
.set(r(key), value)
.then(() => {});
},
removeItem(key) {
return getConnection()
.del(r(key))
.then(() => {});
},
getKeys(base) {
return getConnection().keys(r(base, "*"));
},
async clear(base) {
const keys = await getConnection().keys(r(base, "*"));
if (keys.length === 0) {
return;
}
return getConnection()
.del(...keys)
.then(() => {});
},
};
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const builtinDrivers = {
redis: "unstorage/drivers/redis",
azureKeyVault: "unstorage/drivers/azure-key-vault",
sessionStorage: "unstorage/drivers/session-storage",
vercelKV: "unstorage/drivers/vercel-kv",
};

export type BuiltinDriverName = keyof typeof builtinDrivers;