| Crates.io | fosk |
| lib.rs | fosk |
| version | 0.1.13 |
| created_at | 2025-09-03 22:58:19.329165+00 |
| updated_at | 2025-09-22 17:41:45.165477+00 |
| description | In-memory SQL-like query engine and lightweight data store for testing and prototyping. |
| homepage | |
| repository | https://github.com/lvendrame/fosk |
| max_upload_size | |
| id | 1823356 |
| size | 629,197 |
fosk is a lightweight embedded SQL engine for Rust applications.
It allows you to define in-memory collections, seed them with JSON objects, and query using a SQL-like syntax.
In your Cargo.toml:
[dependencies]
fosk = "0.1.2"
serde_json = "1"
use fosk::{Db, DbConfig, IdType};
use serde_json::json;
fn main() {
let db = Db::new_db_with_config(DbConfig {
id_type: IdType::Int, // auto-increment IDs
id_key: "id".into(),
});
let people = db.create_collection("People");
// Add JSON
let added = people.add(json!({"name": "Alice", "age": 30})).unwrap();
// Retrieve auto-generated ID (if configured)
let id = added.get("id").unwrap().to_string();
println!("Added person with ID: {}", id);
// Add multiple documents
let added_many = people.add_batch(json!([
{"name": "Bob", "age": 25},
{"name": "Carol", "age": 28}
]));
println!("Added many: {:?}", added_many);
// Query with parameters
let rows = db.query_with_args(
"SELECT id, name, age FROM People WHERE id = ?",
json!(id),
).unwrap();
println!("Query result: {rows:?}");
}
Represents a database.
Defines collection behavior.
let db = Db::new();
// Load all collections from a file
db.load_from_file("./collection.json");
let db = Db::new();
// Load all collections from JSON
db.load_from_json(json!({
"people": [...],
"companies": [...],
"products": [...],
}));
let db = Db::new();
let people = db.create_collection("People");
// Load collection from a file
people.load_from_file("./people.json");
let db = Db::new();
let people = db.create_collection("People");
// Load collection from JSON
people.load_from_json(json!([
{ "id": 1, "full_name": "Alice Johnson", "age": 29, "city": "Porto", "vip": true },
{ "id": 2, "full_name": "Bruno Martins", "age": 34, "city": "Lisboa", "vip": false },
{ "id": 3, "full_name": "Carla Sousa", "age": 41, "city": "Braga", "vip": false },
{ "id": 4, "full_name": "David Pereira", "age": 25, "city": "Coimbra", "vip": true }
]));
let db = Db::new();
...
...
...
let json_value = db.write_to_json();
let db = Db::new();
...
...
...
db.write_to_file("./collections.json");
let db = Db::new();
let people = db.create_collection("People");
...
...
people.write_to_file("./people.json");
Example test seed (see fixtures::seed_db):
pub fn seed_db() -> Db {
let db = Db::new_db_with_config(DbConfig {
id_type: IdType::None,
id_key: "id".into(),
});
create_people(&db);
create_products(&db);
create_orders(&db);
create_order_items(&db);
db
}
Licensed under the MIT License. See LICENSE for details.