Add --database subcommand to rapina new command #473
Merged
arferreira merged 5 commits intorapina-rs:mainfrom Apr 13, 2026
Merged
Add --database subcommand to rapina new command #473arferreira merged 5 commits intorapina-rs:mainfrom
arferreira merged 5 commits intorapina-rs:mainfrom
Conversation
Implement database configuration support for project scaffolding with conditional code generation based on the `--db` flag. Key changes: - Add `--db <sqlite|postgres|mysql>` argument with clap value validation - Enforce `--db` requirement for `--template crud` via clap's requires_if - Conditionally generate `.with_database().await?`, `load_dotenv()`, and DB imports only when `--db` is provided - Replace `.env.example` with direct `.env` generation containing clear placeholder values and replacement warnings in English - Unify env generation: `generate_env_content(db_type: Option, extra_vars: Option)` - Refactor CRUD template to require explicit `&DatabaseType` (DB is mandatory) - Extract shared `generate_with_database_line()` helper to eliminate duplication - Place `.await?` on a separate line for proper async builder chain formatting - Validate template name and DB requirement before creating any directories - Move `.env` generation responsibility to individual templates - Update 122 tests to cover new conditional logic and refactored signatures Fixes template generation issues where database code was always injected, even when no database was configured.
arferreira
requested changes
Apr 11, 2026
Collaborator
arferreira
left a comment
There was a problem hiding this comment.
Hey, good feature, nice work so far, just left two things to fix before merging.
- Use `expect()` instead of `?` for database config initialization - Refactor `generate_gitignore_extras` to always include `.env` - Add explicit router setup in REST API template - Parse `DatabaseType` directly in CLI args using clap - Simplify database type handling in `main.rs` - Add test for MySQL gitignore extras
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
What does this PR do?
This PR introduces comprehensive database configuration support for the
rapina newcommand, enabling users to scaffold projects with or without database integration based on the new--dbflag. It refactors the template generation system to conditionally inject database-related code, centralizes environment variable generation, and improves CLI validation.Key Changes:
CLI & Validation:
--db <sqlite|postgres|mysql>flag with strict value parsing.--dbrequirement for--template crudusing Clap'srequires_ifvalidation.Conditional Template Generation:
.with_database(),load_dotenv(),DatabaseConfigimports) is now only generated when--dbis explicitly provided.Environment Configuration:
.env.examplewith direct.envgeneration containing clear placeholder values and English replacement warnings (⚠️ Replace the values below...)..envcreation responsibility to each individual template (rest_api,auth,crud) for better separation of concerns.Architecture & Safety:
&DatabaseTypeexplicitly, as CRUD scaffolds inherently need a database.Related Issues
Closes #371
Checklist
cargo fmtpassescargo clippyhas no warnings