Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions .cursor/rules/better-t-stack-repo.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
alwaysApply: true
---

- use functional programming
- use normal function syntax for functions do not use arrow functions
- no emojis
- use type syntax, dont use interface syntax for types
97 changes: 68 additions & 29 deletions apps/cli/src/helpers/database-providers/prisma-postgres-setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { isCancel, log, select, text } from "@clack/prompts";
import { isCancel, log, select, spinner, text } from "@clack/prompts";
import { consola } from "consola";
import { execa } from "execa";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use Bun.$ for subprocesses instead of execa

Guidelines require Bun.$. Replace execa calls accordingly.

-import { execa } from "execa";
+import { $ } from "bun";
@@
-    const { stdout } = await execa(createDbCommand, {
-      cwd: serverDir,
-      shell: true,
-    });
+    const stdout = await $`bash -lc "cd ${serverDir} && ${createDbCommand}"`.text();
@@
-    await execa(prismaInitCommand, {
-      cwd: serverDir,
-      stdio: "inherit",
-      shell: true,
-    });
+    await $`bash -lc "cd ${serverDir} && ${prismaInitCommand}"`;

If you prefer avoiding bash -lc, split args and use $ with a cwd-scoped variant.

Also applies to: 63-66, 112-116

🤖 Prompt for AI Agents
In apps/cli/src/helpers/database-providers/prisma-postgres-setup.ts around line
4 (also apply changes at lines 63-66 and 112-116), replace usage of execa
imports and calls with Bun.$ subprocess calls; remove the import of execa and
convert each execa invocation to Bun.$ by passing the command and args without
relying on "bash -lc" (or if you must use a shell, use Bun.spawn or Bun.$ with
proper shell flag), and prefer passing argv as separate elements and using the
cwd option when needed to avoid shell quoting issues; ensure exit codes/errors
are handled similarly (await the Bun.$ promise or catch errors) so behavior and
logging remain consistent.

import fs from "fs-extra";
Expand All @@ -12,56 +12,77 @@ import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";

type PrismaConfig = {
databaseUrl: string;
claimUrl?: string;
};

type CreateDbResponse = {
connectionString: string;
directConnectionString: string;
claimUrl: string;
deletionDate: string;
region: string;
name: string;
projectId: string;
};

const AVAILABLE_REGIONS = [
{ value: "ap-southeast-1", label: "Asia Pacific (Singapore)" },
{ value: "ap-northeast-1", label: "Asia Pacific (Tokyo)" },
{ value: "eu-central-1", label: "Europe (Frankfurt)" },
{ value: "eu-west-3", label: "Europe (Paris)" },
{ value: "us-east-1", label: "US East (N. Virginia)" },
{ value: "us-west-1", label: "US West (N. California)" },
];

async function setupWithCreateDb(
serverDir: string,
packageManager: PackageManager,
orm: ORM,
) {
try {
log.info(
"Starting Prisma Postgres setup. Please follow the instructions below:",
"Starting Prisma Postgres setup with create-db. Please follow the instructions below:",
);

const selectedRegion = await select({
message: "Select your preferred region:",
options: AVAILABLE_REGIONS,
initialValue: "ap-southeast-1",
});

if (isCancel(selectedRegion)) return null;

const createDbCommand = getPackageExecutionCommand(
packageManager,
"create-db@latest -i",
`create-db@latest --json --region ${selectedRegion}`,
);

await execa(createDbCommand, {
const s = spinner();
s.start("Creating Prisma Postgres database...");

const { stdout } = await execa(createDbCommand, {
cwd: serverDir,
stdio: "inherit",
shell: true,
});

log.info(
orm === "drizzle"
? pc.yellow(
"Please copy the database URL from the output above and append ?sslmode=require for Drizzle.",
)
: pc.yellow(
"Please copy the Prisma Postgres URL from the output above.",
),
);
s.stop("Database created successfully!");

const databaseUrl = await text({
message:
orm === "drizzle"
? "Paste your database URL (append ?sslmode=require for Drizzle):"
: "Paste your Prisma Postgres database URL:",
validate(value) {
if (!value) return "Please enter a database URL";
if (orm === "drizzle" && !value.includes("?sslmode=require")) {
return "Please append ?sslmode=require to your database URL when using Drizzle";
}
},
});
let createDbResponse: CreateDbResponse;
try {
createDbResponse = JSON.parse(stdout) as CreateDbResponse;
} catch {
consola.error("Failed to parse create-db response");
return null;
}

if (isCancel(databaseUrl)) return null;
const databaseUrl =
orm === "drizzle"
? createDbResponse.directConnectionString
: createDbResponse.connectionString;

return {
databaseUrl: databaseUrl as string,
databaseUrl,
claimUrl: createDbResponse.claimUrl,
};
} catch (error) {
if (error instanceof Error) {
Expand Down Expand Up @@ -135,6 +156,15 @@ async function writeEnvFile(projectDir: string, config?: PrismaConfig) {
condition: true,
},
];

if (config?.claimUrl) {
variables.push({
key: "CLAIM_URL",
value: config.claimUrl,
condition: true,
});
}

await addEnvVariablesToFile(envPath, variables);
} catch (_error) {
consola.error("Failed to update environment configuration");
Expand Down Expand Up @@ -254,9 +284,18 @@ export async function setupPrismaPostgres(config: ProjectConfig) {
await addDotenvImportToPrismaConfig(projectDir);
await addPrismaAccelerateExtension(serverDir);
}

const connectionType =
orm === "drizzle" ? "direct connection" : "Prisma Accelerate";
log.success(
pc.green("Prisma Postgres database configured successfully!"),
pc.green(
`Prisma Postgres database configured successfully with ${connectionType}!`,
),
);

if (prismaConfig.claimUrl) {
log.info(pc.blue(`Claim URL saved to .env: ${prismaConfig.claimUrl}`));
}
} else {
await writeEnvFile(projectDir);
displayManualSetupInstructions();
Expand Down