Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to seed via rust code #1061

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions loco-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
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 @@ -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.as_str())?;
}
Component::Controller {
name,
actions,
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: &str) -> 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!()
}
}
23 changes: 21 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! This module defines functions and operations related to the application's
//! database interactions.

use std::{collections::HashMap, fs::File, io::Write, path::Path, sync::OnceLock, time::Duration};

use chrono::{DateTime, Utc};
use duct::cmd;
use fs_err::{self as fs, create_dir_all};
Expand All @@ -14,6 +12,8 @@ use sea_orm::{
DatabaseConnection, DbConn, EntityTrait, IntoActiveModel, Statement,
};
use sea_orm_migration::MigratorTrait;
use std::future::Future;
use std::{collections::HashMap, fs::File, io::Write, path::Path, sync::OnceLock, time::Duration};
use tracing::info;

use super::Result as AppResult;
Expand All @@ -35,6 +35,16 @@ fn get_extract_db_name() -> &'static Regex {
EXTRACT_DB_NAME.get_or_init(|| Regex::new(r"/([^/]+)$").unwrap())
}

/// A trait for seeding the database with initial data.
/// Seeders should be kept in `src/seeders`.
pub trait Seeder: Send + Sync {
/// The unique name of the seeder.
fn name(&self) -> String;

/// Seeds the database with initial data.
fn seed(&self, db: &DatabaseConnection) -> impl Future<Output = AppResult<()>> + Send + Sync;
}

#[derive(Default, Clone, Debug)]
pub struct MultiDb {
pub db: HashMap<String, DatabaseConnection>,
Expand Down Expand Up @@ -277,6 +287,15 @@ where
Ok(())
}

/// Seed the database with the given Seeder.
///
/// # Errors
///
/// Returns an error if the seeder fails.
pub async fn seed_via_seeder(db: &DatabaseConnection, seeder: &impl Seeder) -> crate::Result<()> {
seeder.seed(db).await
}

/// Checks if the specified table has an 'id' column.
///
/// This function checks if the specified table has an 'id' column, which is a
Expand Down
Loading