sea-orm-verify

Crates.iosea-orm-verify
lib.rssea-orm-verify
version0.1.0
sourcesrc
created_at2023-04-05 11:44:29.885227
updated_at2023-04-05 11:44:29.885227
descriptionverify sea-orm entities with sqlx compile time macros
homepagehttps://gitlab.com/dragonn/sea-orm-verify
repositoryhttps://gitlab.com/dragonn/sea-orm-verify
max_upload_size
id830981
size10,535
Mateusz (dragonnn)

documentation

README

sea-orm-verify

Provides Verify derive macro.

#[derive(DeriveEntityModel, Verify)]
#[derive(Debug, Clone, PartialEq)]
#[sea_orm(table_name = "task")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: u32,
    pub finish_at: Option<DateTime>,
}

generates

impl Model {
    async fn _verify() {
        sqlx::query_as!(Self, "SELECT id, finish_at FROM task");
    }
}

this will cause sqlx query_as macro to verify the struct fields with the database at compile time needs to have setup DATABASE_URL for example using .env or sqlx offline data. Please refer to docs.rs/sqlx

it also needs sqlx as a dependency in your project, for example for Postgres in Cargo.toml it needs:

sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "postgres"] }

Please be aware that sqlx and sea-orm column mapping might be not 100% compatible and this check might fail to catch some column type mismatch you still have

Supported struct attributes inside #[sea_orm():

  • table_name - gets the database table name
  • schema_name - gets the database schema name (for Postgres)

Supported field attributes inside #[verify()]:

  • type_override - override sqlx type like described in Force a Different/Custom Type, useful for custom enums. Enum need to annotated with #[derive(sqlx::Type) and #[sqlx(type_name = "integer")]
  • not_null - forces the column to be NOT NULL, useful for example for db views where sqlx get the nullable wrong.
  • null - same as not_null but forces the column to be NULL
Commit count: 1

cargo fmt