Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions create-db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Options:
${chalk.yellow("--json, -j")} Output machine-readable JSON and exit
${chalk.yellow("--list-regions")} List available regions and exit
${chalk.yellow("--help, -h")} Show this help message
${chalk.yellow("--env, -e")} Outputs DATABASE_URL to the terminal

Examples:
${chalk.gray(`npx ${CLI_NAME} --region us-east-1`)}
Expand All @@ -215,12 +216,14 @@ async function parseArgs() {
"list-regions",
"interactive",
"json",
"env",
];
const shorthandMap = {
r: "region",
i: "interactive",
h: "help",
j: "json",
e: "env",
};

const exitWithError = (message) => {
Expand Down Expand Up @@ -293,6 +296,28 @@ async function parseArgs() {
return { flags };
}

function validateFlagCombinations(flags) {
const conflictingFlags = [
["env", "json"],
["list-regions", "env"],
["list-regions", "json"],
["list-regions", "interactive"],
["list-regions", "region"],
];

for (const [flag1, flag2] of conflictingFlags) {
if (flags[flag1] && flags[flag2]) {
console.error(
chalk.red.bold(
`\n✖ Error: Cannot use --${flag1} and --${flag2} together.\n`
)
);
console.error(chalk.gray("Use --help or -h to see available options.\n"));
process.exit(1);
}
}
}

export async function getRegions(returnJson = false) {
const url = `${CREATE_DB_WORKER_URL}/regions`;
const res = await fetch(url);
Expand Down Expand Up @@ -389,9 +414,9 @@ async function promptForRegion(defaultRegion, userAgent) {
return region;
}

async function createDatabase(name, region, userAgent, returnJson = false) {
async function createDatabase(name, region, userAgent, silent = false) {
let s;
if (!returnJson) {
if (!silent) {
s = spinner();
s.start("Creating your database...");
}
Expand All @@ -407,7 +432,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
});

if (resp.status === 429) {
if (returnJson) {
if (silent) {
return {
error: "rate_limit_exceeded",
message:
Expand Down Expand Up @@ -439,7 +464,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
raw = await resp.text();
result = JSON.parse(raw);
} catch (e) {
if (returnJson) {
if (silent) {
return {
error: "invalid_json",
message: "Unexpected response from create service.",
Expand Down Expand Up @@ -488,7 +513,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${userAgent}&utm_medium=cli`;
const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000);

if (returnJson && !result.error) {
if (silent && !result.error) {
const jsonResponse = {
connectionString: prismaConn,
directConnectionString: directConn,
Expand All @@ -507,7 +532,7 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
}

if (result.error) {
if (returnJson) {
if (silent) {
return {
error: "api_error",
message: result.error.message || "Unknown error",
Expand Down Expand Up @@ -593,6 +618,8 @@ async function main() {

const { flags } = await parseArgs();

validateFlagCombinations(flags);

let userAgent;
const userEnvVars = readUserEnvFile();
if (userEnvVars.PRISMA_ACTOR_NAME && userEnvVars.PRISMA_ACTOR_PROJECT) {
Expand Down Expand Up @@ -670,6 +697,29 @@ async function main() {
}
}

if (flags.env) {
try {
if (chooseRegionPrompt) {
region = await promptForRegion(region, userAgent);
} else {
await validateRegion(region, true);
}
const result = await createDatabase(name, region, userAgent, true);
if (result.error) {
console.error(result.message || "Unknown error");
process.exit(1);
}
process.stdout.write(`DATABASE_URL="${result.directConnectionString}"`);
process.stderr.write(
"\n\n# Claim your database at: " + result.claimUrl
);
process.exit(0);
} catch (e) {
console.error(e?.message || String(e));
process.exit(1);
}
}

intro(chalk.cyan.bold("🚀 Creating a Prisma Postgres database"));
log.message(
chalk.white(`Provisioning a temporary database in ${region}...`)
Expand Down
2 changes: 1 addition & 1 deletion create-db/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-db",
"version": "1.0.6",
"version": "1.0.7",
"description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
"main": "index.js",
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion create-pg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-pg",
"version": "1.0.6",
"version": "1.0.7",
"description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
"main": "index.js",
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion create-postgres/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-postgres",
"version": "1.0.6",
"version": "1.0.7",
"description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
"main": "index.js",
"author": "",
Expand Down