diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 9fc6077..ce93d1c 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -1,12 +1,16 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_create_user; +mod m20230322_103045_create_session; pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![Box::new(m20220101_000001_create_user::Migration)] + vec![ + Box::new(m20220101_000001_create_user::Migration), + Box::new(m20230322_103045_create_session::Migration), + ] } } diff --git a/migration/src/m20220101_000001_create_user.rs b/migration/src/m20220101_000001_create_user.rs index c009d9e..a50a8ac 100644 --- a/migration/src/m20220101_000001_create_user.rs +++ b/migration/src/m20220101_000001_create_user.rs @@ -36,7 +36,7 @@ impl MigrationTrait for Migration { /// Learn more at https://docs.rs/sea-query#iden #[derive(Iden)] -enum User { +pub enum User { Table, Id, Username, diff --git a/migration/src/m20230322_103045_create_session.rs b/migration/src/m20230322_103045_create_session.rs new file mode 100644 index 0000000..963cdf8 --- /dev/null +++ b/migration/src/m20230322_103045_create_session.rs @@ -0,0 +1,50 @@ +use sea_orm_migration::prelude::*; +use crate::m20220101_000001_create_user::User; + +#[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(Session::Table) + .if_not_exists() + .col( + ColumnDef::new(Session::Id) + .text() + .not_null() + .primary_key(), + ) + .col(ColumnDef::new(Session::User).text().not_null()) + .col(ColumnDef::new(Session::Token).text().not_null()) + .col(ColumnDef::new(Session::Expiry).date_time().not_null()) + .foreign_key( + ForeignKey::create() + .name("fk-session-user_id") + .from(Session::Table, Session::User) + .to(User::Table, User::Id) + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Session::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Session { + Table, + Id, + User, + Token, + Expiry +} diff --git a/src/entities/mod.rs b/src/entities/mod.rs new file mode 100644 index 0000000..bd72279 --- /dev/null +++ b/src/entities/mod.rs @@ -0,0 +1,6 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1 + +pub mod prelude; + +pub mod session; +pub mod user; diff --git a/src/entities/prelude.rs b/src/entities/prelude.rs new file mode 100644 index 0000000..c9f9f82 --- /dev/null +++ b/src/entities/prelude.rs @@ -0,0 +1,4 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1 + +pub use super::session::Entity as Session; +pub use super::user::Entity as User; diff --git a/src/entities/session.rs b/src/entities/session.rs new file mode 100644 index 0000000..1813cf7 --- /dev/null +++ b/src/entities/session.rs @@ -0,0 +1,35 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "session")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false, column_type = "Text")] + pub id: String, + #[sea_orm(column_type = "Text")] + pub user: String, + #[sea_orm(column_type = "Text")] + pub token: String, + pub expiry: DateTime, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::User", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entities/user.rs b/src/entities/user.rs new file mode 100644 index 0000000..e171d9a --- /dev/null +++ b/src/entities/user.rs @@ -0,0 +1,32 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "user")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false, column_type = "Text")] + pub id: String, + #[sea_orm(column_type = "Text")] + pub username: String, + #[sea_orm(column_type = "Text")] + pub email: String, + #[sea_orm(column_type = "Text")] + pub password_hash: String, + pub bio: String, + pub flags: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::session::Entity")] + Session, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Session.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/main.rs b/src/main.rs index 5d6bc2d..9059a52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod options; mod api; mod frontend; +mod entities; use std::net::SocketAddr; use axum::Router;