Guide
Custom Driver
It is possible to extend unstorage by creating a custom driver.
Explore src/drivers to inspire how to implement them. Methods can
import { createStorage, defineDriver } from "unstorage";
const myStorageDriver = defineDriver((options) => {
return {
name: "my-custom-driver",
options,
async hasItem(key, _opts) {},
async getItem(key, _opts) {},
async setItem(key, value, _opts) {},
async removeItem(key, _opts) {},
async getKeys(base, _opts) {},
async clear(base, _opts) {},
async dispose() {},
async watch(callback) {},
};
});
const storage = createStorage({
driver: myStorageDriver(),
});
Some important notes:
- Keys should be normalized following
foo:bar
convention - Remove any open watcher and handlers in
dispose()
- Returning a promise is optional, you can return a direct value (see memory driver)
- You don't have acces to the mount base
- Value returned by
getItem
can be a serializableobject
orstring
- When setting
watch
method, unstorage default handler will be disabled. You are responsible to emit event ongetItem
,setItem
andremoveItem
.