[](https://crates.io/crates/sqjson)
[](https://docs.rs/sqjson)
# sqjson
**sqjson** is a simple, embedded, file-based key-value database using JSON values and memory-mapped files (like SQLite, but for JSON). It's written in pure Rust and has zero dependencies beyond `serde`, `memmap2`, and `thiserror`.
## 🚀 Features
- Lightweight and fast
- File-based — stores data in a single `.db` file
- JSON values for flexibility
- Key-based storage and retrieval
- Backed by memory-mapped I/O
- Easy to embed in any Rust project
---
## 📦 Installation
Add to your Rust project:
```sh
cargo add sqjson
```
Or add manually to your `Cargo.toml`:
```toml
[dependencies]
sqjson = "0.1"
```
---
## 🛠 Usage
```rust
use sqjson::{YourDb, DbError};
use serde_json::json;
fn main() -> Result<(), DbError> {
let mut db = YourDb::open("mydata.db")?;
db.put("user:1", &json!({ "name": "Alice", "age": 30 }))?;
db.put("user:2", &json!({ "name": "Bob", "age": 25 }))?;
db.flush()?; // Persist to disk
if let Some(user) = db.get("user:1")? {
println!("Got user: {}", user);
}
Ok(())
}
```
---
## 🔧 API Overview
| `YourDb::open()` | Open or create a database file |
| `put()` | Insert or update a key with a JSON value |
| `get()` | Retrieve a JSON value by key |
| `flush()` | Write index and pages to disk |
| `show_all()` | Print all key-value pairs (debug tool) |
---
## 📁 File Format
- Page 0: Stores the index (key → page ID)
- Page 1+: Stores actual JSON-encoded data
- Each page is fixed-size (`4096` bytes default)
---
## 📃 License
MIT OR Apache-2.0
---
## 👤 Author
Made with ❤️ by Hafiz Ali Raza