Skip to content

Commit

Permalink
Added seeder generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
DenuxPlays committed Dec 7, 2024
1 parent 1dfbdeb commit fa58e83
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions loco-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ mod model;
mod scaffold;
#[cfg(test)]
mod testutil;
#[cfg(feature = "with-db")]
mod seeder;

use std::{str::FromStr, sync::OnceLock};

const MAILER_T: &str = include_str!("templates/mailer/mailer.t");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -245,6 +253,10 @@ pub fn generate(component: Component, appinfo: &AppInfo) -> Result<()> {
let vars =
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,
Expand Down
12 changes: 12 additions & 0 deletions loco-gen/src/seeder.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
let vars = json!({"name": name});
let res = rrgen.generate(DEFAULT_SEEDER_T, &vars)?;

Ok(collect_messages(vec![res]))
}
15 changes: 15 additions & 0 deletions loco-gen/src/templates/seeder/default.t
Original file line number Diff line number Diff line change
@@ -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!()
}
}

0 comments on commit fa58e83

Please sign in to comment.