feat: ✨ seaorm
This commit is contained in:
parent
36c72cee4a
commit
40149b372d
20 changed files with 236 additions and 87 deletions
|
@ -3,11 +3,16 @@ name = "rust_forum"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
[workspace]
|
||||||
|
members = [".", "entity", "migration"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
entity = { path = "entity" }
|
||||||
|
migration = { path = "migration" }
|
||||||
axum = "0.7.9"
|
axum = "0.7.9"
|
||||||
chrono = { version = "0.4.38", features = ["serde"] }
|
chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
|
sea-orm = { version = "1.1.2", features = ["macros", "runtime-tokio-rustls", "sqlx-postgres", "with-chrono", "with-json"] }
|
||||||
serde = { version = "1.0.215", features = ["derive"] }
|
serde = { version = "1.0.215", features = ["derive"] }
|
||||||
serde_json = "1.0.133"
|
serde_json = "1.0.133"
|
||||||
surrealdb = "2.1.1"
|
|
||||||
tokio = { version = "1.41.1", features = ["full"] }
|
tokio = { version = "1.41.1", features = ["full"] }
|
||||||
tower-http = { version = "0.6.2", features = ["cors"] }
|
tower-http = { version = "0.6.2", features = ["cors"] }
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[database_config]
|
[database_config]
|
||||||
address = "localhost:8000"
|
address = "localhost:5432"
|
||||||
username = "root"
|
username = "root"
|
||||||
password = "root"
|
password = "root"
|
||||||
namespace = "rust_forum"
|
database = "rust_forum"
|
||||||
database = "rust_forum"
|
backend = "postgres"
|
7
entity/Cargo.toml
Normal file
7
entity/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "entity"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
sea-orm = "1.1.2"
|
1
entity/src/lib.rs
Normal file
1
entity/src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
22
migration/Cargo.toml
Normal file
22
migration/Cargo.toml
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
[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]
|
||||||
|
version = "1.1.0"
|
||||||
|
features = [
|
||||||
|
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
|
||||||
|
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
|
||||||
|
# e.g.
|
||||||
|
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
|
||||||
|
"sqlx-postgres", # `DATABASE_DRIVER` feature
|
||||||
|
]
|
41
migration/README.md
Normal file
41
migration/README.md
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
# Running Migrator CLI
|
||||||
|
|
||||||
|
- Generate a new migration file
|
||||||
|
```sh
|
||||||
|
cargo run -- generate MIGRATION_NAME
|
||||||
|
```
|
||||||
|
- 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
|
||||||
|
```
|
12
migration/src/lib.rs
Normal file
12
migration/src/lib.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
pub use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
|
mod m20241202_201137_create_user_table;
|
||||||
|
|
||||||
|
pub struct Migrator;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl MigratorTrait for Migrator {
|
||||||
|
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||||
|
vec![Box::new(m20241202_201137_create_user_table::Migration)]
|
||||||
|
}
|
||||||
|
}
|
69
migration/src/m20241202_201137_create_user_table.rs
Normal file
69
migration/src/m20241202_201137_create_user_table.rs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
use extension::postgres::Type;
|
||||||
|
use sea_orm::{EnumIter, Iterable};
|
||||||
|
use sea_orm_migration::{prelude::*, schema::*};
|
||||||
|
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
struct Role;
|
||||||
|
|
||||||
|
#[derive(Iden, EnumIter)]
|
||||||
|
enum RoleType {
|
||||||
|
#[iden = "Default"]
|
||||||
|
Default,
|
||||||
|
#[iden = "Admin"]
|
||||||
|
Admin,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
enum User {
|
||||||
|
Table,
|
||||||
|
Id,
|
||||||
|
Name,
|
||||||
|
Surname,
|
||||||
|
Gender,
|
||||||
|
BirthDate,
|
||||||
|
Email,
|
||||||
|
Role,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DeriveMigrationName)]
|
||||||
|
pub struct Migration;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl MigrationTrait for Migration {
|
||||||
|
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager
|
||||||
|
.create_type(
|
||||||
|
Type::create()
|
||||||
|
.as_enum(Role)
|
||||||
|
.values(RoleType::iter())
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
manager
|
||||||
|
.create_table(
|
||||||
|
Table::create()
|
||||||
|
.table(User::Table)
|
||||||
|
.if_not_exists()
|
||||||
|
.col(pk_auto(User::Id))
|
||||||
|
.col(string(User::Name))
|
||||||
|
.col(string(User::Surname))
|
||||||
|
.col(string(User::Email))
|
||||||
|
.col(date_time(User::BirthDate))
|
||||||
|
.col(boolean(User::Gender))
|
||||||
|
.col(enumeration(
|
||||||
|
User::Role,
|
||||||
|
Alias::new("role"),
|
||||||
|
RoleType::iter(),
|
||||||
|
))
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager
|
||||||
|
.drop_table(Table::drop().table(User::Table).to_owned())
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
6
migration/src/main.rs
Normal file
6
migration/src/main.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
|
#[async_std::main]
|
||||||
|
async fn main() {
|
||||||
|
cli::run_cli(migration::Migrator).await;
|
||||||
|
}
|
|
@ -1,46 +1,31 @@
|
||||||
pub mod interaction;
|
pub mod interaction;
|
||||||
|
pub mod message;
|
||||||
pub mod post;
|
pub mod post;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
pub type SurrealUserReturn = Result<Option<User>, surrealdb::Error>;
|
use std::time::Duration;
|
||||||
pub type SurrealCountReturn = Result<Option<u128>, surrealdb::Error>;
|
|
||||||
|
|
||||||
use std::{sync::LazyLock, time::Duration};
|
use sea_orm::{Database, DatabaseConnection};
|
||||||
|
|
||||||
use surrealdb::{
|
|
||||||
engine::remote::ws::{Client, Ws},
|
|
||||||
opt::auth::Root,
|
|
||||||
Surreal,
|
|
||||||
};
|
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
use crate::{feature::user::User, DatabaseConfig};
|
use crate::DatabaseConfig;
|
||||||
|
|
||||||
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
|
pub async fn establish_connection() -> DatabaseConnection {
|
||||||
|
|
||||||
pub async fn establish_connection() {
|
|
||||||
let database_config = DatabaseConfig::default();
|
let database_config = DatabaseConfig::default();
|
||||||
|
let connection_string = format!(
|
||||||
DB.connect::<Ws>(database_config.address).await.unwrap();
|
"{}://{}:{}@{}/{}",
|
||||||
DB.signin(Root {
|
database_config.backend,
|
||||||
username: &database_config.username,
|
database_config.username,
|
||||||
password: &database_config.password,
|
database_config.password,
|
||||||
})
|
database_config.address,
|
||||||
.await
|
database_config.database
|
||||||
.unwrap();
|
);
|
||||||
DB.use_ns(database_config.namespace).await.unwrap();
|
Database::connect(connection_string).await.unwrap()
|
||||||
DB.use_db(database_config.database).await.unwrap();
|
|
||||||
DB.query("DEFINE INDEX email ON TABLE user FIELDS email UNIQUE;").await.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn is_alive() -> bool {
|
pub async fn is_alive() -> bool {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
alive_status = DB.health() => {
|
|
||||||
match alive_status {
|
_ = sleep(Duration::from_secs(1)) => false,
|
||||||
Ok(_) => true,
|
|
||||||
Err(_) => false,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ = sleep(Duration::from_secs(1)) => false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1 @@
|
||||||
async fn create_interaction() {}
|
|
||||||
async fn read_interaction() {}
|
|
||||||
async fn update_interaction() {}
|
|
||||||
async fn delete_interaction() {}
|
|
||||||
async fn count_interactions_of_a_user() {}
|
|
||||||
async fn count_interactions_of_a_post() {}
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -1,5 +1 @@
|
||||||
async fn create_post() {}
|
|
||||||
async fn read_post() {}
|
|
||||||
async fn update_post() {}
|
|
||||||
async fn delete_post() {}
|
|
||||||
async fn count_posts_of_a_user() {}
|
|
||||||
|
|
|
@ -1,29 +1 @@
|
||||||
|
|
||||||
use crate::feature::user::User;
|
|
||||||
|
|
||||||
use super::{SurrealCountReturn, SurrealUserReturn, DB};
|
|
||||||
|
|
||||||
|
|
||||||
pub async fn create_user(user:User) -> SurrealUserReturn {
|
|
||||||
DB.create("user").content(user).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn read_user(email: &String) -> SurrealUserReturn{
|
|
||||||
DB.select(("user", email)).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn update_user(target_user_email: &String, user: User) -> SurrealUserReturn{
|
|
||||||
DB.update(("user", target_user_email)).content(user).await
|
|
||||||
}
|
|
||||||
pub async fn delete_user(email: &String) -> SurrealUserReturn {
|
|
||||||
DB.delete(("user", email)).await
|
|
||||||
}
|
|
||||||
pub async fn count_users() -> SurrealCountReturn{
|
|
||||||
DB.query("SELECT count() FROM user GROUP BY count;").await?.take("count")
|
|
||||||
}
|
|
||||||
pub async fn count_male_users() -> SurrealCountReturn{
|
|
||||||
DB.query("SELECT count() FROM user WHERE gender = true GROUP BY count;").await?.take("count")
|
|
||||||
}
|
|
||||||
pub async fn count_female_users() -> SurrealCountReturn{
|
|
||||||
DB.query("SELECT count() FROM user WHERE gender = false GROUP BY count;").await?.take("count")
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
pub mod user;
|
pub mod interaction;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
pub mod post;
|
pub mod post;
|
||||||
pub mod interaction;
|
pub mod user;
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
enum InteractionType {
|
enum InteractionType {
|
||||||
Like,
|
Like,
|
||||||
Dislike,
|
Dislike,
|
||||||
|
@ -8,9 +11,12 @@ enum InteractionType {
|
||||||
Rocket,
|
Rocket,
|
||||||
Smile,
|
Smile,
|
||||||
Laugh,
|
Laugh,
|
||||||
|
Sad,
|
||||||
|
Shrug,
|
||||||
}
|
}
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Interaction {
|
struct Interaction {
|
||||||
post_id:String,
|
post_id: String,
|
||||||
user_id:String,
|
user_id: String,
|
||||||
interaction_type: InteractionType,
|
interaction_type: InteractionType,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,21 @@
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub sender_email: String,
|
pub sender_email: String,
|
||||||
pub receiver_email: String,
|
pub receiver_email: String,
|
||||||
pub message: String,
|
pub message: String,
|
||||||
|
pub execution_time: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
pub async fn new(sender_email: String, receiver_email: String, message: String) -> Self{
|
pub async fn new(sender_email: String, receiver_email: String, message: String) -> Self {
|
||||||
Message {
|
Self {
|
||||||
sender_email,
|
sender_email,
|
||||||
receiver_email,
|
receiver_email,
|
||||||
message,
|
message,
|
||||||
|
execution_time: Utc::now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Post {
|
||||||
|
pub poster_email: String,
|
||||||
|
pub post: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Post {
|
||||||
|
pub async fn new(poster_email: String, post: String) -> Self {
|
||||||
|
Self { poster_email, post }
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,7 +24,13 @@ pub struct User {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl User {
|
impl User {
|
||||||
pub async fn new(name: Vec<String>, surname: Vec<String>, gender: bool, birth_date: NaiveDate, email: String) -> Self {
|
pub async fn new(
|
||||||
|
name: Vec<String>,
|
||||||
|
surname: Vec<String>,
|
||||||
|
gender: bool,
|
||||||
|
birth_date: NaiveDate,
|
||||||
|
email: String,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name,
|
name,
|
||||||
surname,
|
surname,
|
||||||
|
@ -34,4 +40,4 @@ impl User {
|
||||||
role: Role::User,
|
role: Role::User,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -1,7 +1,8 @@
|
||||||
pub mod feature;
|
|
||||||
pub mod database;
|
pub mod database;
|
||||||
|
pub mod feature;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
|
use sea_orm::DatabaseConnection;
|
||||||
use utils::naive_toml_parser;
|
use utils::naive_toml_parser;
|
||||||
|
|
||||||
const DATABASE_CONFIG_FILE_LOCATION: &str = "./configs/database_config.toml";
|
const DATABASE_CONFIG_FILE_LOCATION: &str = "./configs/database_config.toml";
|
||||||
|
@ -12,8 +13,8 @@ pub struct DatabaseConfig {
|
||||||
pub address: String,
|
pub address: String,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
pub namespace: String,
|
|
||||||
pub database: String,
|
pub database: String,
|
||||||
|
pub backend: String,
|
||||||
}
|
}
|
||||||
impl Default for DatabaseConfig {
|
impl Default for DatabaseConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
@ -21,8 +22,8 @@ impl Default for DatabaseConfig {
|
||||||
|
|
||||||
if header == "[database_config]" {
|
if header == "[database_config]" {
|
||||||
Self {
|
Self {
|
||||||
|
backend: database_configs.pop().unwrap(),
|
||||||
database: database_configs.pop().unwrap(),
|
database: database_configs.pop().unwrap(),
|
||||||
namespace: database_configs.pop().unwrap(),
|
|
||||||
password: database_configs.pop().unwrap(),
|
password: database_configs.pop().unwrap(),
|
||||||
username: database_configs.pop().unwrap(),
|
username: database_configs.pop().unwrap(),
|
||||||
address: database_configs.pop().unwrap(),
|
address: database_configs.pop().unwrap(),
|
||||||
|
@ -51,3 +52,8 @@ impl Default for ServerConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct AppState {
|
||||||
|
pub database_connection: DatabaseConnection,
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue