Skip to content

Commit

Permalink
update driver
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 5, 2023
1 parent 70d012f commit c8e12de
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/drivers/vercel-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,48 @@ import type { RedisConfigNodejs } from "@upstash/redis";

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

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

export default defineDriver<RedisOptions>((opts) => {
export default defineDriver<VercelKVOptions>((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);
let _client: VercelKV;
const getClient = () => {
if (!_client ) {
_client = createClient(opts);
}
return _connection;
return _client;
};

return {
hasItem(key) {
return getConnection().exists(r(key)).then(Boolean);
return getClient().exists(r(key)).then(Boolean);
},
getItem(key) {
return getConnection().get(r(key));
return getClient().get(r(key));
},
setItem(key, value) {
return getConnection()
return getClient()
.set(r(key), value)
.then(() => {});
},
removeItem(key) {
return getConnection()
return getClient()
.del(r(key))
.then(() => {});
},
getKeys(base) {
return getConnection().keys(r(base, "*"));
return getClient().keys(r(base, "*"));
},
async clear(base) {
const keys = await getConnection().keys(r(base, "*"));
const keys = await getClient().keys(r(base, "*"));
if (keys.length === 0) {
return;
}
return getConnection()
return getClient()
.del(...keys)
.then(() => {});
},
Expand Down

0 comments on commit c8e12de

Please sign in to comment.