Last active
June 27, 2024 15:27
-
-
Save johannschopplich/c87485d8a53e017b991aabd4711ca0ab to your computer and use it in GitHub Desktop.
IndexedDB storage wrapper for VueUse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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