Core database abstractions and SQLite implementation for BurnCloud AI management system.
- SQLite database support using sqlx
- Async/await API
- Connection pooling
- Error handling with detailed error types
- Both file-based and in-memory database support
Add this to your Cargo.toml
:
[dependencies]
burncloud-database = "0.1.0"
use burncloud_database::{create_database, Result};
#[tokio::main]
async fn main() -> Result<()> {
// Create a database
let mut db = create_database("./my_database.db").await?;
// Create a table
db.execute_query(
"CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
)"
).await?;
// Insert data
db.execute_query(
"INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')"
).await?;
// Query data
let users: Vec<(i64, String, String)> = db.fetch_all(
"SELECT id, name, email FROM users"
).await?;
for (id, name, email) in users {
println!("User {}: {} ({})", id, name, email);
}
// Close the database
db.close().await?;
Ok(())
}
use burncloud_database::{create_in_memory_database, Result};
#[tokio::main]
async fn main() -> Result<()> {
let mut db = create_in_memory_database().await?;
// Use the database same as file-based database
// The database will be destroyed when the connection is closed
db.close().await?;
Ok(())
}
use burncloud_database::{Database, Result};
#[tokio::main]
async fn main() -> Result<()> {
let mut db = Database::new("./my_database.db");
db.initialize().await?;
// Use the database...
db.close().await?;
Ok(())
}
The main database struct that provides all database operations.
new(path)
- Create a new database instance with file pathnew_in_memory()
- Create a new in-memory database instanceinitialize()
- Initialize the database connectionconnection()
- Get the database connectionexecute_query(query)
- Execute a SQL queryfetch_one<T>(query)
- Fetch a single rowfetch_all<T>(query)
- Fetch all rowsfetch_optional<T>(query)
- Fetch optional rowclose()
- Close the database connection
create_database(path)
- Create and initialize a file-based databasecreate_in_memory_database()
- Create and initialize an in-memory database
The library provides comprehensive error handling through the DatabaseError
enum:
Connection
- Database connection errorsMigration
- Database migration errorsQuery
- SQL query errorsSerialization
- JSON serialization errorsNotInitialized
- Database not initializedIo
- IO errors
Run the basic usage example:
cargo run --example basic_usage
This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.