sqjson
sqjson is a simple, embedded, file-based key-value database using JSON values and memory-mapped files
(like SQLite, but for JSON). Written in pure Rust with minimal dependencies (serde, memmap2, thiserror).
🚀 Features
- Embedded, single-file storage (
.db) - Fast memory-mapped I/O with
memmap2 - Store structured data as JSON using
serde_json - Key-value API:
put,get,delete,flush - Field-level access:
get_field(key, field) - Secondary indexes for field-based queries:
query(field, value) - Range queries for numbers and strings:
range_query(field, min, max) - Filter records using a custom predicate:
filter(|val| ...) - Paginated queries:
query_page(field, value, limit, offset) - Export query results to JSON:
export_query(field, value, path) - Export full database snapshot:
export_to_file(path) - Show all records (
show_all) for debugging - Minimal dependencies (
serde,memmap2,thiserror)
📦 Installation
Add to your Rust project with:
Or add manually to your Cargo.toml:
🛠 Usage Examples
;
;
)
🔧 API Overview
| Method | Description |
|---|---|
YourDb::open(path) |
Open or create a database file |
put(key, value) |
Insert or update a JSON value under a string key |
get(key) |
Retrieve a JSON value by key |
get_field(key, field) |
Retrieve a specific JSON field of a record |
query(field, value) |
Return list of keys where JSON field equals value |
range_query(field, min, max) |
Find keys where a JSON field falls in a numeric range |
search_contains(field, substring) |
Find keys where a text field contains a given substring |
update_field(key, field, new_value) |
Update a single JSON field without replacing the whole record |
filter(predicate) |
Return key-value pairs matching a custom predicate |
query_page(field, value, limit, offset) |
Paginated query results by field value |
export_query(field, value, path) |
Export query results to JSON file |
delete(key) |
Remove a record by key |
flush() |
Persist index and data pages to disk |
show_all() |
Print all stored key-value pairs (for debugging) |
export_to_file(path) |
Export entire DB contents to a pretty JSON file |
📁 File Format
Page 0: Stores the index (mapping keys to page IDs)
Page 1 and onwards: Store actual JSON-encoded data
Fixed page size (default 4096 bytes)
Data stored as length-prefixed JSON blobs
📃 License
MIT OR Apache-2.0
👤 Author
Hafiz Ali Raza