From b6f33a2bf1cd91d995e35f3fcb3bb664e3ee1571 Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Wed, 27 Aug 2025 20:24:49 -0400 Subject: [PATCH 01/10] feat: `--env` functional --- create-db/index.js | 62 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/create-db/index.js b/create-db/index.js index 32a1a7f..9b9f68c 100755 --- a/create-db/index.js +++ b/create-db/index.js @@ -194,6 +194,7 @@ Options: ${chalk.yellow("--json, -j")} Output machine-readable JSON and exit ${chalk.yellow("--list-regions")} List available regions and exit ${chalk.yellow("--help, -h")} Show this help message + ${chalk.yellow("--env, -e")} Outputs DATABASE_URL to the terminal Examples: ${chalk.gray(`npx ${CLI_NAME} --region us-east-1`)} @@ -215,12 +216,14 @@ async function parseArgs() { "list-regions", "interactive", "json", + "env", ]; const shorthandMap = { r: "region", i: "interactive", h: "help", j: "json", + e: "env", }; const exitWithError = (message) => { @@ -293,6 +296,28 @@ async function parseArgs() { return { flags }; } +function validateFlagCombinations(flags) { + const conflictingFlags = [ + ["env", "json"], + ["list-regions", "env"], + ["list-regions", "json"], + ["list-regions", "interactive"], + ["list-regions", "region"], + ]; + + for (const [flag1, flag2] of conflictingFlags) { + if (flags[flag1] && flags[flag2]) { + console.error( + chalk.red.bold( + `\nāœ– Error: Cannot use --${flag1} and --${flag2} together.\n` + ) + ); + console.error(chalk.gray("Use --help or -h to see available options.\n")); + process.exit(1); + } + } +} + export async function getRegions(returnJson = false) { const url = `${CREATE_DB_WORKER_URL}/regions`; const res = await fetch(url); @@ -389,9 +414,9 @@ async function promptForRegion(defaultRegion, userAgent) { return region; } -async function createDatabase(name, region, userAgent, returnJson = false) { +async function createDatabase(name, region, userAgent, silent = false) { let s; - if (!returnJson) { + if (!silent) { s = spinner(); s.start("Creating your database..."); } @@ -407,7 +432,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) { }); if (resp.status === 429) { - if (returnJson) { + if (silent) { return { error: "rate_limit_exceeded", message: @@ -439,7 +464,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) { raw = await resp.text(); result = JSON.parse(raw); } catch (e) { - if (returnJson) { + if (silent) { return { error: "invalid_json", message: "Unexpected response from create service.", @@ -488,7 +513,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) { const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${userAgent}&utm_medium=cli`; const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000); - if (returnJson && !result.error) { + if (silent && !result.error) { const jsonResponse = { connectionString: prismaConn, directConnectionString: directConn, @@ -507,7 +532,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) { } if (result.error) { - if (returnJson) { + if (silent) { return { error: "api_error", message: result.error.message || "Unknown error", @@ -593,6 +618,8 @@ async function main() { const { flags } = await parseArgs(); + validateFlagCombinations(flags); + let userAgent; const userEnvVars = readUserEnvFile(); if (userEnvVars.PRISMA_ACTOR_NAME && userEnvVars.PRISMA_ACTOR_PROJECT) { @@ -670,6 +697,29 @@ async function main() { } } + if (flags.env) { + try { + if (chooseRegionPrompt) { + region = await promptForRegion(region, userAgent); + } else { + await validateRegion(region, true); + } + const result = await createDatabase(name, region, userAgent, true); + if (result.error) { + console.error(result.message || "Unknown error"); + process.exit(1); + } + process.stdout.write(`DATABASE_URL="${result.directConnectionString}"`); + process.stderr.write( + "\n\n# Claim your database at: " + result.claimUrl + ); + process.exit(0); + } catch (e) { + console.error(e?.message || String(e)); + process.exit(1); + } + } + intro(chalk.cyan.bold("šŸš€ Creating a Prisma Postgres database")); log.message( chalk.white(`Provisioning a temporary database in ${region}...`) From 0a00cd1eeb2e0b7335ac8a766b95e20da050a8cd Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Wed, 27 Aug 2025 20:25:37 -0400 Subject: [PATCH 02/10] chore: version bump --- create-db/package.json | 2 +- create-pg/package.json | 2 +- create-postgres/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/create-db/package.json b/create-db/package.json index 9028b25..4287ea3 100644 --- a/create-db/package.json +++ b/create-db/package.json @@ -1,6 +1,6 @@ { "name": "create-db", - "version": "1.0.6", + "version": "1.0.7", "description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.", "main": "index.js", "author": "", diff --git a/create-pg/package.json b/create-pg/package.json index 3749029..7476a61 100644 --- a/create-pg/package.json +++ b/create-pg/package.json @@ -1,6 +1,6 @@ { "name": "create-pg", - "version": "1.0.6", + "version": "1.0.7", "description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.", "main": "index.js", "author": "", diff --git a/create-postgres/package.json b/create-postgres/package.json index f1d12f8..5577dd3 100644 --- a/create-postgres/package.json +++ b/create-postgres/package.json @@ -1,6 +1,6 @@ { "name": "create-postgres", - "version": "1.0.6", + "version": "1.0.7", "description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.", "main": "index.js", "author": "", From 7d0f22f60f6282f52ad74c466d062f9c82e55a94 Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Wed, 27 Aug 2025 20:40:12 -0400 Subject: [PATCH 03/10] fix: add more incompatible flags --- create-db/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/create-db/index.js b/create-db/index.js index 9b9f68c..5a0a1bd 100755 --- a/create-db/index.js +++ b/create-db/index.js @@ -303,6 +303,8 @@ function validateFlagCombinations(flags) { ["list-regions", "json"], ["list-regions", "interactive"], ["list-regions", "region"], + ["interactive", "env"], + ["interactive", "json"], ]; for (const [flag1, flag2] of conflictingFlags) { From 7edd860809caf9526409cabe14672ff18da8522e Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Wed, 27 Aug 2025 21:50:15 -0400 Subject: [PATCH 04/10] fix: code rabbit changes --- create-db/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/create-db/index.js b/create-db/index.js index 5a0a1bd..d9f301d 100755 --- a/create-db/index.js +++ b/create-db/index.js @@ -194,7 +194,7 @@ Options: ${chalk.yellow("--json, -j")} Output machine-readable JSON and exit ${chalk.yellow("--list-regions")} List available regions and exit ${chalk.yellow("--help, -h")} Show this help message - ${chalk.yellow("--env, -e")} Outputs DATABASE_URL to the terminal + ${chalk.yellow("--env, -e")} Prints DATABASE_URL to the terminal Examples: ${chalk.gray(`npx ${CLI_NAME} --region us-east-1`)} @@ -202,6 +202,7 @@ Examples: ${chalk.gray(`npx ${CLI_NAME} --interactive`)} ${chalk.gray(`npx ${CLI_NAME} -i`)} ${chalk.gray(`npx ${CLI_NAME} --json --region us-east-1`)} + ${chalk.gray(`npx ${CLI_NAME} --env --region us-east-1`)} `); process.exit(0); } @@ -539,6 +540,7 @@ async function createDatabase(name, region, userAgent, silent = false) { error: "api_error", message: result.error.message || "Unknown error", details: result.error, + status: result.error.status, }; } @@ -637,6 +639,7 @@ async function main() { "has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"), "has-list-regions-flag": rawArgs.includes("--list-regions"), "has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"), + "has-env-flag": rawArgs.includes("--env") || rawArgs.includes("-e"), "has-user-agent-from-env": !!userAgent, "node-version": process.version, platform: process.platform, From 8cabcd89832403850b1b77437a61af8651eb1bfe Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Thu, 28 Aug 2025 06:48:48 -0400 Subject: [PATCH 05/10] fix: code rabbit changes --- create-db/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/create-db/index.js b/create-db/index.js index d9f301d..3a69295 100755 --- a/create-db/index.js +++ b/create-db/index.js @@ -540,7 +540,7 @@ async function createDatabase(name, region, userAgent, silent = false) { error: "api_error", message: result.error.message || "Unknown error", details: result.error, - status: result.error.status, + status: result.error.status ?? resp.status, }; } @@ -652,8 +652,11 @@ async function main() { } let name = new Date().toISOString(); - let userLocation = await detectUserLocation(); - let region = getRegionClosestToLocation(userLocation) || "us-east-1"; + let region = flags.region || "us-east-1"; + if (!flags.region || !flags.interactive) { + const userLocation = await detectUserLocation(); + region = getRegionClosestToLocation(userLocation) || region; + } let chooseRegionPrompt = false; if (flags.help) { From a7e9b5f9ab599514f99a0c0b1e70f0e6499751ba Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Thu, 28 Aug 2025 06:59:20 -0400 Subject: [PATCH 06/10] fix: text cuttoff due to stderr --- create-db/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/create-db/index.js b/create-db/index.js index 3a69295..feb1113 100755 --- a/create-db/index.js +++ b/create-db/index.js @@ -513,7 +513,7 @@ async function createDatabase(name, region, userAgent, silent = false) { ? `postgresql://${directUser}:${directPass}@${directHost}${directPort}/${directDbName}?sslmode=require` : null; - const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${userAgent}&utm_medium=cli`; + const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${userAgent || CLI_NAME}&utm_medium=cli`; const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000); if (silent && !result.error) { @@ -717,10 +717,8 @@ async function main() { console.error(result.message || "Unknown error"); process.exit(1); } - process.stdout.write(`DATABASE_URL="${result.directConnectionString}"`); - process.stderr.write( - "\n\n# Claim your database at: " + result.claimUrl - ); + console.log(`DATABASE_URL="${result.directConnectionString}"`); + console.error("\n# Claim your database at: " + result.claimUrl); process.exit(0); } catch (e) { console.error(e?.message || String(e)); From a1e6a4c52de292e2664747778748610dc226c468 Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Thu, 28 Aug 2025 09:56:18 -0400 Subject: [PATCH 07/10] feat: available flags added to readme --- README.md | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c23fc72..bc93f08 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,41 @@ This monorepo contains tools and services that enable developers to quickly prov 2. **Cloudflare Workers** - Backend services for database management and OAuth authentication 3. **Monorepo Infrastructure** - Shared tooling and versioning +## CLI Reference + +### Available Flags + +| Flag | Description | Example | +| ---------------- | ----------------------------------- | -------------------- | +| `--region` | Specify database region | `--region us-east-1` | +| `--list-regions` | List available regions | `--list-regions` | +| `--interactive` | Enable interactive region selection | `--interactive` | +| `--help` | Show help information | `--help` | +| `--json` | Output the info in a JSON format | `--json` | + +### Examples + +```bash +# Create database with specific region +npx create-db --region eu-west-1 + +# List available regions +npx create-db --list-regions + +# Interactive mode +npx create-db --interactive + +# Output in JSON format +npx create-db --json + +# Show help +npx create-db --help + +# Alternative command shorthand names work the same way +npx create-pg -r us-east-1 +npx create-pg -j +``` + ## Packages ### CLI Tools @@ -107,7 +142,7 @@ CLAIM_DB_WORKER_URL="http://127.0.0.1:9999" ### Installation -```bash +````bash # Clone the repository git clone https://github.com/prisma/create-db.git cd create-db @@ -128,7 +163,7 @@ cd ../create-db && pnpm install ```env INTEGRATION_TOKEN=your_prisma_integration_token -``` +```` **Claim DB Worker** (`claim-db-worker/.dev.vars`): From 327abc29f55846e85975c629fbc1c45d75b6d9cc Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Thu, 28 Aug 2025 10:12:13 -0400 Subject: [PATCH 08/10] chore: coderabbit --- README.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index bc93f08..cf7246e 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,14 @@ This monorepo contains tools and services that enable developers to quickly prov ### Available Flags -| Flag | Description | Example | -| ---------------- | ----------------------------------- | -------------------- | -| `--region` | Specify database region | `--region us-east-1` | -| `--list-regions` | List available regions | `--list-regions` | -| `--interactive` | Enable interactive region selection | `--interactive` | -| `--help` | Show help information | `--help` | -| `--json` | Output the info in a JSON format | `--json` | +| Flag | Description | Example | +| ---------------- | ------------------------------------------------- | -------------------- | +| `--region` | Specify database region | `--region us-east-1` | +| `--list-regions` | List available regions | `--list-regions` | +| `--interactive` | Enable interactive region selection | `--interactive` | +| `--help` | Show help information | `--help` | +| `--json` | Output the info in a JSON format | `--json` | +| `--env`, `-e` | Print DATABASE_URL to stdout; claim URL to stderr | `--env` | ### Examples @@ -40,6 +41,10 @@ npx create-db --json # Show help npx create-db --help +# Get --env response into .env +npx create-db --env > .env # Only DATABASE_URL +npx create-db --env > .env 2>&1 # Both DATABASE_URL and Claim URL + # Alternative command shorthand names work the same way npx create-pg -r us-east-1 npx create-pg -j From 69ffe5bf93556ddbdf23304f27d2d3ab059f11ce Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Thu, 28 Aug 2025 10:20:36 -0400 Subject: [PATCH 09/10] fix: minor readme syntax error --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cf7246e..0fe873d 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ CLAIM_DB_WORKER_URL="http://127.0.0.1:9999" ### Installation -````bash +```bash # Clone the repository git clone https://github.com/prisma/create-db.git cd create-db @@ -159,6 +159,7 @@ pnpm install cd create-db-worker && pnpm install cd ../claim-db-worker && pnpm install cd ../create-db && pnpm install +``` ### Local Development @@ -168,7 +169,7 @@ cd ../create-db && pnpm install ```env INTEGRATION_TOKEN=your_prisma_integration_token -```` +``` **Claim DB Worker** (`claim-db-worker/.dev.vars`): From db27abb24cf630b2c1f550b4b121e6f877ae1723 Mon Sep 17 00:00:00 2001 From: Aidan McAlister Date: Thu, 28 Aug 2025 11:44:45 -0400 Subject: [PATCH 10/10] fix: updated to suggest `>>` instead of `>` --- README.md | 6 +++--- create-db/index.js | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0fe873d..51c4730 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,9 @@ npx create-db --json # Show help npx create-db --help -# Get --env response into .env -npx create-db --env > .env # Only DATABASE_URL -npx create-db --env > .env 2>&1 # Both DATABASE_URL and Claim URL +# Get --env response into .env (Be careful to not use `> .env` as that will overwrite your .env) +npx create-db --env >> .env # Only DATABASE_URL +npx create-db --env >> .env 2>&1 # Both DATABASE_URL and Claim URL # Alternative command shorthand names work the same way npx create-pg -r us-east-1 diff --git a/create-db/index.js b/create-db/index.js index feb1113..6998627 100755 --- a/create-db/index.js +++ b/create-db/index.js @@ -194,7 +194,7 @@ Options: ${chalk.yellow("--json, -j")} Output machine-readable JSON and exit ${chalk.yellow("--list-regions")} List available regions and exit ${chalk.yellow("--help, -h")} Show this help message - ${chalk.yellow("--env, -e")} Prints DATABASE_URL to the terminal + ${chalk.yellow("--env, -e")} Prints DATABASE_URL to the terminal. ${chalk.gray(`To write to .env, use --env >> .env`)} Examples: ${chalk.gray(`npx ${CLI_NAME} --region us-east-1`)} @@ -203,6 +203,7 @@ Examples: ${chalk.gray(`npx ${CLI_NAME} -i`)} ${chalk.gray(`npx ${CLI_NAME} --json --region us-east-1`)} ${chalk.gray(`npx ${CLI_NAME} --env --region us-east-1`)} + ${chalk.gray(`npx ${CLI_NAME} --env >> .env`)} `); process.exit(0); }