Skip to content

Commit

Permalink
fix: support base in getKeys and clear
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 2, 2023
1 parent 04db51a commit a604db5
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/drivers/vercel-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import type { VercelKV } from "@vercel/kv";
import type { RedisConfigNodejs } from "@upstash/redis";

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

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

export default defineDriver<RedisOptions>((opts) => {
let base = opts?.base || "";
if (base && !base.endsWith(":")) {
base += ":";
}
const r = (key: string) => base + key;
const base = normalizeBaseKey(opts?.base);
const r = (...keys: string[]) => joinKeys(base, ...keys);

let _connection: VercelKV;
const getConnection = () => {
Expand Down Expand Up @@ -41,11 +39,11 @@ export default defineDriver<RedisOptions>((opts) => {
.del(r(key))
.then(() => {});
},
getKeys() {
return getConnection().keys(r("*"));
getKeys(base) {
return getConnection().keys(r(base, "*"));
},
async clear() {
const keys = await getConnection().keys(r("*"));
async clear(base) {
const keys = await getConnection().keys(r(base, "*"));
if (keys.length === 0) {
return;
}
Expand Down

0 comments on commit a604db5

Please sign in to comment.