Skip to content

Commit

Permalink
feat: support a prefix for storage keys
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf committed Nov 13, 2024
1 parent 5e1295b commit 18d1558
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export interface ConversationKeyStorage<C extends Context, S> {
type: "key";
/** An optional version for the data, defaults to `0` */
version?: string | number;
/** An optional prefix to prepend to the storage key */
prefix?: string;
/** An optional storage key function, defaults to `ctx.chatId` */
getStorageKey?(ctx: C): string | undefined;
/** The underlying storage that defines how to read and write raw data */
Expand All @@ -183,7 +185,6 @@ function defaultStorage<C extends Context, S>(): ConversationKeyStorage<C, S> {
const store = new Map<string, VersionedState<S>>();
return {
type: "key",
getStorageKey: defaultStorageKey,
adapter: {
read: (key) => store.get(key),
write: (key, state) => void store.set(key, state),
Expand Down Expand Up @@ -218,9 +219,10 @@ export function uniformStorage<C extends Context, S>(
const { versionify, unpack } = pinVersion(version);

if (storage.type === "key") {
const { getStorageKey = defaultStorageKey, adapter } = storage;
const { getStorageKey = defaultStorageKey, prefix = "", adapter } =
storage;
return (ctx: C) => {
const key = getStorageKey(ctx);
const key = prefix + getStorageKey(ctx);
return key === undefined
? {
read: () => undefined,
Expand Down

0 comments on commit 18d1558

Please sign in to comment.