What's new in SeaORM 0.6.0
Tags: rust, githunt, news, opensource
Published: 2022-02-20 02:29:19

🎉 We are pleased to release SeaORM [`0.6.0`](https://github.com/SeaQL/sea-orm/releases/tag/0.6.0) today! Here are some feature highlights 🌟:

## Migration

[[#335](https://github.com/SeaQL/sea-orm/pull/335)] Version control you database schema with migrations written in SeaQuery or in raw SQL. View [migration docs](/SeaORM/docs/migration/setting-up-migration) to learn more.

1. Setup the migration directory by executing `sea-orm-cli migrate init`.
    ```
    migration
    ├── Cargo.toml
    ├── README.md
    └── src
        ├── lib.rs
        ├── m20220101_000001_create_table.rs
        └── main.rs
    ```
2. Defines the migration in SeaQuery.
    ```rust
    use sea_schema::migration::prelude::*;

    pub struct Migration;

    impl MigrationName for Migration {
        fn name(&self) -> &str {
            "m20220101_000001_create_table"
        }
    }

    #[async_trait::async_trait]
    impl MigrationTrait for Migration {
        async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
            manager
                .create_table( ... )
                .await
        }

        async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
            manager
                .drop_table( ... )
                .await
        }
    }
    ```
3. Apply the migration by executing `sea-orm-cli migrate`.
    ```shell
    $ sea-orm-cli migrate
    Applying all pending migrations
    Applying migration 'm20220101_000001_create_table'
    Migration 'm20220101_000001_create_table' has been applied
    ```

Designed by:
- [Chris Tsang](https://github.com/tyt2y3)

Contributed by:
- [Billy Chan](https://github.com/billy1624)

## Support DateTimeUtc & DateTimeLocal in Model

[[#489](https://github.com/SeaQL/sea-orm/pull/489)] Represents database's timestamp column in Model with attribute of type `DateTimeLocal` (`chrono::DateTime<Local>`) or `DateTimeUtc` (`chrono::DateTime<Utc>`).

```rust
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "satellite")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub satellite_name: String,
    pub launch_date: DateTimeUtc,
    pub deployment_date: DateTimeLocal,
}
```

Proposed by:
- [lz1998](https://github.com/lz1998)
- [Chris Tsang](https://github.com/tyt2y3)

Contributed by:
- [Charles·Chege](https://github.com/charleschege)
- [Billy Chan](https://github.com/billy1624)

## Mock Join Results

[[#455](https://github.com/SeaQL/sea-orm/pull/455)] Constructs mock results of related model with tuple of model.

```rust
let db = MockDatabase::new(DbBackend::Postgres)
    // Mocking result of cake with its related fruit
    .append_query_results(vec![vec![(
        cake::Model {
            id: 1,
            name: "Apple Cake".to_owned(),
        },
        fruit::Model {
            id: 2,
            name: "Apple".to_owned(),
            cake_id: Some(1),
        },
    )]])
    .into_connection();

assert_eq!(
    cake::Entity::find()
        .find_also_related(fruit::Entity)
        .all(&db)
        .await?,
    vec![(
        cake::Model {
            id: 1,
            name: "Apple Cake".to_owned(),
        },
        Some(fruit::Model {
            id: 2,
            name: "Apple".to_owned(),
            cake_id: Some(1),
        })
    )]
);
```

Proposed by:
- [Bastian](https://github.com/cemoktra)

Contributed by:
- [Bastian](https://github.com/cemoktra)
- [Billy Chan](https://github.com/billy1624)

## Support Max Connection Lifetime Option

[[#493](https://github.com/SeaQL/sea-orm/pull/493)] You can set the maximum lifetime of individual connection with the `max_lifetime` method.

```rust
let mut opt = ConnectOptions::new("protocol://username:password@host/database".to_owned());
opt.max_lifetime(Duration::from_secs(8))
    .max_connections(100)
    .min_connections(5)
    .connect_timeout(Duration::from_secs(8))
    .idle_timeout(Duration::from_secs(8))
    .sqlx_logging(true);

let db = Database::connect(opt).await?;
```

Proposed by:
- [Émile Fugulin](https://github.com/Sytten)

Contributed by:
- [Billy Chan](https://github.com/billy1624)


## SeaORM CLI & Codegen Updates

- [[#433](https://github.com/SeaQL/sea-orm/pull/433)] Generates the `column_name` macro attribute for column which is not named in snake case
- [[#335](https://github.com/SeaQL/sea-orm/pull/335)] Introduces migration subcommands `sea-orm-cli migrate`

Proposed by:
- [Gabriel Paulucci](https://github.co)

Contributed by:
- [Billy Chan](https://github.com/billy1624)

## Sponsor

Our [GitHub Sponsor](https://github.com/sponsors/SeaQL) profile is up! If you feel generous, a small donation will be greatly appreciated.

A big shout out to our sponsors 😇:

- [Émile Fugulin](https://github.com/Sytten)
- [Zachary Vander Velden](https://github.com/exzachlyvv)
- [Shane Sveller](https://github.com/shanesveller)
- [Sakti Dwi Cahyono](https://github.com/sakti)
- Unnamed Sponsor

## Community

SeaQL is a community driven project. We welcome you to participate, contribute and together build for Rust's future.

Here is the roadmap for SeaORM [`0.7.x`](https://github.com/SeaQL/sea-orm/milestone/7).