4 releases
Uses new Rust 2024
| 0.0.4 | Jun 18, 2025 |
|---|---|
| 0.0.3 | Jun 17, 2025 |
| 0.0.2 | Jun 1, 2025 |
| 0.0.1 | Jun 1, 2025 |
#153 in #sqlx
172 downloads per month
Used in axion
60KB
1K
SLoC
axion-db
Core database interaction layer for the Axion framework.
Note: This crate is part of the Axion framework and is not typically intended for direct standalone use. It provides the foundational database connectivity, schema introspection, and type mapping capabilities that power Axion.
Overview
axion-db is responsible for:
- Database Configuration: Defining and managing connection parameters for various SQL databases.
- Connection Pooling: Utilizing
sqlx::AnyPoolfor efficient, asynchronous database connections. - Schema Introspection: Querying database metadata (schemas, tables, columns, views, functions, etc.) to understand the database structure.
- Type Mapping: Providing a basic mapping between SQL data types and corresponding Rust types for code generation and dynamic query handling within the Axion framework.
- Raw Query Execution: Offering a simple interface to execute raw SQL queries, primarily used by higher-level Axion components.
Features
- Async Native: Built on
sqlxandtokiofor fully asynchronous database operations. - Multi-Database Support (via
sqlx::Any):- PostgreSQL
- MySQL/MariaDB
- SQLite
- Connection Pooling: Leverages
sqlx's robust connection pooling. - Detailed Schema Introspection: Gathers information about tables, columns (types, nullability, PKs, FKs), views, functions, procedures, and enums.
- Configurable: Flexible
DbConfigfor various connection setups.
Usage
This crate is primarily consumed by other Axion components (axion-server and axion). Direct usage would typically involve setting up a DbConfig and then creating a DbClient to perform introspection or raw queries.
use axion_db::prelude::*;
use std::sync::Arc;
# async fn run_example() -> axion_db::error::DbResult<()> {
// Ensure drivers are installed for sqlx::Any (typically done once in main application)
sqlx::any::install_default_drivers();
let db_config = DbConfig::new(DatabaseType::Postgres) // Or Mysql, Sqlite
.host("localhost")
.port(5432)
.username("your_user")
.password("your_password")
.database_name("your_database")
.pool_options(PoolOptionsConfig {
max_connections: Some(10),
..Default::default()
});
let client = Arc::new(DbClient::new(db_config).await?);
client.test_connection().await?;
let version = client.get_db_version().await?;
println!("Connected to database version: {}", version.lines().next().unwrap_or_default());
// Example: List schemas
let schemas = client.list_all_schemas(false).await?; // false = exclude system schemas
println!("Available user schemas: {:?}", schemas);
if let Some(first_schema) = schemas.first() {
// Example: List tables in the first user schema
let tables = client.list_tables_in_schema(first_schema).await?;
println!("Tables in schema '{}': {:?}", first_schema, tables);
if let Some(first_table) = tables.first() {
// Example: Get metadata for the first table in that schema
let table_meta = client.get_table_metadata(first_schema, first_table).await?;
println!("Metadata for table '{}.{}': {:#?}", first_schema, first_table, table_meta);
}
}
# Ok(())
# }
For more comprehensive examples of how axion-db is used to power automatic API generation, please see the main Axion repository examples.
Dependencies
~48–65MB
~1M SLoC