axion-db

Crates.ioaxion-db
lib.rsaxion-db
version0.0.4
created_at2025-06-01 05:07:31.095965+00
updated_at2025-06-18 06:04:32.756923+00
descriptionDatabase functionality for the axion framework
homepage
repositoryhttps://github.com/Yrrrrrf/axion
max_upload_size
id1696894
size126,836
Fernando Bryan Reza Campos (Yrrrrrf)

documentation

README

axion-db

Core database interaction layer for the Axion framework.

GitHub: Axion crates.io docs.rs

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::AnyPool for 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 sqlx and tokio for 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 DbConfig for 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.

Commit count: 15

cargo fmt