Skip to content

Instantly share code, notes, and snippets.

@johannschopplich
Last active June 27, 2024 15:27
Show Gist options
  • Save johannschopplich/c87485d8a53e017b991aabd4711ca0ab to your computer and use it in GitHub Desktop.
Save johannschopplich/c87485d8a53e017b991aabd4711ca0ab to your computer and use it in GitHub Desktop.
IndexedDB storage wrapper for VueUse
import { useStorageAsync } from "@vueuse/core";
import { get, set, del } from "idb-keyval";
export const STORAGE_KEY_PREFIX = "app.session.";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useIdbStorage<T = any>(key: string, initialValue: T) {
return useStorageAsync(`${STORAGE_KEY_PREFIX}${key}`, initialValue, {
async getItem(key: string) {
return (await get<string>(key)) ?? null;
},
async setItem(key: string, value: string) {
await set(key, value);
},
async removeItem(key: string) {
await del(key);
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment