Skip to content

Commit d8a84f1

Browse files
Merge pull request #65 from prisma/DC-6214-accelerate-removed
DC-6214 Feat: Accelerate removed
2 parents 90211a6 + 1aa2c31 commit d8a84f1

File tree

1 file changed

+132
-89
lines changed

1 file changed

+132
-89
lines changed

create-db/index.js

Lines changed: 132 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -429,17 +429,27 @@ async function promptForRegion(defaultRegion, userAgent, cliRunId) {
429429
process.exit(0);
430430
}
431431

432-
void sendAnalyticsToWorker("create_db:region_selected", {
433-
command: CLI_NAME,
434-
region: region,
435-
"selection-method": "interactive",
436-
"user-agent": userAgent,
437-
}, cliRunId);
432+
void sendAnalyticsToWorker(
433+
"create_db:region_selected",
434+
{
435+
command: CLI_NAME,
436+
region: region,
437+
"selection-method": "interactive",
438+
"user-agent": userAgent,
439+
},
440+
cliRunId
441+
);
438442

439443
return region;
440444
}
441445

442-
async function createDatabase(name, region, userAgent, cliRunId, silent = false) {
446+
async function createDatabase(
447+
name,
448+
region,
449+
userAgent,
450+
cliRunId,
451+
silent = false
452+
) {
443453
let s;
444454
if (!silent) {
445455
s = spinner();
@@ -473,13 +483,17 @@ async function createDatabase(name, region, userAgent, cliRunId, silent = false)
473483
);
474484
}
475485

476-
void sendAnalyticsToWorker("create_db:database_creation_failed", {
477-
command: CLI_NAME,
478-
region: region,
479-
"error-type": "rate_limit",
480-
"status-code": 429,
481-
"user-agent": userAgent,
482-
}, cliRunId);
486+
void sendAnalyticsToWorker(
487+
"create_db:database_creation_failed",
488+
{
489+
command: CLI_NAME,
490+
region: region,
491+
"error-type": "rate_limit",
492+
"status-code": 429,
493+
"user-agent": userAgent,
494+
},
495+
cliRunId
496+
);
483497

484498
await flushAnalytics();
485499
process.exit(1);
@@ -503,21 +517,24 @@ async function createDatabase(name, region, userAgent, cliRunId, silent = false)
503517
s.stop("Unexpected response from create service.");
504518
}
505519

506-
void sendAnalyticsToWorker("create_db:database_creation_failed", {
507-
command: CLI_NAME,
508-
region,
509-
"error-type": "invalid_json",
510-
"status-code": resp.status,
511-
"user-agent": userAgent,
512-
}, cliRunId);
520+
void sendAnalyticsToWorker(
521+
"create_db:database_creation_failed",
522+
{
523+
command: CLI_NAME,
524+
region,
525+
"error-type": "invalid_json",
526+
"status-code": resp.status,
527+
"user-agent": userAgent,
528+
},
529+
cliRunId
530+
);
513531

514532
await flushAnalytics();
515533
process.exit(1);
516534
}
517535

518536
const database = result.data ? result.data.database : result.databases?.[0];
519537
const projectId = result.data ? result.data.id : result.id;
520-
const prismaConn = database?.connectionString;
521538

522539
const directConnDetails = result.data
523540
? database?.apiKeys?.[0]?.directConnection
@@ -543,8 +560,7 @@ async function createDatabase(name, region, userAgent, cliRunId, silent = false)
543560

544561
if (silent && !result.error) {
545562
const jsonResponse = {
546-
connectionString: prismaConn,
547-
directConnectionString: directConn,
563+
connectionString: directConn,
548564
claimUrl: claimUrl,
549565
deletionDate: expiryDate.toISOString(),
550566
region: database?.region?.id || region,
@@ -575,13 +591,17 @@ async function createDatabase(name, region, userAgent, cliRunId, silent = false)
575591
);
576592
}
577593

578-
void sendAnalyticsToWorker("create_db:database_creation_failed", {
579-
command: CLI_NAME,
580-
region: region,
581-
"error-type": "api_error",
582-
"error-message": result.error.message,
583-
"user-agent": userAgent,
584-
}, cliRunId);
594+
void sendAnalyticsToWorker(
595+
"create_db:database_creation_failed",
596+
{
597+
command: CLI_NAME,
598+
region: region,
599+
"error-type": "api_error",
600+
"error-message": result.error.message,
601+
"user-agent": userAgent,
602+
},
603+
cliRunId
604+
);
585605

586606
await flushAnalytics();
587607
process.exit(1);
@@ -593,53 +613,43 @@ async function createDatabase(name, region, userAgent, cliRunId, silent = false)
593613

594614
const expiryFormatted = expiryDate.toLocaleString();
595615

616+
log.message("");
617+
log.info(chalk.bold("Database Connection"));
596618
log.message("");
597619

598-
log.info(chalk.bold("Connect to your database →"));
599-
600-
if (prismaConn) {
601-
log.message(
602-
chalk.magenta(" Use this connection string optimized for Prisma ORM:")
603-
);
604-
log.message(" " + chalk.yellow(prismaConn));
605-
log.message("");
606-
}
607-
620+
// Direct connection (only output this one)
608621
if (directConn) {
609-
log.message(
610-
chalk.cyan(" Use this connection string for everything else:")
611-
);
622+
log.message(chalk.cyan(" Connection String:"));
612623
log.message(" " + chalk.yellow(directConn));
613624
log.message("");
614625
} else {
615-
log.warning(
616-
chalk.yellow(
617-
"Direct connection details are not available in the API response."
618-
)
619-
);
626+
log.warning(chalk.yellow(" Connection details are not available."));
627+
log.message("");
620628
}
621629

630+
631+
// Claim database section
622632
const clickableUrl = terminalLink(claimUrl, claimUrl, { fallback: false });
623-
log.success(`${chalk.bold("Claim your database →")}`);
624-
log.message(
625-
chalk.cyan(" Want to keep your database? Claim for free via this link:")
626-
);
633+
log.success(chalk.bold("Claim Your Database"));
634+
log.message(chalk.cyan(" Keep your database for free:"));
627635
log.message(" " + chalk.yellow(clickableUrl));
628636
log.message(
629637
chalk.italic(
630638
chalk.gray(
631-
" Your database will be deleted on " +
632-
expiryFormatted +
633-
" if not claimed."
639+
` Database will be deleted on ${expiryFormatted} if not claimed.`
634640
)
635641
)
636642
);
637643

638-
void sendAnalyticsToWorker("create_db:database_created", {
639-
command: CLI_NAME,
640-
region,
641-
utm_source: CLI_NAME,
642-
}, cliRunId);
644+
void sendAnalyticsToWorker(
645+
"create_db:database_created",
646+
{
647+
command: CLI_NAME,
648+
region,
649+
utm_source: CLI_NAME,
650+
},
651+
cliRunId
652+
);
643653
}
644654

645655
export async function main() {
@@ -659,22 +669,27 @@ export async function main() {
659669
userAgent = `${userEnvVars.PRISMA_ACTOR_NAME}/${userEnvVars.PRISMA_ACTOR_PROJECT}`;
660670
}
661671

662-
void sendAnalyticsToWorker("create_db:cli_command_ran", {
663-
command: CLI_NAME,
664-
"full-command": `${CLI_NAME} ${rawArgs.join(" ")}`.trim(),
665-
"has-region-flag": rawArgs.includes("--region") || rawArgs.includes("-r"),
666-
"has-interactive-flag":
667-
rawArgs.includes("--interactive") || rawArgs.includes("-i"),
668-
"has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
669-
"has-list-regions-flag": rawArgs.includes("--list-regions"),
670-
"has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
671-
"has-env-flag": rawArgs.includes("--env") || rawArgs.includes("-e"),
672-
"has-user-agent-from-env": !!userAgent,
673-
"node-version": process.version,
674-
platform: process.platform,
675-
arch: process.arch,
676-
"user-agent": userAgent,
677-
}, cliRunId);
672+
void sendAnalyticsToWorker(
673+
"create_db:cli_command_ran",
674+
{
675+
command: CLI_NAME,
676+
"full-command": `${CLI_NAME} ${rawArgs.join(" ")}`.trim(),
677+
"has-region-flag":
678+
rawArgs.includes("--region") || rawArgs.includes("-r"),
679+
"has-interactive-flag":
680+
rawArgs.includes("--interactive") || rawArgs.includes("-i"),
681+
"has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
682+
"has-list-regions-flag": rawArgs.includes("--list-regions"),
683+
"has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
684+
"has-env-flag": rawArgs.includes("--env") || rawArgs.includes("-e"),
685+
"has-user-agent-from-env": !!userAgent,
686+
"node-version": process.version,
687+
platform: process.platform,
688+
arch: process.arch,
689+
"user-agent": userAgent,
690+
},
691+
cliRunId
692+
);
678693

679694
if (!flags.help && !flags.json) {
680695
await isOffline();
@@ -701,12 +716,16 @@ export async function main() {
701716
if (flags.region) {
702717
region = flags.region;
703718

704-
void sendAnalyticsToWorker("create_db:region_selected", {
705-
command: CLI_NAME,
706-
region: region,
707-
"selection-method": "flag",
708-
"user-agent": userAgent,
709-
}, cliRunId);
719+
void sendAnalyticsToWorker(
720+
"create_db:region_selected",
721+
{
722+
command: CLI_NAME,
723+
region: region,
724+
"selection-method": "flag",
725+
"user-agent": userAgent,
726+
},
727+
cliRunId
728+
);
710729
}
711730

712731
if (flags.interactive) {
@@ -720,7 +739,13 @@ export async function main() {
720739
} else {
721740
await validateRegion(region, true);
722741
}
723-
const result = await createDatabase(name, region, userAgent, cliRunId, true);
742+
const result = await createDatabase(
743+
name,
744+
region,
745+
userAgent,
746+
cliRunId,
747+
true
748+
);
724749
console.log(JSON.stringify(result, null, 2));
725750
await flushAnalytics();
726751
process.exit(0);
@@ -744,13 +769,19 @@ export async function main() {
744769
} else {
745770
await validateRegion(region, true);
746771
}
747-
const result = await createDatabase(name, region, userAgent, cliRunId, true);
772+
const result = await createDatabase(
773+
name,
774+
region,
775+
userAgent,
776+
cliRunId,
777+
true
778+
);
748779
if (result.error) {
749780
console.error(result.message || "Unknown error");
750781
await flushAnalytics();
751782
process.exit(1);
752783
}
753-
console.log(`DATABASE_URL="${result.directConnectionString}"`);
784+
console.log(`DATABASE_URL="${result.connectionString}"`);
754785
console.error("\n# Claim your database at: " + result.claimUrl);
755786
await flushAnalytics();
756787
process.exit(0);
@@ -787,7 +818,19 @@ export async function main() {
787818
}
788819
}
789820

790-
// Only run main() if this file is being executed directly, not when imported
791-
if (import.meta.url === `file://${process.argv[1]}`) {
792-
main();
821+
// Run main() if this file is being executed directly
822+
const isDirectExecution =
823+
import.meta.url.endsWith("/index.js") ||
824+
process.argv[1] === import.meta.url.replace("file://", "") ||
825+
process.argv[1].includes("create-db") ||
826+
process.argv[1].includes("create-pg") ||
827+
process.argv[1].includes("create-postgres");
828+
829+
if (isDirectExecution && !process.env.__CREATE_DB_EXECUTING) {
830+
process.env.__CREATE_DB_EXECUTING = "true";
831+
main().catch(console.error);
793832
}
833+
834+
// if (import.meta.url.endsWith('/index.js') || process.argv[1] === import.meta.url.replace('file://', '')) {
835+
// main().catch(console.error);
836+
// }

0 commit comments

Comments
 (0)