rango-orm-core 0.2.0

Database-free core of Rango ORM: model traits, schema and field types
Documentation
use crate::constraints::{Constraint, OrderBy};

/// Represents the schema of a single table, as declared by a Model.
#[derive(Debug, Clone)]
pub struct TableSchema {
    pub table_name: String,
    pub columns: Vec<ColumnDef>,
    pub constraints: Vec<Constraint>,
    pub ordering: Vec<OrderBy>,
    pub managed: bool,
    pub comment: Option<String>,
}

impl Default for TableSchema {
    fn default() -> Self {
        Self {
            table_name: String::new(),
            columns: Vec::new(),
            constraints: Vec::new(),
            ordering: Vec::new(),
            managed: true,
            comment: None,
        }
    }
}

/// Definition of a single column.
#[derive(Debug, Clone)]
pub struct ColumnDef {
    pub name: String,
    pub col_type: ColumnType,
    pub nullable: bool,
    pub primary_key: bool,
    pub unique: bool,
    pub default: Option<DefaultValue>,
    pub references: Option<FkReference>,
    /// Create a b-tree index on this column.
    pub index: bool,
}

/// Common column types, backend-agnostic.
/// Backends map these to their native SQL types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ColumnType {
    Bool,
    SmallInt,
    Int,
    BigInt,
    /// `INTEGER` primary key generated by the database (identity / auto-increment).
    AutoInt,
    /// `BIGINT` primary key generated by the database (identity / auto-increment).
    AutoBigInt,
    Float,
    Double,
    Decimal {
        precision: u8,
        scale: u8,
    },
    Text,
    Varchar(u32),
    Bytea,
    Uuid,
    Date,
    Time,
    DateTime,
    Json,
    Jsonb, // Postgres-specific — backends that don't support it fall back to Json
}

#[derive(Debug, Clone)]
pub enum DefaultValue {
    Literal(String),
    CurrentTimestamp,
    GeneratedUuid,
}

/// Foreign key reference.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FkReference {
    pub table: String,
    pub column: String,
    pub on_delete: ReferentialAction,
    pub on_update: ReferentialAction,
}

/// Standard SQL referential actions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReferentialAction {
    Cascade,
    SetNull,
    SetDefault,
    Restrict,
    NoAction,
}