create-db 1.0.7-pr42-claim-worker-migrate-nextjs-17300957361.0 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +66 -9
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -194,6 +194,7 @@ Options:
194
194
  ${chalk.yellow("--json, -j")} Output machine-readable JSON and exit
195
195
  ${chalk.yellow("--list-regions")} List available regions and exit
196
196
  ${chalk.yellow("--help, -h")} Show this help message
197
+ ${chalk.yellow("--env, -e")} Prints DATABASE_URL to the terminal. ${chalk.gray(`To write to .env, use --env >> .env`)}
197
198
 
198
199
  Examples:
199
200
  ${chalk.gray(`npx ${CLI_NAME} --region us-east-1`)}
@@ -201,6 +202,8 @@ Examples:
201
202
  ${chalk.gray(`npx ${CLI_NAME} --interactive`)}
202
203
  ${chalk.gray(`npx ${CLI_NAME} -i`)}
203
204
  ${chalk.gray(`npx ${CLI_NAME} --json --region us-east-1`)}
205
+ ${chalk.gray(`npx ${CLI_NAME} --env --region us-east-1`)}
206
+ ${chalk.gray(`npx ${CLI_NAME} --env >> .env`)}
204
207
  `);
205
208
  process.exit(0);
206
209
  }
@@ -215,12 +218,14 @@ async function parseArgs() {
215
218
  "list-regions",
216
219
  "interactive",
217
220
  "json",
221
+ "env",
218
222
  ];
219
223
  const shorthandMap = {
220
224
  r: "region",
221
225
  i: "interactive",
222
226
  h: "help",
223
227
  j: "json",
228
+ e: "env",
224
229
  };
225
230
 
226
231
  const exitWithError = (message) => {
@@ -293,6 +298,30 @@ async function parseArgs() {
293
298
  return { flags };
294
299
  }
295
300
 
301
+ function validateFlagCombinations(flags) {
302
+ const conflictingFlags = [
303
+ ["env", "json"],
304
+ ["list-regions", "env"],
305
+ ["list-regions", "json"],
306
+ ["list-regions", "interactive"],
307
+ ["list-regions", "region"],
308
+ ["interactive", "env"],
309
+ ["interactive", "json"],
310
+ ];
311
+
312
+ for (const [flag1, flag2] of conflictingFlags) {
313
+ if (flags[flag1] && flags[flag2]) {
314
+ console.error(
315
+ chalk.red.bold(
316
+ `\nāœ– Error: Cannot use --${flag1} and --${flag2} together.\n`
317
+ )
318
+ );
319
+ console.error(chalk.gray("Use --help or -h to see available options.\n"));
320
+ process.exit(1);
321
+ }
322
+ }
323
+ }
324
+
296
325
  export async function getRegions(returnJson = false) {
297
326
  const url = `${CREATE_DB_WORKER_URL}/regions`;
298
327
  const res = await fetch(url);
@@ -389,9 +418,9 @@ async function promptForRegion(defaultRegion, userAgent) {
389
418
  return region;
390
419
  }
391
420
 
392
- async function createDatabase(name, region, userAgent, returnJson = false) {
421
+ async function createDatabase(name, region, userAgent, silent = false) {
393
422
  let s;
394
- if (!returnJson) {
423
+ if (!silent) {
395
424
  s = spinner();
396
425
  s.start("Creating your database...");
397
426
  }
@@ -407,7 +436,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
407
436
  });
408
437
 
409
438
  if (resp.status === 429) {
410
- if (returnJson) {
439
+ if (silent) {
411
440
  return {
412
441
  error: "rate_limit_exceeded",
413
442
  message:
@@ -439,7 +468,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
439
468
  raw = await resp.text();
440
469
  result = JSON.parse(raw);
441
470
  } catch (e) {
442
- if (returnJson) {
471
+ if (silent) {
443
472
  return {
444
473
  error: "invalid_json",
445
474
  message: "Unexpected response from create service.",
@@ -485,10 +514,10 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
485
514
  ? `postgresql://${directUser}:${directPass}@${directHost}${directPort}/${directDbName}?sslmode=require`
486
515
  : null;
487
516
 
488
- const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${userAgent}&utm_medium=cli`;
517
+ const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${userAgent || CLI_NAME}&utm_medium=cli`;
489
518
  const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
490
519
 
491
- if (returnJson && !result.error) {
520
+ if (silent && !result.error) {
492
521
  const jsonResponse = {
493
522
  connectionString: prismaConn,
494
523
  directConnectionString: directConn,
@@ -507,11 +536,12 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
507
536
  }
508
537
 
509
538
  if (result.error) {
510
- if (returnJson) {
539
+ if (silent) {
511
540
  return {
512
541
  error: "api_error",
513
542
  message: result.error.message || "Unknown error",
514
543
  details: result.error,
544
+ status: result.error.status ?? resp.status,
515
545
  };
516
546
  }
517
547
 
@@ -593,6 +623,8 @@ async function main() {
593
623
 
594
624
  const { flags } = await parseArgs();
595
625
 
626
+ validateFlagCombinations(flags);
627
+
596
628
  let userAgent;
597
629
  const userEnvVars = readUserEnvFile();
598
630
  if (userEnvVars.PRISMA_ACTOR_NAME && userEnvVars.PRISMA_ACTOR_PROJECT) {
@@ -608,6 +640,7 @@ async function main() {
608
640
  "has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
609
641
  "has-list-regions-flag": rawArgs.includes("--list-regions"),
610
642
  "has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
643
+ "has-env-flag": rawArgs.includes("--env") || rawArgs.includes("-e"),
611
644
  "has-user-agent-from-env": !!userAgent,
612
645
  "node-version": process.version,
613
646
  platform: process.platform,
@@ -620,8 +653,11 @@ async function main() {
620
653
  }
621
654
 
622
655
  let name = new Date().toISOString();
623
- let userLocation = await detectUserLocation();
624
- let region = getRegionClosestToLocation(userLocation) || "us-east-1";
656
+ let region = flags.region || "us-east-1";
657
+ if (!flags.region || !flags.interactive) {
658
+ const userLocation = await detectUserLocation();
659
+ region = getRegionClosestToLocation(userLocation) || region;
660
+ }
625
661
  let chooseRegionPrompt = false;
626
662
 
627
663
  if (flags.help) {
@@ -670,6 +706,27 @@ async function main() {
670
706
  }
671
707
  }
672
708
 
709
+ if (flags.env) {
710
+ try {
711
+ if (chooseRegionPrompt) {
712
+ region = await promptForRegion(region, userAgent);
713
+ } else {
714
+ await validateRegion(region, true);
715
+ }
716
+ const result = await createDatabase(name, region, userAgent, true);
717
+ if (result.error) {
718
+ console.error(result.message || "Unknown error");
719
+ process.exit(1);
720
+ }
721
+ console.log(`DATABASE_URL="${result.directConnectionString}"`);
722
+ console.error("\n# Claim your database at: " + result.claimUrl);
723
+ process.exit(0);
724
+ } catch (e) {
725
+ console.error(e?.message || String(e));
726
+ process.exit(1);
727
+ }
728
+ }
729
+
673
730
  intro(chalk.cyan.bold("šŸš€ Creating a Prisma Postgres database"));
674
731
  log.message(
675
732
  chalk.white(`Provisioning a temporary database in ${region}...`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "1.0.7-pr42-claim-worker-migrate-nextjs-17300957361.0",
3
+ "version": "1.0.7",
4
4
  "description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
5
5
  "main": "index.js",
6
6
  "author": "",