Skip to content

Commit

Permalink
Seed use AppContext
Browse files Browse the repository at this point in the history
  • Loading branch information
DenuxPlays committed Dec 7, 2024
1 parent a074624 commit 4f3f2d4
Show file tree
Hide file tree
Showing 28 changed files with 95 additions and 66 deletions.
10 changes: 5 additions & 5 deletions docs-site/content/docs/the-app/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ Integrate your seed into the app's Hook implementations by following these steps
impl Hooks for App {
// Other implementations...
async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
async fn seed(ctx: &AppContext, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(&ctx.db, &base.join("users.yaml").display().to_string()).await?;
Ok(())
}
}
Expand Down Expand Up @@ -579,7 +579,7 @@ use loco_rs::testing::prelude::*;
async fn handle_create_with_password_with_duplicate() {
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();
assert!(get_user_by_id(1).ok());
}
```
Expand Down Expand Up @@ -703,7 +703,7 @@ async fn can_find_by_pid() {
configure_insta!();
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();
let existing_user =
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
Expand Down Expand Up @@ -759,7 +759,7 @@ async fn is_user_exists() {
configure_insta!();
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();
assert!(get_user_by_id(1).ok());
}
Expand Down
2 changes: 2 additions & 0 deletions examples/demo/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod m20220101_000001_users;
mod m20231103_114510_notes;
mod m20240416_071825_roles;
mod m20240416_082115_users_roles;
mod m20241207_223633_cars;
pub struct Migrator;

#[async_trait::async_trait]
Expand All @@ -14,6 +15,7 @@ impl MigratorTrait for Migrator {
Box::new(m20231103_114510_notes::Migration),
Box::new(m20240416_071825_roles::Migration),
Box::new(m20240416_082115_users_roles::Migration),
Box::new(m20241207_223633_cars::Migration),
// inject-above (do not remove this comment)
]
}
Expand Down
9 changes: 6 additions & 3 deletions examples/demo/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Hooks for App {

fn routes(ctx: &AppContext) -> AppRoutes {
AppRoutes::with_default_routes()
.add_route(controllers::pages::routes())
.add_route(
controllers::mylayer::routes(ctx.clone())
.layer(middlewares::routes::role::RoleRouteLayer::new(ctx.clone())),
Expand Down Expand Up @@ -109,9 +110,11 @@ impl Hooks for App {
Ok(())
}

async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
db::seed::<notes::ActiveModel>(db, &base.join("notes.yaml").display().to_string()).await?;
async fn seed(ctx: &AppContext, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(&ctx.db, &base.join("users.yaml").display().to_string())
.await?;
db::seed::<notes::ActiveModel>(&ctx.db, &base.join("notes.yaml").display().to_string())
.await?;
Ok(())
}
}
2 changes: 2 additions & 0 deletions examples/demo/src/controllers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pub mod responses;
pub mod upload;
pub mod user;
pub mod view_engine;

pub mod pages;
17 changes: 17 additions & 0 deletions examples/demo/src/controllers/pages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::unnecessary_struct_initialization)]
#![allow(clippy::unused_async)]
use axum::debug_handler;
use loco_rs::prelude::*;

#[debug_handler]
pub async fn about(
ViewEngine(v): ViewEngine<TeraView>,
State(_ctx): State<AppContext>,
) -> Result<Response> {
format::render().view(&v, "pages/about.html", data!({}))
}

pub fn routes() -> Routes {
Routes::new().prefix("pages").add("about", get(about))
}
2 changes: 1 addition & 1 deletion examples/demo/src/controllers/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use axum_extra::extract::cookie::Cookie;
use loco_rs::prelude::*;
use serde::Serialize;
use utoipa::{openapi, OpenApi, ToSchema};
use utoipa::{OpenApi, ToSchema};

#[derive(Serialize)]
pub struct Health {
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/tasks/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Task for SeedData {
db::reset::<Migrator>(&app_context.db).await?;
}
let path = std::path::Path::new("src/fixtures");
db::run_app_seed::<App>(&app_context.db, path).await?;
db::run_app_seed::<App>(&app_context, path).await?;
Ok(())
}
}
1 change: 1 addition & 0 deletions examples/demo/tests/cmd/cli.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ $ demo_app-cli routes --environment test
[GET] /notes/:id
[DELETE] /notes/:id
[POST] /notes/:id
[GET] /pages/about
[GET] /response/album
[GET] /response/empty
[GET] /response/empty_json
Expand Down
2 changes: 2 additions & 0 deletions examples/demo/tests/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod roles;
mod users;
mod users_roles;

mod cars;
14 changes: 7 additions & 7 deletions examples/demo/tests/models/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn handle_create_with_password_with_duplicate() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let new_user: Result<Model, ModelError> = Model::create_with_password(
&boot.app_context.db,
Expand All @@ -81,7 +81,7 @@ async fn can_find_by_email() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let existing_user = Model::find_by_email(&boot.app_context.db, "[email protected]").await;
let non_existing_user_results =
Expand All @@ -97,7 +97,7 @@ async fn can_find_by_pid() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let existing_user =
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
Expand All @@ -114,7 +114,7 @@ async fn can_verification_token() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -143,7 +143,7 @@ async fn can_set_forgot_password_sent() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -172,7 +172,7 @@ async fn can_verified() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand All @@ -199,7 +199,7 @@ async fn can_reset_password() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/tests/requests/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn can_get_or_insert() {
configure_insta!();

request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();
let response = request.get("/cache/get_or_insert").await;
assert_eq!(response.text(), "user1");

Expand Down
6 changes: 3 additions & 3 deletions examples/demo/tests/requests/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn can_get_notes(#[case] test_name: &str, #[case] params: serde_json::Valu
configure_insta!();

request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();

let notes = request.get("notes").add_query_params(params).await;

Expand Down Expand Up @@ -80,7 +80,7 @@ async fn can_get_note() {
configure_insta!();

request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();

let add_note_request = request.get("/notes/1").await;

Expand All @@ -105,7 +105,7 @@ async fn can_delete_note() {
configure_insta!();

request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();

let count_before_delete = Entity::find().all(&ctx.db).await.unwrap().len();
let delete_note_request = request.delete("/notes/1").await;
Expand Down
2 changes: 1 addition & 1 deletion loco-gen/src/templates/model/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn test_model() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

// query your model, e.g.:
//
Expand Down
2 changes: 1 addition & 1 deletion loco-gen/src/templates/seeder/default.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl Seeder for {{struct_name}}Seeder {
"{{struct_name}}Seeder".to_string()
}

async fn seed(&self, conn: &DatabaseConnection) -> AppResult<()> {
async fn seed(&self, ctx: &AppContext) -> AppResult<()> {
todo!()
}
}
4 changes: 2 additions & 2 deletions loco-new/base_template/src/app.rs.t
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ impl Hooks for App {
Ok(())
}
async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
async fn seed(ctx: &AppContext, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(&ctx.db, &base.join("users.yaml").display().to_string()).await?;
Ok(())
}
{%- endif %}
Expand Down
14 changes: 7 additions & 7 deletions loco-new/base_template/tests/models/users.rs.t
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn handle_create_with_password_with_duplicate() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let new_user: Result<Model, ModelError> = Model::create_with_password(
&boot.app_context.db,
Expand All @@ -81,7 +81,7 @@ async fn can_find_by_email() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let existing_user = Model::find_by_email(&boot.app_context.db, "user1@example.com").await;
let non_existing_user_results =
Expand All @@ -97,7 +97,7 @@ async fn can_find_by_pid() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let existing_user =
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
Expand All @@ -114,7 +114,7 @@ async fn can_verification_token() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -143,7 +143,7 @@ async fn can_set_forgot_password_sent() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -172,7 +172,7 @@ async fn can_verified() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand All @@ -199,7 +199,7 @@ async fn can_reset_password() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Hooks for App {
Ok(())
}

async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
async fn seed(ctx: &AppContext, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(&ctx.db, &base.join("users.yaml").display().to_string()).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Hooks for App {
Ok(())
}

async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
async fn seed(ctx: &AppContext, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(&ctx.db, &base.join("users.yaml").display().to_string()).await?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub trait Hooks: Send {

/// Seeds the database with initial data.
#[cfg(feature = "with-db")]
async fn seed(db: &DatabaseConnection, path: &Path) -> Result<()>;
async fn seed(db: &AppContext, path: &Path) -> Result<()>;

/// Called when the application is shutting down.
/// This function allows users to perform any necessary cleanup or final
Expand Down
2 changes: 1 addition & 1 deletion src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub async fn run_db<H: Hooks, M: MigratorTrait>(
if reset {
db::reset::<M>(&app_context.db).await?;
}
db::run_app_seed::<H>(&app_context.db, &from).await?;
db::run_app_seed::<H>(app_context, &from).await?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
//! Ok(())
//! }
//!
//! async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
//! async fn seed(db: &AppContext, base: &Path) -> Result<()> {
//! Ok(())
//! }
//! }
Expand Down
Loading

0 comments on commit 4f3f2d4

Please sign in to comment.