use indexmap::IndexMap;
use crate::tree::Tree;
pub struct SeederGenerator;
impl SeederGenerator {
pub fn generate(tree: &Tree) -> IndexMap<String, String> {
let mut files = IndexMap::new();
let mut lines = vec![
"//! Database seed functions.".to_string(),
"//! Run these to populate your database with test data.".to_string(),
String::new(),
"use sqlx::PgPool;".to_string(),
"use crate::db::factories::*;".to_string(),
String::new(),
"/// Run all seeders.".to_string(),
"pub async fn run_all(pool: &PgPool) {".to_string(),
];
for model_name in &tree.seeders {
let lower = model_name.to_lowercase();
if tree.models.contains_key(model_name) {
if let Some(model) = tree.models.get(model_name) {
if model.pivot { continue; }
}
lines.push(format!(" create_{}_default(pool).await;", lower));
}
}
lines.push("}".to_string());
let content = lines.join("\n");
files.insert("src/db/seeds.rs".to_string(), content);
files
}
}