create-db 1.0.10-pr62-posthog-and-minor-fixes-18784162723.0 → 1.0.10-pr62-posthog-and-minor-fixes-18784790117.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 +54 -17
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -17,24 +17,44 @@ const CREATE_DB_WORKER_URL =
17
17
  const CLAIM_DB_WORKER_URL =
18
18
  process.env.CLAIM_DB_WORKER_URL || "https://create-db.prisma.io";
19
19
 
20
+ // Track pending analytics promises to ensure they complete before exit
21
+ const pendingAnalytics = [];
22
+
20
23
  async function sendAnalyticsToWorker(eventName, properties) {
21
24
  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
- }
25
+ const timer = setTimeout(() => controller.abort(), 5000);
26
+
27
+ const analyticsPromise = (async () => {
28
+ try {
29
+ const payload = {
30
+ eventName,
31
+ properties: { distinct_id: CLI_RUN_ID, ...(properties || {}) },
32
+ };
33
+ await fetch(`${CREATE_DB_WORKER_URL}/analytics`, {
34
+ method: "POST",
35
+ headers: { "Content-Type": "application/json" },
36
+ body: JSON.stringify(payload),
37
+ signal: controller.signal,
38
+ });
39
+ } catch (error) {
40
+ // Silently fail - analytics shouldn't block CLI
41
+ } finally {
42
+ clearTimeout(timer);
43
+ }
44
+ })();
45
+
46
+ pendingAnalytics.push(analyticsPromise);
47
+ return analyticsPromise;
48
+ }
49
+
50
+ // Wait for all pending analytics with a timeout
51
+ async function flushAnalytics(maxWaitMs = 500) {
52
+ if (pendingAnalytics.length === 0) return;
53
+
54
+ const timeout = new Promise((resolve) => setTimeout(resolve, maxWaitMs));
55
+ const allAnalytics = Promise.all(pendingAnalytics);
56
+
57
+ await Promise.race([allAnalytics, timeout]);
38
58
  }
39
59
 
40
60
  async function detectUserLocation() {
@@ -135,6 +155,7 @@ async function isOffline() {
135
155
  `Check your internet connection or visit ${chalk.green("https://www.prisma-status.com/\n")}`
136
156
  )
137
157
  );
158
+ await flushAnalytics();
138
159
  process.exit(1);
139
160
  }
140
161
  }
@@ -205,6 +226,7 @@ Examples:
205
226
  ${chalk.gray(`npx ${CLI_NAME} --env --region us-east-1`)}
206
227
  ${chalk.gray(`npx ${CLI_NAME} --env >> .env`)}
207
228
  `);
229
+ await flushAnalytics();
208
230
  process.exit(0);
209
231
  }
210
232
 
@@ -405,6 +427,7 @@ async function promptForRegion(defaultRegion, userAgent) {
405
427
 
406
428
  if (region === null) {
407
429
  cancel(chalk.red("Operation cancelled."));
430
+ await flushAnalytics();
408
431
  process.exit(0);
409
432
  }
410
433
 
@@ -460,6 +483,7 @@ async function createDatabase(name, region, userAgent, silent = false) {
460
483
  "user-agent": userAgent,
461
484
  });
462
485
 
486
+ await flushAnalytics();
463
487
  process.exit(1);
464
488
  }
465
489
 
@@ -489,6 +513,7 @@ async function createDatabase(name, region, userAgent, silent = false) {
489
513
  "user-agent": userAgent,
490
514
  });
491
515
 
516
+ await flushAnalytics();
492
517
  process.exit(1);
493
518
  }
494
519
 
@@ -560,6 +585,7 @@ async function createDatabase(name, region, userAgent, silent = false) {
560
585
  "user-agent": userAgent,
561
586
  });
562
587
 
588
+ await flushAnalytics();
563
589
  process.exit(1);
564
590
  }
565
591
 
@@ -667,6 +693,7 @@ export async function main() {
667
693
 
668
694
  if (flags["list-regions"]) {
669
695
  await listRegions();
696
+ await flushAnalytics();
670
697
  process.exit(0);
671
698
  }
672
699
 
@@ -694,6 +721,7 @@ export async function main() {
694
721
  }
695
722
  const result = await createDatabase(name, region, userAgent, true);
696
723
  console.log(JSON.stringify(result, null, 2));
724
+ await flushAnalytics();
697
725
  process.exit(0);
698
726
  } catch (e) {
699
727
  console.log(
@@ -703,6 +731,7 @@ export async function main() {
703
731
  2
704
732
  )
705
733
  );
734
+ await flushAnalytics();
706
735
  process.exit(1);
707
736
  }
708
737
  }
@@ -717,13 +746,16 @@ export async function main() {
717
746
  const result = await createDatabase(name, region, userAgent, true);
718
747
  if (result.error) {
719
748
  console.error(result.message || "Unknown error");
749
+ await flushAnalytics();
720
750
  process.exit(1);
721
751
  }
722
752
  console.log(`DATABASE_URL="${result.directConnectionString}"`);
723
753
  console.error("\n# Claim your database at: " + result.claimUrl);
754
+ await flushAnalytics();
724
755
  process.exit(0);
725
756
  } catch (e) {
726
757
  console.error(e?.message || String(e));
758
+ await flushAnalytics();
727
759
  process.exit(1);
728
760
  }
729
761
  }
@@ -746,10 +778,15 @@ export async function main() {
746
778
  await createDatabase(name, region, userAgent);
747
779
 
748
780
  outro("");
781
+ await flushAnalytics();
749
782
  } catch (error) {
750
783
  console.error("Error:", error.message);
784
+ await flushAnalytics();
751
785
  process.exit(1);
752
786
  }
753
787
  }
754
788
 
755
- main();
789
+ // Only run main() if this file is being executed directly, not when imported
790
+ if (import.meta.url === `file://${process.argv[1]}`) {
791
+ main();
792
+ }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "1.0.10-pr62-posthog-and-minor-fixes-18784162723.0",
3
+ "version": "1.0.10-pr62-posthog-and-minor-fixes-18784790117.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
- "author": "",
6
+ "author": "prisma",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/prisma/create-db.git"