Skip to content

Conversation

@AmanVarshney01
Copy link
Owner

@AmanVarshney01 AmanVarshney01 commented Jun 20, 2025

Summary by CodeRabbit

  • New Features
    • Added support for Cloudflare D1 as a database setup option for projects using the Workers runtime.
    • CLI prompts and configuration templates now include options and setup guidance for Cloudflare D1.
  • Validation
    • Introduced checks to ensure Cloudflare D1 can only be selected with SQLite and the Workers runtime.
  • Documentation
    • Enhanced configuration templates with detailed instructions for setting up Cloudflare D1 and managing environment variables.

@changeset-bot
Copy link

changeset-bot bot commented Jun 20, 2025

🦋 Changeset detected

Latest commit: 81cb28d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
create-better-t-stack Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 20, 2025

Walkthrough

Support for Cloudflare D1 as a database setup option was integrated into the CLI project. This includes updates to type definitions, validation, prompts, setup logic, environment configuration, and templates for Drizzle and Workers. Conditional logic ensures D1 is only selectable with the correct runtime and database type, with corresponding environment and configuration changes.

Changes

File(s) Change Summary
apps/cli/src/helpers/database-providers/d1-setup.ts Added setupCloudflareD1 function to configure D1 environment variables.
apps/cli/src/helpers/project-generation/env-setup.ts Extended specialized setup detection to include "d1".
apps/cli/src/helpers/setup/db-setup.ts Imported and invoked setupCloudflareD1 for SQLite with "d1" setup.
apps/cli/src/prompts/config-prompts.ts Passed runtime to getDBSetupChoice in prompts.
apps/cli/src/prompts/database-setup.ts Added "Cloudflare D1" as a conditional option for SQLite when runtime is "workers"; updated function signature.
apps/cli/src/types.ts Added "d1" to DatabaseSetupSchema enum.
apps/cli/src/validation.ts Enforced that "d1" can only be used with SQLite and Workers runtime.
apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs Added conditional config for D1 with Cloudflare-specific environment variables.
apps/cli/templates/db/drizzle/sqlite/src/db/index.ts.hbs Conditional import/initialization of Drizzle client for D1 under Workers runtime.
apps/cli/templates/runtime/workers/apps/server/wrangler.jsonc.hbs Improved comments and added conditional D1 database configuration and instructions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Prompts
    participant Setup
    participant EnvSetup
    participant Validation

    User->>CLI: Run project setup
    CLI->>Prompts: Gather config (includes runtime)
    Prompts->>Prompts: getDBSetupChoice (now includes D1 if runtime is workers)
    Prompts->>CLI: Return config (may include dbSetup: "d1")
    CLI->>Validation: Validate config
    Validation->>CLI: Enforce D1 only with SQLite and Workers
    CLI->>Setup: setupDatabase
    Setup->>Setup: If dbSetup is "d1" and database is "sqlite"
    Setup->>EnvSetup: setupCloudflareD1 (add env vars)
    Setup->>CLI: Complete setup
Loading

Poem

🐰
D1 arrives, a cloud-bound friend,
With Workers runtime, rules to defend.
Prompts and configs now know its name,
Env vars set, templates in frame.
SQLite joins the fluffy sky,
As rabbits code and clouds pass by!
☁️🥕

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 20, 2025

Open in StackBlitz

npx https://pkg.pr.new/AmanVarshney01/create-better-t-stack@335

commit: 81cb28d

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/cli/src/helpers/database-providers/d1-setup.ts (1)

8-36: Consider improving error handling and adding input validation.

The function implementation is solid but could benefit from better error handling and validation:

  1. Silent error handling: The try-catch block silently ignores all errors, which might hide important setup failures from users.
  2. Path assumptions: The function assumes the apps/server directory structure exists.

Consider this improvement:

export async function setupCloudflareD1(config: ProjectConfig): Promise<void> {
	const { projectDir } = config;

+	// Validate that this is actually a D1 setup
+	if (config.dbSetup !== "d1") {
+		throw new Error("setupCloudflareD1 should only be called for D1 database setup");
+	}

	const envPath = path.join(projectDir, "apps/server", ".env");

	const variables: EnvVariable[] = [
		{
			key: "CLOUDFLARE_ACCOUNT_ID",
			value: "",
			condition: true,
		},
		{
			key: "CLOUDFLARE_DATABASE_ID",
			value: "",
			condition: true,
		},
		{
			key: "CLOUDFLARE_D1_TOKEN",
			value: "",
			condition: true,
		},
	];

	try {
		await addEnvVariablesToFile(envPath, variables);
-	} catch (_err) {
-		// ignore
+	} catch (error) {
+		// Log warning but don't fail the setup process
+		console.warn("Warning: Failed to setup D1 environment variables:", error instanceof Error ? error.message : "Unknown error");
	}
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 846d705 and e8c6e99.

📒 Files selected for processing (10)
  • apps/cli/src/helpers/database-providers/d1-setup.ts (1 hunks)
  • apps/cli/src/helpers/project-generation/env-setup.ts (1 hunks)
  • apps/cli/src/helpers/setup/db-setup.ts (2 hunks)
  • apps/cli/src/prompts/config-prompts.ts (1 hunks)
  • apps/cli/src/prompts/database-setup.ts (2 hunks)
  • apps/cli/src/types.ts (1 hunks)
  • apps/cli/src/validation.ts (1 hunks)
  • apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs (1 hunks)
  • apps/cli/templates/db/drizzle/sqlite/src/db/index.ts.hbs (2 hunks)
  • apps/cli/templates/runtime/workers/apps/server/wrangler.jsonc.hbs (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
apps/cli/src/helpers/setup/db-setup.ts (1)
apps/cli/src/helpers/database-providers/d1-setup.ts (1)
  • setupCloudflareD1 (8-36)
apps/cli/src/prompts/database-setup.ts (1)
apps/cli/src/types.ts (4)
  • DatabaseSetup (67-67)
  • ORM (11-11)
  • Backend (16-16)
  • Runtime (23-23)
apps/cli/src/helpers/database-providers/d1-setup.ts (2)
apps/cli/src/types.ts (1)
  • ProjectConfig (117-134)
apps/cli/src/helpers/project-generation/env-setup.ts (2)
  • EnvVariable (6-10)
  • addEnvVariablesToFile (12-85)
🔇 Additional comments (15)
apps/cli/src/helpers/database-providers/d1-setup.ts (1)

1-6: LGTM! Clean imports and type usage.

The imports are well-organized and properly typed, following the established patterns in the codebase.

apps/cli/src/helpers/project-generation/env-setup.ts (1)

189-190: LGTM! Correctly categorizes D1 as a specialized setup.

Adding dbSetup === "d1" to the specializedSetup condition is correct since Cloudflare D1 doesn't use a generic DATABASE_URL and has its own dedicated environment variables. This follows the same pattern as other cloud database providers like Supabase and Turso.

apps/cli/src/validation.ts (1)

361-376: LGTM! Proper validation rules for D1 setup.

The validation logic correctly enforces the D1 constraints:

  • Requires SQLite database (since D1 is SQLite-compatible)
  • Requires Workers runtime (D1 only works with Cloudflare Workers)

The error messages are clear and helpful, and the implementation follows the established pattern of other database setup validations in this file.

apps/cli/src/prompts/config-prompts.ts (1)

88-88: LGTM! Enables conditional D1 prompting based on runtime.

Adding results.runtime as a parameter to getDBSetupChoice is necessary to enable conditional display of the D1 option when the runtime is "workers". This follows the established pattern of passing contextual information to prompt functions.

apps/cli/src/types.ts (1)

63-63: LGTM! Adds D1 as a valid database setup option.

Adding "d1" to the DatabaseSetupSchema enum properly defines the new Cloudflare D1 option as a valid database setup choice, enabling type safety throughout the codebase.

apps/cli/src/helpers/setup/db-setup.ts (2)

8-8: LGTM: Import statement correctly added.

The import for setupCloudflareD1 follows the existing pattern and is properly placed alphabetically.


73-74: LGTM: Conditional logic properly implemented.

The D1 setup follows the exact same pattern as the existing Turso setup, with proper conditional checks for both database type and setup option.

apps/cli/src/prompts/database-setup.ts (2)

3-3: LGTM: Function signature properly updated.

The addition of the optional runtime parameter maintains backward compatibility while enabling the new D1 functionality.

Also applies to: 10-10


36-44: LGTM: Conditional D1 option correctly implemented.

The spread operator pattern correctly includes the D1 option only when runtime is "workers", and the option configuration provides clear labeling and helpful hints.

apps/cli/templates/db/drizzle/sqlite/src/db/index.ts.hbs (2)

16-21: LGTM: D1-specific database initialization correctly implemented.

The D1 setup uses the appropriate drizzle-orm/d1 import and directly initializes with env.DB, which is the correct approach for Cloudflare D1 bindings.


21-34: LGTM: Fallback logic properly maintained.

The else block correctly maintains the existing libsql logic for non-D1 setups under the workers runtime, ensuring backward compatibility.

apps/cli/templates/db/drizzle/sqlite/drizzle.config.ts.hbs (2)

6-14: LGTM: D1 configuration correctly implemented.

The D1 configuration uses the appropriate sqlite dialect with d1-http driver and references the correct Cloudflare environment variables that are set up by the setupCloudflareD1 function.


15-23: LGTM: Fallback configuration properly maintained.

The else block correctly maintains the existing Turso configuration, ensuring compatibility with non-D1 SQLite setups.

apps/cli/templates/runtime/workers/apps/server/wrangler.jsonc.hbs (2)

8-13: LGTM: Improved environment variable guidance.

The updated comments provide clear distinction between public environment variables and secrets, with proper guidance on using wrangler secret put for sensitive data.


15-33: LGTM: Comprehensive D1 configuration with excellent documentation.

The D1 database configuration is well-structured with:

  • Clear setup instructions for users
  • Proper binding configuration
  • Migration directory setup
  • Preview database configuration for local development

The step-by-step comments make it easy for users to get D1 up and running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants