ignis-db 0.2.0

A comprehensive PostgreSQL ORM with macro-driven schema management
Documentation
//! Example demonstrating model derive macros and CRUD operations
//!
//! This example shows how to use the #[derive(IgnisModel, Insertable, Updatable, Deletable)]
//! macros to create database models with automatic CRUD operations.

use anyhow::Result;
use ignis_db::{
    impl_deletable, impl_ignis_model, impl_insertable, impl_updatable, BindValue, IgnisModel,
    IgnisObject,
};

// Define a table using the table! macro
ignis_db::table! {
    users (id) {
        id -> Int4,
        email -> Varchar,
        name -> Text,
        active -> Bool,
        created_at -> Timestamptz,
    }
}

// Define a model struct with derive macros
#[derive(Debug, Clone)]
pub struct User {
    pub id: i32,
    pub email: String,
    pub name: String,
    pub active: bool,
    pub created_at: chrono::DateTime<chrono::Utc>,
}

impl_ignis_model!(User, "users", "id");
impl_insertable!(User);
impl_updatable!(User);
impl_deletable!(User);

// Implement the get_primary_key_value method for User
impl User {
    fn get_primary_key_value(&self) -> Result<BindValue> {
        Ok(BindValue::Int4(self.id))
    }
}

// Define another table and model
ignis_db::table! {
    posts (id) {
        id -> Int4,
        user_id -> Int4,
        title -> Text,
        content -> Text,
        published -> Bool,
        created_at -> Timestamptz,
    }
}

#[derive(Debug, Clone)]
pub struct Post {
    pub id: i32,
    pub user_id: i32,
    pub title: String,
    pub content: String,
    pub published: bool,
    pub created_at: chrono::DateTime<chrono::Utc>,
}

impl_ignis_model!(Post, "posts", "id");
impl_insertable!(Post);
impl_updatable!(Post);
impl_deletable!(Post);

impl Post {
    fn get_primary_key_value(&self) -> Result<BindValue> {
        Ok(BindValue::Int4(self.id))
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    println!("🚀 Ignis-DB Model CRUD Example");
    println!("================================\n");

    // Create some sample data
    let now = chrono::Utc::now();

    let user = User {
        id: 1,
        email: "alice@example.com".to_string(),
        name: "Alice Johnson".to_string(),
        active: true,
        created_at: now,
    };

    let post = Post {
        id: 1,
        user_id: 1,
        title: "My First Post".to_string(),
        content: "This is the content of my first post.".to_string(),
        published: true,
        created_at: now,
    };

    // Demonstrate the generated constants
    println!("📋 Model Information:");
    println!("User table: {}", User::TABLE);
    println!("User primary key: {}", User::PK);
    println!("User full name: {}", User::FULL_TABLE_NAME);
    println!("User schema: {:?}", User::SCHEMA);
    println!();

    println!("Post table: {}", Post::TABLE);
    println!("Post primary key: {}", Post::PK);
    println!("Post full name: {}", Post::FULL_TABLE_NAME);
    println!("Post schema: {:?}", Post::SCHEMA);
    println!();

    // Demonstrate the generated methods
    println!("🔧 Generated Methods:");
    println!("- User::insert() - Available");
    println!("- User::update() - Available");
    println!("- User::delete() - Available");
    println!();

    // Show the table definitions
    println!("🗃️ Table Definitions:");
    let users_table = users::table();
    println!("Users table DDL:");
    println!("{}", users_table.generate_ddl());
    println!();

    let posts_table = posts::table();
    println!("Posts table DDL:");
    println!("{}", posts_table.generate_ddl());
    println!();

    // Demonstrate type-safe column access
    println!("🎯 Type-Safe Column Access:");
    let user_id_col = users::Columns::id();
    let user_email_col = users::Columns::email();
    let user_active_col = users::Columns::active();

    println!("User ID column: {:?}", user_id_col);
    println!("User email column: {:?}", user_email_col);
    println!("User active column: {:?}", user_active_col);
    println!();

    // Show inventory registration
    println!("📚 Inventory Registration:");
    let tables: Vec<_> = ignis_db::get_registered_tables().collect();
    println!("Registered tables: {}", tables.len());
    for table in &tables {
        println!("  - {} ({})", table.0.name, table.0.object_type());
    }
    println!();

    println!("✅ Model CRUD example completed successfully!");
    println!();
    println!("💡 What this demonstrates:");
    println!("  1. Automatic trait implementation via derive macros");
    println!("  2. Type-safe CRUD operations (insert, update, delete)");
    println!("  3. Compile-time table and column validation");
    println!("  4. Integration with the existing table! macro system");
    println!("  5. Inventory registration for schema management");

    Ok(())
}