#sea-orm #generator #macro #macro-for-generating #id #typed #postgresql #utoipa

sea-orm-typed-id

A Rust library that provides a macro for generating type-safe database ID types for SeaORM

3 releases (breaking)

0.3.0 May 12, 2025
0.2.0 Nov 10, 2024
0.1.0 Nov 10, 2024

#867 in Database interfaces

Download history 8/week @ 2025-02-18 5/week @ 2025-02-25 3/week @ 2025-03-04 9/week @ 2025-03-11 1/week @ 2025-03-18 7/week @ 2025-04-01 1/week @ 2025-04-08 79/week @ 2025-05-06 77/week @ 2025-05-13 2/week @ 2025-05-20 17/week @ 2025-05-27

175 downloads per month

MIT license

20KB
154 lines

sea-orm-typed-id

Crates.io License: MIT

A Rust library that provides a macro for generating type-safe database ID types for SeaORM.

Caveat

You might not actually need this library; it’s just a macro, and you might be better off simply copying the code from src/lib.rs into your project.

Features

  • all: Enables all features
  • rustls: Enables rustls TLS backend for SeaORM
  • postgres: Enables PostgreSQL array support
  • utoipa: Enables OpenAPI schema generation support

Installation

Add this to your Cargo.toml:

[dependencies]
sea-orm-typed-id = { version = "0.2.0", features = ["all"] }

Usage

use sea_orm_typed_id::define_id;

define_id!(CakeId);

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "cakes")]
pub struct Cake {
    pub id: CakeId,
}

define_id!(FillingId);

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "fillings")]
pub struct Filling {
    pub id: FillingId,
}

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "cake_fillings")]
pub struct CakeFilling {
    pub cake_id: CakeId,
    pub filling_id: FillingId,
}

Known Shortcomings

Typed IDs won't work with postgres arrays.

// ...
pub struct Model {
    // ...
    pub filling_ids: Vec<FillingId>, // Doesn't work
}

This won't compile because Vec<FillingId> doesn't implement sea_orm::TryGetable which we also can't add ourselves as both Vec and sea_orm::TryGetable are external.

One of possible workouts is to make fields private and add getter for it.

// ...
pub struct Model {
    // ...
    filling_ids: Vec<i32>,
}

impl Model {
    pub fn filling_ids(&self) -> Vec<FillingId> {
        self.filling_ids.iter().map(FillingId::from).collect()
    }
}

License

This project is licensed under the MIT License. See the LICENSE file for details.

Dependencies

~54MB
~1M SLoC