-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add salvo example * cargo clippy * bug fix
- Loading branch information
1 parent
f667213
commit b673991
Showing
23 changed files
with
1,486 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
HOST=127.0.0.1 | ||
PORT=8000 | ||
#DATABASE_URL="mysql://root:@localhost/poem_example" | ||
DATABASE_URL="sqlite::memory:" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "sea-orm-salvo-example" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[workspace] | ||
members = [".", "entity", "migration"] | ||
|
||
[dependencies] | ||
tokio = { version = "1.15.0", features = ["macros", "rt-multi-thread"] } | ||
salvo = { version = "0.27", features = ["affix", "serve-static"] } | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
serde = { version = "1", features = ["derive"] } | ||
tera = "1.8.0" | ||
dotenv = "0.15" | ||
entity = { path = "entity" } | ||
migration = { path = "migration" } | ||
|
||
[dependencies.sea-orm] | ||
path = "../../" # remove this line in your own project | ||
version = "^0.9.1" # sea-orm version | ||
features = [ | ||
"debug-print", | ||
"runtime-tokio-native-tls", | ||
"sqlx-sqlite", | ||
# "sqlx-postgres", | ||
# "sqlx-mysql", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
![screenshot](Screenshot.png) | ||
|
||
# Poem with SeaORM example app | ||
|
||
1. Modify the `DATABASE_URL` var in `.env` to point to your chosen database | ||
|
||
1. Turn on the appropriate database feature for your chosen db in `Cargo.toml` (the `"sqlx-sqlite",` line) | ||
|
||
1. Execute `cargo run` to start the server | ||
|
||
1. Visit [localhost:8000](http://localhost:8000) in browser after seeing the `server started` line |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "entity" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
name = "entity" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
serde = { version = "1", features = ["derive"] } | ||
|
||
[dependencies.sea-orm] | ||
path = "../../../" # remove this line in your own project | ||
version = "^0.9.1" # sea-orm version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod post; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use sea_orm::entity::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)] | ||
#[sea_orm(table_name = "posts")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
#[serde(skip_deserializing)] | ||
pub id: i32, | ||
pub title: String, | ||
#[sea_orm(column_type = "Text")] | ||
pub text: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "migration" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
name = "migration" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
async-std = { version = "^1", features = ["attributes", "tokio1"] } | ||
|
||
[dependencies.sea-orm-migration] | ||
path = "../../../sea-orm-migration" # remove this line in your own project | ||
version = "^0.9.1" # sea-orm-migration version | ||
features = [ | ||
# Enable following runtime and db backend features if you want to run migration via CLI | ||
# "runtime-tokio-native-tls", | ||
# "sqlx-sqlite", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Running Migrator CLI | ||
|
||
- Apply all pending migrations | ||
```sh | ||
cargo run | ||
``` | ||
```sh | ||
cargo run -- up | ||
``` | ||
- Apply first 10 pending migrations | ||
```sh | ||
cargo run -- up -n 10 | ||
``` | ||
- Rollback last applied migrations | ||
```sh | ||
cargo run -- down | ||
``` | ||
- Rollback last 10 applied migrations | ||
```sh | ||
cargo run -- down -n 10 | ||
``` | ||
- Drop all tables from the database, then reapply all migrations | ||
```sh | ||
cargo run -- fresh | ||
``` | ||
- Rollback all applied migrations, then reapply all migrations | ||
```sh | ||
cargo run -- refresh | ||
``` | ||
- Rollback all applied migrations | ||
```sh | ||
cargo run -- reset | ||
``` | ||
- Check the status of all migrations | ||
```sh | ||
cargo run -- status | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pub use sea_orm_migration::prelude::*; | ||
|
||
mod m20220120_000001_create_post_table; | ||
|
||
pub struct Migrator; | ||
|
||
#[async_trait::async_trait] | ||
impl MigratorTrait for Migrator { | ||
fn migrations() -> Vec<Box<dyn MigrationTrait>> { | ||
vec![Box::new(m20220120_000001_create_post_table::Migration)] | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
examples/salvo_example/migration/src/m20220120_000001_create_post_table.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.create_table( | ||
Table::create() | ||
.table(Posts::Table) | ||
.if_not_exists() | ||
.col( | ||
ColumnDef::new(Posts::Id) | ||
.integer() | ||
.not_null() | ||
.auto_increment() | ||
.primary_key(), | ||
) | ||
.col(ColumnDef::new(Posts::Title).string().not_null()) | ||
.col(ColumnDef::new(Posts::Text).string().not_null()) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.drop_table(Table::drop().table(Posts::Table).to_owned()) | ||
.await | ||
} | ||
} | ||
|
||
/// Learn more at https://docs.rs/sea-query#iden | ||
#[derive(Iden)] | ||
enum Posts { | ||
Table, | ||
Id, | ||
Title, | ||
Text, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
cli::run_cli(migration::Migrator).await; | ||
} |
Oops, something went wrong.