Module model

Module model 

Source
Expand description

Core Model trait and column metadata structures.

Defines the Model trait that must be implemented by all ORM entities, and the ColumnInfo struct containing metadata about table columns.

§Model Module

This module defines the core Model trait and associated structures for Bottle ORM. It provides the interface that all database entities must implement, along with metadata structures for describing table columns.

§Overview

The Model trait is the foundation of Bottle ORM. It defines how Rust structs map to database tables, including:

  • Table name resolution
  • Column metadata (types, constraints, relationships)
  • Serialization to/from database format

§Automatic Implementation

The Model trait is typically implemented automatically via the #[derive(Model)] procedural macro, which analyzes struct fields and #[orm(...)] attributes to generate the necessary implementation.

§Example Usage

use bottle_orm::Model;
use uuid::Uuid;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;

#[derive(Model, Debug, Clone, Serialize, Deserialize, FromRow)]
struct User {
    #[orm(primary_key)]
    id: Uuid,

    #[orm(size = 50, unique, index)]
    username: String,

    #[orm(size = 100)]
    email: String,

    age: Option<i32>,

    #[orm(create_time)]
    created_at: DateTime<Utc>,
}

#[derive(Model, Debug, Clone, Serialize, Deserialize, FromRow)]
struct Post {
    #[orm(primary_key)]
    id: Uuid,

    #[orm(foreign_key = "User::id")]
    user_id: Uuid,

    #[orm(size = 200)]
    title: String,

    content: String,

    #[orm(create_time)]
    created_at: DateTime<Utc>,
}

§Supported ORM Attributes

  • #[orm(primary_key)] - Marks field as primary key
  • #[orm(unique)] - Adds UNIQUE constraint
  • #[orm(index)] - Creates database index
  • #[orm(size = N)] - Sets VARCHAR size (for String fields)
  • #[orm(create_time)] - Auto-populate with current timestamp on creation
  • #[orm(update_time)] - Auto-update timestamp on modification (future feature)
  • #[orm(foreign_key = "Table::Column")] - Defines foreign key relationship

Structs§

ColumnInfo
Metadata information about a database column.

Traits§

Model
The core trait defining a Database Model (Table) in Bottle ORM.