FOSK
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.
โจ Features
- In-memory database with collections (tables)
- Configurable ID strategies: integer, UUID, or none
- Simple JSON storage (serde_json::Value)
- SQL parser with support for:
- SELECT, WHERE, GROUP BY, HAVING
- JOIN (inner, left, right, full)
- ORDER BY, LIMIT, OFFSET
- Parameterized queries (? placeholders, including arrays)
- Test-friendly: create databases on the fly and seed them
Installation
In your Cargo.toml:
[]
= "0.1.2"
= "1"
Quick example
use ;
use json;
๐ Main Types & API
Db
Represents a database.
- new_db_with_config(config: DbConfig) -> Db
- create_collection(name: &str) -> CollectionHandle
- query(sql: &str) -> Result<Vec, AnalyzerError>
- query_with_args(sql: &str, args: Value) -> Result<Vec, AnalyzerError>
- drop_collection(col_name: &str) -> bool
- clear()
- get_config() -> DbConfig
Config
Defines collection behavior.
- DbConfig::int("id") โ auto-increment integer IDs
- DbConfig::uuid("id") โ UUID v4 IDs
- DbConfig::none("id") โ no automatic ID field
CollectionHandle
- add(item: Value) -> Option
- get_all() -> Vec
- get(id: &str) -> Option
- update(id: &str, item: Value) -> Option
- delete(id: &str) -> Option
extras
- get_paginated(offset: usize, limit: usize) -> Vec
- exists(id: &str) -> bool
- count() -> usize
- add_batch(items: Value) -> Vec
- update_partial(id: &str, partial_item: Value) -> Option
- clear() -> usize
- get_config() -> DbConfig
Load from existing data
fn Db::load_from_file(path: &std::path::Path) -> Result<String, String>;
let db = new;
// Load all collections from a file
db.load_from_file;
fn Db::load_from_json(json_value: &serde_json::Value) -> Result<usize, String>
let db = new;
// Load all collections from JSON
db.load_from_json;
fn DbCollection::load_from_file(path: &std::path::Path) -> Result<String, String>;
let db = new;
let people = db.create_collection;
// Load collection from a file
people.load_from_file;
fn DbCollection::load_from_json(json_value: &serde_json::Value) -> Result<String, String>
let db = new;
let people = db.create_collection;
// Load collection from JSON
people.load_from_json;
Save data
fn Db::fn write_to_json(&self) -> Value
let db = new;
...
...
...
let json_value = db.write_to_json;
fn DbCollection::write_to_file(&self, file_path: &OsString) -> Result<(), String>
let db = new;
...
...
...
db.write_to_file;
fn DbCollection::write_to_file(path: &std::path::Path) -> Result<(), String>
let db = new;
let people = db.create_collection;
...
...
people.write_to_file;
๐งช Testing & Seeding
Example test seed (see fixtures::seed_db):
โ ๏ธ Notes
- Projections normally output unqualified field names (id, name), unless duplicates exist. In case of conflicts, names are disambiguated with their collection prefix (id, o.id).
๐ License
Licensed under the MIT License. See LICENSE for details.