-
-
Notifications
You must be signed in to change notification settings - Fork 242
Add D1 Database #335
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
Add D1 Database #335
Conversation
🦋 Changeset detectedLatest commit: 81cb28d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
WalkthroughSupport 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
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
Poem
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
7937bda to
e8c6e99
Compare
commit: |
There was a problem hiding this 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:
- Silent error handling: The try-catch block silently ignores all errors, which might hide important setup failures from users.
- Path assumptions: The function assumes the
apps/serverdirectory 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
📒 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 thespecializedSetupcondition is correct since Cloudflare D1 doesn't use a genericDATABASE_URLand 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.runtimeas a parameter togetDBSetupChoiceis 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 theDatabaseSetupSchemaenum 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
setupCloudflareD1follows 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
runtimeparameter 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/d1import and directly initializes withenv.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
sqlitedialect withd1-httpdriver and references the correct Cloudflare environment variables that are set up by thesetupCloudflareD1function.
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 putfor 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.
Summary by CodeRabbit