Skip to content

Commit

Permalink
[SeaORM] Docs 1.0-rc (#120)
Browse files Browse the repository at this point in the history
* Refreshed migration schema definition (SeaQL/sea-orm#2099)

* Reworked SQLite Type Mappings (SeaQL/sea-orm#2077)

* `DerivePartialModel` macro attribute `entity` supports `syn::Type` as well (SeaQL/sea-orm#2137)

* Added `RelationDef::from_alias()` (SeaQL/sea-orm#2146)
  • Loading branch information
billy1624 authored Apr 11, 2024
1 parent 858ab8d commit e24647b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 33 deletions.
40 changes: 25 additions & 15 deletions SeaORM/docs/03-migration/02-writing-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ Here are some common DDL snippets you may find useful.
Story,
}

// Remember to import `sea_orm_migration::schema::*` schema helpers into scope
use sea_orm_migration::{prelude::*, schema::*};

// Defining the schema with helpers
manager
.create_table(
Table::create()
.table(Post::Table)
.if_not_exists()
.col(pk_auto(Post::Id))
.col(string(Post::Title))
.col(string(Post::Text))
.col(enumeration_null(Post::Category, Alias::new("category"), Category::iter()))
)
.await

// Or, you can define the schema without the helpers
manager
.create_table(
Table::create()
Expand All @@ -136,7 +153,6 @@ Here are some common DDL snippets you may find useful.
ColumnDef::new(Post::Category)
.enumeration(Alias::new("category"), Category::iter()),
)
.to_owned(),
)
.await
```
Expand Down Expand Up @@ -167,7 +183,6 @@ Here are some common DDL snippets you may find useful.
Type::create()
.as_enum(CategoryEnum)
.values(CategoryVariants::iter())
.to_owned(),
)
.await?;
```
Expand Down Expand Up @@ -275,23 +290,19 @@ impl MigrationTrait for Migration {
You can combine multiple changes within both up and down migration functions. Here is a complete example:

```rust
// Remember to import `sea_orm_migration::schema::*` schema helpers into scope
use sea_orm_migration::{prelude::*, schema::*};

async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {

manager
.create_table(
sea_query::Table::create()
.table(Post::Table)
.if_not_exists()
.col(
ColumnDef::new(Post::Id)
.integer()
.not_null()
.auto_increment()
.primary_key()
)
.col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Post::Text).string().not_null())
.to_owned()
.col(pk_auto(Post::Id))
.col(string(Post::Title))
.col(string(Post::Text))
)
.await?;

Expand All @@ -302,7 +313,6 @@ async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
.name("idx-post_title")
.table(Post::Table)
.col(Post::Title)
.to_owned(),
)
.await?;

Expand All @@ -315,10 +325,10 @@ and here we have the matching down function:
```rust
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {

manager.drop_index(Index::drop().name("idx-post-title").to_owned())
manager.drop_index(Index::drop().name("idx-post-title"))
.await?;

manager.drop_table(Table::drop().table(Post::Table).to_owned())
manager.drop_table(Table::drop().table(Post::Table))
.await?;

Ok(()) // All good!
Expand Down
36 changes: 18 additions & 18 deletions SeaORM/docs/04-generate-entity/02-entity-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,32 @@ For the mappings of Rust primitive data types.

| Rust type | Database Type <br/> ([`ColumnType`](https://docs.rs/sea-orm/*/sea_orm/entity/enum.ColumnType.html)) | SQLite <br/> datatype | MySQL <br/> datatype | PostgreSQL <br/> datatype |
| --------- | --------- | --------- | --------- | --------- |
| `String` | Char | text | char | char |
| `String` | String | text | varchar | varchar |
| `i8` | TinyInteger | integer | tinyint | char |
| `u8` | TinyUnsigned | integer | tinyint unsigned | N/A |
| `i16` | SmallInteger | integer | smallint | smallint |
| `u16` | SmallUnsigned | integer | smallint unsigned | N/A |
| `String` | Char | char | char | char |
| `String` | String | varchar | varchar | varchar |
| `i8` | TinyInteger | tinyint | tinyint | char |
| `u8` | TinyUnsigned | tinyint | tinyint unsigned | N/A |
| `i16` | SmallInteger | smallint | smallint | smallint |
| `u16` | SmallUnsigned | smallint | smallint unsigned | N/A |
| `i32` | Integer | integer | int | integer |
| `u32` | Unsigned | integer | int unsigned | N/A |
| `i64` | BigInteger | integer | bigint | bigint |
| `u64` | BigUnsigned | N/A | bigint unsigned | N/A |
| `f32` | Float | real | float | real |
| `f64` | Double | real | double | double precision |
| `bool` | Boolean | integer | bool | bool |
| `i64` | BigInteger | bigint | bigint | bigint |
| `u64` | BigUnsigned | bigint | bigint unsigned | N/A |
| `f32` | Float | float | float | real |
| `f64` | Double | double | double | double precision |
| `bool` | Boolean | boolean | bool | bool |
| `Vec<u8>` | Binary | blob | blob | bytea |

For the mappings of Rust non-primitive data types. You can check [`entity/prelude.rs`](https://github.com/SeaQL/sea-orm/blob/master/src/entity/prelude.rs) for all of the reexported types.

| Rust type | Database Type <br/> ([`ColumnType`](https://docs.rs/sea-orm/*/sea_orm/entity/enum.ColumnType.html)) | SQLite <br/> datatype | MySQL <br/> datatype | PostgreSQL <br/> datatype |
| --------- | --------- | --------- | --------- | --------- |
| `Date`: chrono::NaiveDate <br/>`TimeDate`: time::Date | Date | text | date | date |
| `Time`: chrono::NaiveTime <br/>`TimeTime`: time::Time | Time | text | time | time |
| `DateTime`: chrono::NaiveDateTime <br/>`TimeDateTime`: time::PrimitiveDateTime | DateTime | text | datetime | timestamp |
| `DateTimeLocal`: chrono::DateTime&lt;Local&gt; <br/>`DateTimeUtc`: chrono::DateTime&lt;Utc&gt; | Timestamp | text | timestamp | N/A |
| `DateTimeWithTimeZone`: chrono::DateTime&lt;FixedOffset&gt; <br/>`TimeDateTimeWithTimeZone`: time::OffsetDateTime | TimestampWithTimeZone | text | timestamp | timestamp with time zone |
| `Uuid`: uuid::Uuid, uuid::fmt::Braced, uuid::fmt::Hyphenated, uuid::fmt::Simple, uuid::fmt::Urn | Uuid | text | binary(16) | uuid |
| `Json`: serde_json::Value | Json | text | json | json |
| `Date`: chrono::NaiveDate <br/>`TimeDate`: time::Date | Date | date_text | date | date |
| `Time`: chrono::NaiveTime <br/>`TimeTime`: time::Time | Time | time_text | time | time |
| `DateTime`: chrono::NaiveDateTime <br/>`TimeDateTime`: time::PrimitiveDateTime | DateTime | datetime_text | datetime | timestamp |
| `DateTimeLocal`: chrono::DateTime&lt;Local&gt; <br/>`DateTimeUtc`: chrono::DateTime&lt;Utc&gt; | Timestamp | timestamp_text | timestamp | N/A |
| `DateTimeWithTimeZone`: chrono::DateTime&lt;FixedOffset&gt; <br/>`TimeDateTimeWithTimeZone`: time::OffsetDateTime | TimestampWithTimeZone | timestamp_with_timezone_text | timestamp | timestamp with time zone |
| `Uuid`: uuid::Uuid, uuid::fmt::Braced, uuid::fmt::Hyphenated, uuid::fmt::Simple, uuid::fmt::Urn | Uuid | uuid_text | binary(16) | uuid |
| `Json`: serde_json::Value | Json | json_text | json | json |
| `Decimal`: rust_decimal::Decimal | Decimal | real | decimal | decimal |

You can override the default mappings between a Rust type and `ColumnType` by the `column_type` attribute.
Expand Down
25 changes: 25 additions & 0 deletions SeaORM/docs/06-relation/06-custom-join-condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,29 @@ assert_eq!(
]
.join(" ")
);
```

Specify table alias in the join statement:

```rust
assert_eq!(
cake::Entity::find()
.join_as(
JoinType::LeftJoin,
cake_filling::Relation::Cake.def().rev(),
cf.clone()
)
.join(
JoinType::LeftJoin,
cake_filling::Relation::Filling.def().from_alias(cf)
)
.build(DbBackend::MySql)
.to_string(),
[
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`",
"LEFT JOIN `cake_filling` AS `cf` ON `cake`.`id` = `cf`.`cake_id`",
"LEFT JOIN `filling` ON `cf`.`filling_id` = `filling`.`id`",
]
.join(" ")
);
```
2 changes: 2 additions & 0 deletions SeaORM/docs/08-advanced-query/01-custom-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ You can define a partial model, and the corresponding columns will be automatica
```rust
#[derive(DerivePartialModel, FromQueryResult)]
#[sea_orm(entity = "User")]
// `DerivePartialModel` macro attribute `entity` supports `syn::Type` as well
#[sea_orm(entity = "<entity::Model as ModelTrait>::Entity")]
struct PartialUser {
pub id: i32,
pub avatar: String,
Expand Down

0 comments on commit e24647b

Please sign in to comment.