diff --git a/examples/simple-list/Cargo.toml b/examples/simple-list/Cargo.toml new file mode 100644 index 0000000..8e9c691 --- /dev/null +++ b/examples/simple-list/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "simple-list" +edition.workspace = true +version.workspace = true +description.workspace = true +license.workspace = true +repository.workspace = true + +[dependencies] +chrono = "0.4.41" +clap = { version = "4.5.38", features = ["derive"] } +lazy-supplements.workspace = true +lazy-supplements-migration.workspace = true +sea-orm = "1.1.11" +sea-orm-migration.workspace = true +serde = { version = "1.0.219", features = ["derive"] } +tokio = "1.45.1" + +[dev-dependencies] +lazy-supplements = { workspace = true, features = [ "test" ] } \ No newline at end of file diff --git a/examples/simple-list/src/cli/add.rs b/examples/simple-list/src/cli/add.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/simple-list/src/cli/delete.rs b/examples/simple-list/src/cli/delete.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/simple-list/src/cli/list.rs b/examples/simple-list/src/cli/list.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/simple-list/src/cli/mod.rs b/examples/simple-list/src/cli/mod.rs new file mode 100644 index 0000000..a586220 --- /dev/null +++ b/examples/simple-list/src/cli/mod.rs @@ -0,0 +1,16 @@ +use clap::{Parser, Subcommand}; +use lazy_supplements::{cli::ServerArgs, config::PartialServerConfig}; + +#[derive(Debug, Parser)] +pub struct Cli { + #[command(subcommand)] + command: CliCommand +} + +#[derive(Debug, Subcommand)] +pub enum CliCommand { + Add, + Delete, + List, + Server(ServerArgs) +} diff --git a/examples/simple-list/src/cli/server.rs b/examples/simple-list/src/cli/server.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/simple-list/src/entity/list_item.rs b/examples/simple-list/src/entity/list_item.rs new file mode 100644 index 0000000..dc5ace4 --- /dev/null +++ b/examples/simple-list/src/entity/list_item.rs @@ -0,0 +1,58 @@ +use chrono::Local; +use sea_orm::entity::{ + *, + prelude::* +}; +use serde::{Deserialize, Serialize}; + + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)] +#[sea_orm(table_name = "list_item")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + #[sea_orm(indexed)] + pub created_at: DateTimeUtc, + #[sea_orm(indexed)] + pub updated_at: DateTimeUtc, + #[sea_orm(indexed)] + pub is_trashed: bool, + #[sea_orm(indexed)] + pub content: String, +} + +#[derive(Copy, Clone, Debug, DeriveRelation, EnumIter)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} + +impl ActiveModel { + pub fn new() -> Self { + let timestamp: DateTimeUtc = Local::now().to_utc(); + Self{ + id: Set(Uuid::new_v4()), + created_at: Set(timestamp), + updated_at: Set(timestamp), + is_trashed: Set(false), + ..Default::default() + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use crate::global::GLOBAL; + + #[tokio::test] + async fn check_insert_node() { + let db = crate::tests::get_or_init_temporary_database().await; + + ActiveModel{ + content: Set("test note".to_owned()), + ..ActiveModel::new() + }.insert(db).await.unwrap(); + } + +} \ No newline at end of file diff --git a/examples/simple-list/src/entity/mod.rs b/examples/simple-list/src/entity/mod.rs new file mode 100644 index 0000000..e0c7570 --- /dev/null +++ b/examples/simple-list/src/entity/mod.rs @@ -0,0 +1,10 @@ +mod list_item; +pub use lazy_supplements::entity::*; + +pub use list_item::{ + ActiveModel as ListItemActiveModel, + Column as ListItemColumn, + Entity as ListItemEntity, + Model as ListItemModel, +}; + diff --git a/examples/simple-list/src/main.rs b/examples/simple-list/src/main.rs new file mode 100644 index 0000000..d90ff84 --- /dev/null +++ b/examples/simple-list/src/main.rs @@ -0,0 +1,28 @@ +use clap::Parser; + +mod migration; +mod cli; +mod entity; + +pub use lazy_supplements::error; +pub use lazy_supplements::global; + +#[cfg(test)] +pub mod tests { + use sea_orm::DatabaseConnection; + use sea_orm_migration::MigratorTrait; + + use super::*; + pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection { + global::GLOBAL.get_or_try_init_temporary_database( |x| async { + migration::Migrator::up(&x, None).await?; + Ok(x) + }).await.unwrap() + } +} + +#[tokio::main] +async fn main() { + let args = cli::Cli::parse(); + println!("{:?}", args); +} diff --git a/examples/simple-list/src/migration/m20220101_000002_create_simple_list_tables.rs b/examples/simple-list/src/migration/m20220101_000002_create_simple_list_tables.rs new file mode 100644 index 0000000..e3dafaf --- /dev/null +++ b/examples/simple-list/src/migration/m20220101_000002_create_simple_list_tables.rs @@ -0,0 +1,50 @@ +use sea_orm_migration::{prelude::*, schema::*}; + +use lazy_supplements_migration::TableMigration; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + ListItem::up(manager).await?; + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + ListItem::down(manager).await?; + Ok(()) + } +} + +#[derive(DeriveIden)] +enum ListItem { + Table, + Id, + CreatedAt, + UpdatedAt, + IsTrashed, + Content, +} + +#[async_trait::async_trait] +impl TableMigration for ListItem { + async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> { + manager.create_table( + Table::create() + .table(Self::Table) + .if_not_exists() + .col(pk_uuid(Self::Id)) + .col(timestamp(Self::CreatedAt)) + .col(timestamp(Self::UpdatedAt)) + .col(boolean(Self::IsTrashed)) + .col(string_len(Self::Content, 255)) + .to_owned() + ).await?; + Ok(()) + } + async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr>{ + manager.drop_table(Table::drop().table(Self::Table).to_owned()).await + } +} diff --git a/examples/simple-list/src/migration/mod.rs b/examples/simple-list/src/migration/mod.rs new file mode 100644 index 0000000..d9fe571 --- /dev/null +++ b/examples/simple-list/src/migration/mod.rs @@ -0,0 +1,15 @@ +use sea_orm_migration::{prelude::*, schema::*}; +use lazy_supplements_migration::m20220101_000001_create_lazy_supplements_tables; +mod m20220101_000002_create_simple_list_tables; + +pub struct Migrator; + +#[async_trait::async_trait] +impl MigratorTrait for Migrator { + fn migrations() -> Vec> { + vec![ + Box::new(m20220101_000002_create_simple_list_tables::Migration), + Box::new(m20220101_000001_create_lazy_supplements_tables::Migration) + ] + } +} diff --git a/examples/timestamper/Cargo.toml b/examples/timestamper/Cargo.toml deleted file mode 100644 index 8a40a3e..0000000 --- a/examples/timestamper/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "timestamper" -edition.workspace = true -version.workspace = true -description.workspace = true -license.workspace = true -repository.workspace = true - -[dependencies] -lazy-supplements.workspace = true \ No newline at end of file diff --git a/examples/timestamper/src/main.rs b/examples/timestamper/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/examples/timestamper/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/lazy-supplements/src/global/mod.rs b/lazy-supplements/src/global/mod.rs index 0de3e9d..b9c6945 100644 --- a/lazy-supplements/src/global/mod.rs +++ b/lazy-supplements/src/global/mod.rs @@ -13,5 +13,5 @@ pub struct Global { database: OnceCell, } -#[cfg(any(test, feature="test"))] +#[cfg(test)] pub use database::tests::get_or_init_temporary_database; \ No newline at end of file