diff --git a/loco-gen/src/lib.rs b/loco-gen/src/lib.rs index 90ed56a93..4a148f142 100644 --- a/loco-gen/src/lib.rs +++ b/loco-gen/src/lib.rs @@ -11,8 +11,11 @@ mod controller; mod model; #[cfg(feature = "with-db")] mod scaffold; +#[cfg(feature = "with-db")] +mod seeder; #[cfg(test)] mod testutil; + use std::{str::FromStr, sync::OnceLock}; const MAILER_T: &str = include_str!("templates/mailer/mailer.t"); @@ -171,6 +174,11 @@ pub enum Component { // k kind: ScaffoldKind, }, + #[cfg(feature = "with-db")] + Seeder { + /// Name of the thing to generate + name: String, + }, Controller { /// Name of the thing to generate name: String, @@ -246,6 +254,10 @@ pub fn generate(component: Component, appinfo: &AppInfo) -> Result<()> { json!({ "name": name, "ts": chrono::Utc::now(), "pkg_name": appinfo.app_name}); rrgen.generate(MIGRATION_T, &vars)?; } + #[cfg(feature = "with-db")] + Component::Seeder { name } => { + seeder::generate(&rrgen, name)?; + } Component::Controller { name, actions, diff --git a/loco-gen/src/seeder.rs b/loco-gen/src/seeder.rs new file mode 100644 index 000000000..f1c1fe6de --- /dev/null +++ b/loco-gen/src/seeder.rs @@ -0,0 +1,12 @@ +use super::{collect_messages, Result}; +use rrgen::RRgen; +use serde_json::json; + +const DEFAULT_SEEDER_T: &str = include_str!("templates/seeder/default.t"); + +pub fn generate(rrgen: &RRgen, name: String) -> Result { + let vars = json!({"name": name}); + let res = rrgen.generate(DEFAULT_SEEDER_T, &vars)?; + + Ok(collect_messages(vec![res])) +} diff --git a/loco-gen/src/templates/seeder/default.t b/loco-gen/src/templates/seeder/default.t new file mode 100644 index 000000000..a56f3d084 --- /dev/null +++ b/loco-gen/src/templates/seeder/default.t @@ -0,0 +1,15 @@ +{% set module_name = name | snake_case -%} +{% set struct_name = module_name | pascal_case -%} +use loco_rs::prelude::*; + +pub struct {{struct_name}}Seeder; + +impl Seeder for {{struct_name}}Seeder { + fn name(&self) -> String { + "{{struct_name}}Seeder".to_string() + } + + async fn seed(&self, conn: &DatabaseConnection) -> AppResult<()> { + todo!() + } +}