2 releases
new 0.1.1 | Mar 17, 2025 |
---|---|
0.1.0 | Mar 17, 2025 |
#782 in Database interfaces
170 downloads per month
7KB
120 lines
LiteQL
LiteQL is a lightweight wrapper for Rusqlite and Eloquent, designed to make working with SQLite in Rust easier and more intuitive.
Usage
Here's a simple example demonstrating its usage.
use std::sync::LazyLock;
use eloquent::Eloquent;
use rusqlite::{Connection, Error, Row};
use liteql::{Execute, FromRow, Query};
#[derive(Debug)]
pub struct User {
pub id: String,
pub name: String,
pub age: u8,
}
impl FromRow for User {
fn from_row(row: &Row) -> Result<Self, Error>
where
Self: Sized,
{
Ok(Self {
id: row.get(0)?,
name: row.get(1)?,
age: row.get(2)?,
})
}
}
const DATABASE: LazyLock<Connection> =
LazyLock::new(|| Connection::open("database.sqlite").unwrap());
fn main() {
"DROP TABLE IF EXISTS user".execute(&DATABASE).unwrap();
"
CREATE TABLE user (
id VARCHAR(20) PRIMARY KEY,
name VARCHAR(40) NOT NULL,
age INTEGER NOT NULL
)"
.execute(&DATABASE)
.unwrap();
let george = User {
id: "georgiyozhegov".into(),
name: "George".into(),
age: 99,
};
Eloquent::query()
.table("user")
.insert("id", george.id)
.insert("name", george.name)
.insert("age", george.age as u32)
.execute(&DATABASE)
.unwrap();
let other = User {
id: "otheruser".into(),
name: "User".into(),
age: 21,
};
Eloquent::query()
.table("user")
.insert("id", other.id)
.insert("name", other.name)
.insert("age", other.age as u32)
.execute(&DATABASE)
.unwrap();
let users: Vec<User> = Eloquent::query()
.table("user")
.select("*")
.where_gt("age", 30)
.query(&DATABASE)
.unwrap();
for user in users {
dbg!(user);
}
}
Dependencies
~23MB
~456K SLoC