Storage
IStorage interface and three backends: JSON file, SQLite, and in-memory.
Overview
@consensus-tools/storage provides the storage abstraction layer for consensus-tools. It defines the IStorage interface and three implementations: JSON file, SQLite, and in-memory. All other packages that need persistence depend on this package.
Moved from core in v0.9.0
Storage classes were previously re-exported from @consensus-tools/core. As of v0.9.0, import them directly from @consensus-tools/storage.
Installation
Requires @consensus-tools/schemas (bundled — installed automatically).
Quick start
For config-driven setup:
Storage backends
| Backend | Persistence | Best for |
|---|---|---|
JsonStorage | File (JSON) | Local development, single-process deployments |
SqliteStorage | SQLite database | Production use with concurrent access |
MemoryStorage | In-memory (lost on exit) | Tests, quick prototyping, CI environments |
API reference
IStorage interface
All backends implement the same contract:
Use update() for writes
Prefer update() over getState() + saveState() separately. update() provides mutex protection against concurrent writes and is the primary write API.
Direct usage
Storage caps
Limit storage growth by trimming old entries:
Exports reference
| Export | Description |
|---|---|
createStorage(config) | Factory — creates the right backend from config |
JsonStorage | File-based JSON storage |
SqliteStorage | SQLite-based storage (requires optional better-sqlite3) |
MemoryStorage | In-memory storage for dev/test |
IStorage | Storage interface type |
StorageCaps | Storage capacity limits type |
defaultState() | Returns an empty default StorageState |
applyStorageCaps(state, caps) | Trim state to capacity limits (FIFO) |
Mutex | Mutex for concurrent access control |