@teactjs/storage
storagePlugin, useStorage, useGlobalStorage, and the StorageDriver interface with built-in memory and file drivers.
Persistent key/value storage for Teact bots. Register storagePlugin once, then read and write
state from any component with useStorage, or reach the raw driver with useGlobalStorage.
bun add @teactjs/storageExports
| Export | Kind | Description |
|---|---|---|
storagePlugin | plugin | Registers a storage driver on the bot. |
useStorage | hook | useStorage<T>(key, defaultValue) → [value, setValue], reactive per key. |
useGlobalStorage | hook | Returns the underlying StorageDriver for manual reads/writes. |
MemoryDriver | class | In-memory driver (default; data is lost on restart). |
FileDriver | class | JSON-file driver that persists to disk. |
StorageDriver | type | Interface for writing a custom driver. |
StoragePluginOptions | type | Options for storagePlugin. |
Register the plugin
storagePlugin({ driver, path }) — driver is 'memory' (default), 'file', or a custom
StorageDriver. For the file driver, path is a JSON file, not a directory:
// teact.config.ts
import { defineConfig } from "@teactjs/core";
import { storagePlugin } from "@teactjs/storage";
export default defineConfig({
plugins: [storagePlugin({ driver: "file", path: ".teact/storage.json" })],
});Read and write state
useStorage is reactive per key — updating it re-renders the component:
import { useStorage } from "@teactjs/storage";
const [favorites, setFavorites] = useStorage<string[]>("favorites", []);useGlobalStorage() returns the driver itself: get, set, delete, has, keys, clear.
StorageDriver interface
A driver implements six synchronous methods:
interface StorageDriver {
get<T>(key: string): T | undefined;
set<T>(key: string, value: T): void;
delete(key: string): void;
has(key: string): boolean;
clear(): void;
keys(): string[];
}Pass an instance as driver to plug in any backend (Redis, SQLite, Supabase, …):
import { RedisDriver } from "@teactjs/storage-redis";
storagePlugin({ driver: new RedisDriver({ url: "redis://localhost:6379" }) });On edge runtimes there is no filesystem: the 'file' driver warns and degrades to memory, which
does not persist across isolate freezes. Provide a driver backed by durable async storage (KV,
Redis, a DB) instead.
See the Storage guide for driver patterns and the edge caveat in full.