mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
docs: Add documentation supporting native, asynchronous database operations (#580)
* feat: add sea orm documents * feat: add description for sea orm cli
This commit is contained in:
15
examples/sea-orm-databases/Cargo.toml
Normal file
15
examples/sea-orm-databases/Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "sea-orm-databases"
|
||||
version = "0.1.0"
|
||||
publish = false
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
sea-orm = { version = "1.1.0", features = [
|
||||
"sqlx-sqlite",
|
||||
"runtime-tokio-native-tls",
|
||||
"macros",
|
||||
] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
uuid = { version = "1", features = ["v4"] }
|
74
examples/sea-orm-databases/src/main.rs
Normal file
74
examples/sea-orm-databases/src/main.rs
Normal file
@ -0,0 +1,74 @@
|
||||
use std::io;
|
||||
|
||||
use actix_web::{error, web, App, HttpResponse, HttpServer, Responder};
|
||||
use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, Set};
|
||||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
// <handler>
|
||||
// Importing the existing entity modules
|
||||
pub mod prelude;
|
||||
pub mod users;
|
||||
use crate::users::ActiveModel as UserActiveModel;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct User {
|
||||
id: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
async fn insert_new_user(
|
||||
conn: &DatabaseConnection,
|
||||
user_name: String,
|
||||
) -> Result<User, sea_orm::DbErr> {
|
||||
// Create insertion model
|
||||
let uid = Uuid::new_v4().to_string();
|
||||
let new_user = UserActiveModel {
|
||||
id: Set(uid.clone()),
|
||||
name: Set(user_name),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Insert the user
|
||||
let user = new_user.insert(conn).await?;
|
||||
|
||||
Ok(User {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
})
|
||||
}
|
||||
// </handler>
|
||||
|
||||
// <main>
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
let database_url = "sqlite:app.db";
|
||||
let conn = Database::connect(database_url)
|
||||
.await
|
||||
.expect("Failed to connect to database");
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(web::Data::new(conn.clone()))
|
||||
.route("/{name}", web::get().to(index))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </main>
|
||||
|
||||
// <index>
|
||||
async fn index(
|
||||
conn: web::Data<DatabaseConnection>,
|
||||
name: web::Path<(String,)>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let (name,) = name.into_inner();
|
||||
|
||||
let user = insert_new_user(&conn, name)
|
||||
.await
|
||||
.map_err(error::ErrorInternalServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(user))
|
||||
}
|
||||
// </index>
|
5
examples/sea-orm-databases/src/mod.rs
Normal file
5
examples/sea-orm-databases/src/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod users;
|
3
examples/sea-orm-databases/src/prelude.rs
Normal file
3
examples/sea-orm-databases/src/prelude.rs
Normal file
@ -0,0 +1,3 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4
|
||||
|
||||
pub use super::users::Entity as Users;
|
16
examples/sea-orm-databases/src/users.rs
Normal file
16
examples/sea-orm-databases/src/users.rs
Normal file
@ -0,0 +1,16 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "users")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
Reference in New Issue
Block a user