Crate libsql_orm_macros

Source
Expand description

Procedural macros for libsql-orm

This crate provides derive macros and attribute macros for the libsql-orm library, enabling automatic implementation of ORM traits and convenient model definitions.

§Derive Macros

§#[derive(Model)]

Automatically implements the Model trait for a struct, providing all CRUD operations and ORM functionality.

use libsql_orm::Model;
use serde::{Serialize, Deserialize};

#[derive(Model, Serialize, Deserialize)]
struct User {
    pub id: Option<i64>,
    pub name: String,
    pub email: String,
}

§Attribute Macros

§#[table_name("custom_name")]

Specifies a custom table name for the model. By default, the table name is derived from the struct name converted to lowercase.

use libsql_orm::Model;
use serde::{Serialize, Deserialize};

#[derive(Model, Serialize, Deserialize)]
#[table_name("custom_users")]
struct User {
    pub id: Option<i64>,
    pub name: String,
}

§#[orm_column(...)]

Specifies custom column properties for database fields.

use libsql_orm::Model;
use serde::{Serialize, Deserialize};

#[derive(Model, Serialize, Deserialize)]
struct User {
    #[orm_column(type = "INTEGER PRIMARY KEY AUTOINCREMENT")]
    pub id: Option<i64>,
     
    #[orm_column(not_null, unique)]
    pub email: String,
     
    #[orm_column(type = "TEXT DEFAULT 'active'")]
    pub status: String,
}

§Function-like Macros

§generate_migration!(Model)

Generates a database migration from a model definition.

use libsql_orm::{generate_migration, MigrationManager};

let migration = generate_migration!(User);
let manager = MigrationManager::new(db);
manager.execute_migration(&migration).await?;

Macros§

generate_migration
Macro to generate migration from a model

Attribute Macros§

orm_column
Column attribute macro for defining SQL column properties

Derive Macros§

Model
Derive macro for the Model trait