A minimal JSON-based key-value store for Rust with TTL support and multi-platform compatibility.
TinyKV provides a simple persistent key-value store that works across different Rust environments - from standard desktop applications to embedded systems and WebAssembly. Data is stored in human-readable JSON format with optional automatic expiration.
- JSON file-based persistence with atomic writes
- TTL (time-to-live) expiration for keys
- Auto-save functionality
- Backup support with .bak files
- Multi-platform: std, no_std, and WebAssembly
- Flexible serialization: serde or nanoserde
- Thread-safe operations
Add to your Cargo.toml:
# Default configuration (std + serde)
tinykv = "0.4"
# For embedded systems (no_std + nanoserde)
tinykv = { version = "0.4", default-features = false, features = ["nanoserde"] }
# Minimal configuration (no_std only)
tinykv = { version = "0.4", default-features = false }
# WebAssembly support
tinykv = { version = "0.4", features = ["wasm", "nanoserde"] }use tinykv::TinyKV;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create or open a store with namespace
let mut store = TinyKV::open("data.json")?
.with_auto_save()
.with_namespace("app1");
// Store some data (automatically prefixed with "app1:")
store.set("username", "alice")?;
store.set("count", 42)?;
// Store with expiration (1 hour)
store.set_with_ttl("session_token", "abc123", 3600)?;
// Retrieve data
let username: Option<String> = store.get("username")?;
let count: Option<i32> = store.get("count")?;
// List keys (returns ["username", "count", "session_token"])
let keys = store.keys();
println!("User: {:?}, Count: {:?}", username, count);
Ok(())
}#![no_std]
extern crate alloc;
use tinykv::TinyKV;
fn main() -> Result<(), tinykv::TinyKVError> {
let mut store = TinyKV::new();
store.set("device_id", "ESP32_001")?;
store.set("config", "production")?;
// Serialize for external storage
let serialized = store.to_data()?;
// Later, restore from serialized data
let restored_store = TinyKV::from_data(&serialized)?;
Ok(())
}import { TinyKVWasm } from 'tinykv';
// Use browser localStorage
const store = TinyKVWasm.openLocalStorage('myapp');
store.set('theme', 'dark');
store.setWithTtl('session', 'abc123', 3600); // 1 hour
// Prefix operations
const userKeys = store.listKeys('user:'); // ['user:123', 'user:456']
const deleted = store.clearPrefix('temp:'); // returns countNote: For optimal performance, serve WASM files with Content-Type: application/wasm. TinyKV will automatically fallback to slower instantiation if the MIME type is incorrect.
TinyKV stores data in a structured JSON format:
{
"username": {
"value": "alice",
"expires_at": null
},
"session_token": {
"value": "abc123",
"expires_at": 1725300000
}
}std(default): Enables file I/O, TTL, and standard library featuresserde(default): Uses serde for serialization (maximum compatibility)nanoserde: Uses nanoserde for faster compilation and smaller binarieswasm: Enables WebAssembly support with localStorage backend
TinyKV::open(path)- Open or create file-based storeTinyKV::new()- Create in-memory storeset(key, value)- Store a valueset_with_ttl(key, value, seconds)- Store with expirationget(key)- Retrieve a valueremove(key)- Delete a keycontains_key(key)- Check if key existskeys()- List all keyslist_keys(prefix)- List keys with prefixclear()- Remove all entriesclear_prefix(prefix)- Remove entries with prefixsave()- Manually save to disk
with_auto_save()- Enable automatic savingwith_backup(enabled)- Enable/disable backup fileswith_namespace(prefix)- Set key namespace prefixpurge_expired()- Remove expired entries
| Platform | File I/O | TTL | Auto-save | Serialization |
|---|---|---|---|---|
| std | ✓ | ✓ | ✓ | serde/nanoserde |
| no_std | ✗ | ✗ | ✗ | nanoserde/manual |
| WASM | localStorage | ✓ | ✓ | nanoserde |
Ideal for:
- Application configuration files
- Game save data
- CLI tool settings
- Test data persistence
- Embedded device configuration
- Browser-based applications
- Rapid prototyping
Not recommended for:
- High-performance databases
- Complex relational queries
- Concurrent multi-user access
- Large datasets (>100MB)
Full API documentation is available at docs.rs/tinykv.
- BREAKING: WASM
setWithTtlnow acceptsnumberinstead ofbigint - Added namespace support with
with_namespace(prefix)method - Added prefix operations:
list_keys(prefix)andclear_prefix(prefix) - Enhanced WASM bindings with
listKeys()andclearPrefix()methods - Modernized package.json with
exportsfield andsideEffects: false - Improved WASM performance documentation
- Enhanced no_std support
- Updated documentation and examples
- Improved JSON output formatting
- Added nanoserde support for minimal binary size
- Improved compilation speed
- Enhanced embedded systems compatibility
- Initial release
- Core key-value operations with TTL
- File-based JSON persistence
- Auto-save and backup support
MIT License