create-db 1.0.10-pr62-posthog-and-minor-fixes-18784513395.0 → 1.0.10-pr62-posthog-and-minor-fixes-18785189855.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 +69 -34
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -10,31 +10,49 @@ import chalk from "chalk";
10
10
 
11
11
  dotenv.config();
12
12
 
13
- const CLI_RUN_ID = randomUUID();
14
-
15
13
  const CREATE_DB_WORKER_URL =
16
14
  process.env.CREATE_DB_WORKER_URL || "https://create-db-temp.prisma.io";
17
15
  const CLAIM_DB_WORKER_URL =
18
16
  process.env.CLAIM_DB_WORKER_URL || "https://create-db.prisma.io";
19
17
 
20
- async function sendAnalyticsToWorker(eventName, properties) {
18
+ // Track pending analytics promises to ensure they complete before exit
19
+ const pendingAnalytics = [];
20
+
21
+ async function sendAnalyticsToWorker(eventName, properties, cliRunId) {
21
22
  const controller = new AbortController();
22
- const timer = setTimeout(() => controller.abort(), 2000);
23
- try {
24
- const payload = {
25
- eventName,
26
- properties: { distinct_id: CLI_RUN_ID, ...(properties || {}) },
27
- };
28
- await fetch(`${CREATE_DB_WORKER_URL}/analytics`, {
29
- method: "POST",
30
- headers: { "Content-Type": "application/json" },
31
- body: JSON.stringify(payload),
32
- signal: controller.signal,
33
- });
34
- } catch (error) {
35
- } finally {
36
- clearTimeout(timer);
37
- }
23
+ const timer = setTimeout(() => controller.abort(), 5000);
24
+
25
+ const analyticsPromise = (async () => {
26
+ try {
27
+ const payload = {
28
+ eventName,
29
+ properties: { distinct_id: cliRunId, ...(properties || {}) },
30
+ };
31
+ await fetch(`${CREATE_DB_WORKER_URL}/analytics`, {
32
+ method: "POST",
33
+ headers: { "Content-Type": "application/json" },
34
+ body: JSON.stringify(payload),
35
+ signal: controller.signal,
36
+ });
37
+ } catch (error) {
38
+ // Silently fail - analytics shouldn't block CLI
39
+ } finally {
40
+ clearTimeout(timer);
41
+ }
42
+ })();
43
+
44
+ pendingAnalytics.push(analyticsPromise);
45
+ return analyticsPromise;
46
+ }
47
+
48
+ // Wait for all pending analytics with a timeout
49
+ async function flushAnalytics(maxWaitMs = 500) {
50
+ if (pendingAnalytics.length === 0) return;
51
+
52
+ const timeout = new Promise((resolve) => setTimeout(resolve, maxWaitMs));
53
+ const allAnalytics = Promise.all(pendingAnalytics);
54
+
55
+ await Promise.race([allAnalytics, timeout]);
38
56
  }
39
57
 
40
58
  async function detectUserLocation() {
@@ -135,6 +153,7 @@ async function isOffline() {
135
153
  `Check your internet connection or visit ${chalk.green("https://www.prisma-status.com/\n")}`
136
154
  )
137
155
  );
156
+ await flushAnalytics();
138
157
  process.exit(1);
139
158
  }
140
159
  }
@@ -205,6 +224,7 @@ Examples:
205
224
  ${chalk.gray(`npx ${CLI_NAME} --env --region us-east-1`)}
206
225
  ${chalk.gray(`npx ${CLI_NAME} --env >> .env`)}
207
226
  `);
227
+ await flushAnalytics();
208
228
  process.exit(0);
209
229
  }
210
230
 
@@ -384,7 +404,7 @@ function handleError(message, extra = "") {
384
404
  process.exit(1);
385
405
  }
386
406
 
387
- async function promptForRegion(defaultRegion, userAgent) {
407
+ async function promptForRegion(defaultRegion, userAgent, cliRunId) {
388
408
  let regions;
389
409
  try {
390
410
  regions = await getRegions();
@@ -405,6 +425,7 @@ async function promptForRegion(defaultRegion, userAgent) {
405
425
 
406
426
  if (region === null) {
407
427
  cancel(chalk.red("Operation cancelled."));
428
+ await flushAnalytics();
408
429
  process.exit(0);
409
430
  }
410
431
 
@@ -413,12 +434,12 @@ async function promptForRegion(defaultRegion, userAgent) {
413
434
  region: region,
414
435
  "selection-method": "interactive",
415
436
  "user-agent": userAgent,
416
- });
437
+ }, cliRunId);
417
438
 
418
439
  return region;
419
440
  }
420
441
 
421
- async function createDatabase(name, region, userAgent, silent = false) {
442
+ async function createDatabase(name, region, userAgent, cliRunId, silent = false) {
422
443
  let s;
423
444
  if (!silent) {
424
445
  s = spinner();
@@ -458,8 +479,9 @@ async function createDatabase(name, region, userAgent, silent = false) {
458
479
  "error-type": "rate_limit",
459
480
  "status-code": 429,
460
481
  "user-agent": userAgent,
461
- });
482
+ }, cliRunId);
462
483
 
484
+ await flushAnalytics();
463
485
  process.exit(1);
464
486
  }
465
487
 
@@ -487,8 +509,9 @@ async function createDatabase(name, region, userAgent, silent = false) {
487
509
  "error-type": "invalid_json",
488
510
  "status-code": resp.status,
489
511
  "user-agent": userAgent,
490
- });
512
+ }, cliRunId);
491
513
 
514
+ await flushAnalytics();
492
515
  process.exit(1);
493
516
  }
494
517
 
@@ -558,8 +581,9 @@ async function createDatabase(name, region, userAgent, silent = false) {
558
581
  "error-type": "api_error",
559
582
  "error-message": result.error.message,
560
583
  "user-agent": userAgent,
561
- });
584
+ }, cliRunId);
562
585
 
586
+ await flushAnalytics();
563
587
  process.exit(1);
564
588
  }
565
589
 
@@ -615,11 +639,14 @@ async function createDatabase(name, region, userAgent, silent = false) {
615
639
  command: CLI_NAME,
616
640
  region,
617
641
  utm_source: CLI_NAME,
618
- });
642
+ }, cliRunId);
619
643
  }
620
644
 
621
645
  export async function main() {
622
646
  try {
647
+ // Generate unique ID for this CLI run
648
+ const cliRunId = randomUUID();
649
+
623
650
  const rawArgs = process.argv.slice(2);
624
651
 
625
652
  const { flags } = await parseArgs();
@@ -647,7 +674,7 @@ export async function main() {
647
674
  platform: process.platform,
648
675
  arch: process.arch,
649
676
  "user-agent": userAgent,
650
- });
677
+ }, cliRunId);
651
678
 
652
679
  if (!flags.help && !flags.json) {
653
680
  await isOffline();
@@ -667,6 +694,7 @@ export async function main() {
667
694
 
668
695
  if (flags["list-regions"]) {
669
696
  await listRegions();
697
+ await flushAnalytics();
670
698
  process.exit(0);
671
699
  }
672
700
 
@@ -678,7 +706,7 @@ export async function main() {
678
706
  region: region,
679
707
  "selection-method": "flag",
680
708
  "user-agent": userAgent,
681
- });
709
+ }, cliRunId);
682
710
  }
683
711
 
684
712
  if (flags.interactive) {
@@ -688,12 +716,13 @@ export async function main() {
688
716
  if (flags.json) {
689
717
  try {
690
718
  if (chooseRegionPrompt) {
691
- region = await promptForRegion(region, userAgent);
719
+ region = await promptForRegion(region, userAgent, cliRunId);
692
720
  } else {
693
721
  await validateRegion(region, true);
694
722
  }
695
- const result = await createDatabase(name, region, userAgent, true);
723
+ const result = await createDatabase(name, region, userAgent, cliRunId, true);
696
724
  console.log(JSON.stringify(result, null, 2));
725
+ await flushAnalytics();
697
726
  process.exit(0);
698
727
  } catch (e) {
699
728
  console.log(
@@ -703,6 +732,7 @@ export async function main() {
703
732
  2
704
733
  )
705
734
  );
735
+ await flushAnalytics();
706
736
  process.exit(1);
707
737
  }
708
738
  }
@@ -710,20 +740,23 @@ export async function main() {
710
740
  if (flags.env) {
711
741
  try {
712
742
  if (chooseRegionPrompt) {
713
- region = await promptForRegion(region, userAgent);
743
+ region = await promptForRegion(region, userAgent, cliRunId);
714
744
  } else {
715
745
  await validateRegion(region, true);
716
746
  }
717
- const result = await createDatabase(name, region, userAgent, true);
747
+ const result = await createDatabase(name, region, userAgent, cliRunId, true);
718
748
  if (result.error) {
719
749
  console.error(result.message || "Unknown error");
750
+ await flushAnalytics();
720
751
  process.exit(1);
721
752
  }
722
753
  console.log(`DATABASE_URL="${result.directConnectionString}"`);
723
754
  console.error("\n# Claim your database at: " + result.claimUrl);
755
+ await flushAnalytics();
724
756
  process.exit(0);
725
757
  } catch (e) {
726
758
  console.error(e?.message || String(e));
759
+ await flushAnalytics();
727
760
  process.exit(1);
728
761
  }
729
762
  }
@@ -738,16 +771,18 @@ export async function main() {
738
771
  )
739
772
  );
740
773
  if (chooseRegionPrompt) {
741
- region = await promptForRegion(region, userAgent);
774
+ region = await promptForRegion(region, userAgent, cliRunId);
742
775
  }
743
776
 
744
777
  region = await validateRegion(region);
745
778
 
746
- await createDatabase(name, region, userAgent);
779
+ await createDatabase(name, region, userAgent, cliRunId);
747
780
 
748
781
  outro("");
782
+ await flushAnalytics();
749
783
  } catch (error) {
750
784
  console.error("Error:", error.message);
785
+ await flushAnalytics();
751
786
  process.exit(1);
752
787
  }
753
788
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "1.0.10-pr62-posthog-and-minor-fixes-18784513395.0",
3
+ "version": "1.0.10-pr62-posthog-and-minor-fixes-18785189855.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": "prisma",