create-db 1.0.1-pr43-DC-4828-json-flag-17106021047.0 → 1.0.1-pr43-DC-4828-json-flag-17106316584.0

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 +51 -9
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -315,7 +315,33 @@ async function createDatabase(name, region, returnJson = false) {
315
315
  process.exit(1);
316
316
  }
317
317
 
318
- const result = await resp.json();
318
+ let result;
319
+ let raw;
320
+ try {
321
+ raw = await resp.text();
322
+ result = JSON.parse(raw);
323
+ } catch (e) {
324
+ if (returnJson) {
325
+ return {
326
+ error: "invalid_json",
327
+ message: "Unexpected response from create service.",
328
+ raw,
329
+ status: resp.status,
330
+ };
331
+ }
332
+ if (s) {
333
+ s.stop("Unexpected response from create service.");
334
+ }
335
+ try {
336
+ await analytics.capture("create_db:database_creation_failed", {
337
+ command: CLI_NAME,
338
+ region,
339
+ "error-type": "invalid_json",
340
+ "status-code": resp.status,
341
+ });
342
+ } catch {}
343
+ process.exit(1);
344
+ }
319
345
 
320
346
  const database = result.data ? result.data.database : result.databases?.[0];
321
347
  const projectId = result.data ? result.data.id : result.id;
@@ -331,9 +357,13 @@ async function createDatabase(name, region, returnJson = false) {
331
357
  ? encodeURIComponent(directConnDetails.pass)
332
358
  : "";
333
359
  const directHost = directConnDetails?.host;
360
+ const directPort = directConnDetails?.port
361
+ ? `:${directConnDetails.port}`
362
+ : "";
363
+ const directDbName = directConnDetails?.database || "postgres";
334
364
  const directConn =
335
365
  directConnDetails && directHost
336
- ? `postgresql://${directUser}:${directPass}@${directHost}/postgres`
366
+ ? `postgresql://${directUser}:${directPass}@${directHost}${directPort}/${directDbName}`
337
367
  : null;
338
368
 
339
369
  const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${CLI_NAME}&utm_medium=cli`;
@@ -448,7 +478,7 @@ async function main() {
448
478
 
449
479
  const { flags } = await parseArgs();
450
480
 
451
- if (!flags.help) {
481
+ if (!flags.help && !flags.json) {
452
482
  await isOffline();
453
483
  }
454
484
 
@@ -482,13 +512,25 @@ async function main() {
482
512
  }
483
513
 
484
514
  if (flags.json) {
485
- if (chooseRegionPrompt) {
486
- region = await promptForRegion(region);
515
+ try {
516
+ if (chooseRegionPrompt) {
517
+ region = await promptForRegion(region);
518
+ } else {
519
+ await validateRegion(region, true);
520
+ }
521
+ const result = await createDatabase(name, region, true);
522
+ console.log(JSON.stringify(result, null, 2));
523
+ process.exit(0);
524
+ } catch (e) {
525
+ console.log(
526
+ JSON.stringify(
527
+ { error: "cli_error", message: e?.message || String(e) },
528
+ null,
529
+ 2
530
+ )
531
+ );
532
+ process.exit(1);
487
533
  }
488
-
489
- const result = await createDatabase(name, region, true);
490
- console.log(JSON.stringify(result, null, 2));
491
- process.exit(0);
492
534
  }
493
535
 
494
536
  intro(chalk.cyan.bold("🚀 Creating a Prisma Postgres database"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "1.0.1-pr43-DC-4828-json-flag-17106021047.0",
3
+ "version": "1.0.1-pr43-DC-4828-json-flag-17106316584.0",
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": "",