Version v2.2.0 Size 0.8 KB gzip Dependencies Zero dependencies
Why Vault?
Vault gives browser and SQLite persistence one typed schema while keeping backend guarantees explicit. Use VaultStore for portable CRUD and observation; choose IndexedDB or the opt-in SQLite subpath when you need atomic transactions or lazy iteration.
ts
// Before
localStorage.setItem('theme', JSON.stringify({ value: 'dark' }));
const theme = JSON.parse(localStorage.getItem('theme') ?? '{}').value;
// After
await store.put('preferences', { id: 'theme', value: 'dark' });
const theme = await store.get('preferences', 'theme');| Feature | Vault | Raw Web Storage | Dexie |
|---|---|---|---|
| Bundle size | 0.8 KB | Browser built-in | Extra dependency |
| Runtime dependencies | |||
| Typed schema and keys | Application-defined | ||
| Portable Memory/Web Storage API | IndexedDB only | ||
| Explicit atomic transactions | IndexedDB capability | ||
| Driver-neutral SQLite | Opt-in subpath |
Use Vault when you need typed browser persistence or application-owned SQLite with one portable CRUD API and explicit storage capabilities.
Consider raw Web Storage when you only persist one or two unstructured values. Consider Dexie when you need a broader IndexedDB ecosystem.
Installation
sh
pnpm add @vielzeug/vaultsh
npm install @vielzeug/vaultsh
yarn add @vielzeug/vaultQuick Start
Define a schema, create a portable store, and dispose it with its owner.
ts
import { table } from '@vielzeug/vault';
import { createLocalStorage } from '@vielzeug/vault/local-storage';
const store = createLocalStorage({
name: 'app-v2',
schema: { preferences: table<{ id: string; theme: 'dark' | 'light' }>('id') },
});
try {
await store.put('preferences', { id: 'theme', theme: 'dark' });
console.log(await store.get('preferences', 'theme'));
} finally {
await store.dispose();
}Features
table()defines typed records with portable string or number keys./memory,/local-storage, and/session-storagereturn portableVaultStoreinstances without loading other adapters.observe()emits current and changed table snapshots.ttlcreates validated expiration durations./indexeddbreturnsIndexedDbVaultStorewithbatch()anditerate().createSQLite()is an opt-in, driver-neutral subpath for Node, Bun, and Deno SQLite drivers./indexeddbalso exportsdefineMigration()for schema upgrades.scheduleExpiredPrune()removes stale TTL entries on an owned schedule.