Skip to content

Commit baf8a0b

Browse files
fix(cli): use ppg adapter for prisma postgres
1 parent eb45f50 commit baf8a0b

File tree

6 files changed

+52
-137
lines changed

6 files changed

+52
-137
lines changed

apps/cli/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const dependencyVersionMap = {
7272
"@prisma/adapter-better-sqlite3": "^7.0.0",
7373
"@prisma/adapter-pg": "^7.0.0",
7474
"@prisma/adapter-planetscale": "^7.0.0",
75+
"@prisma/adapter-ppg": "^7.0.0",
7576

7677
mongoose: "^8.14.0",
7778

apps/cli/src/helpers/core/db-setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export async function setupDatabase(
7878
"ws",
7979
);
8080
prismaDevDependencies.push("@types/ws");
81+
} else if (dbSetup === "prisma-postgres") {
82+
prismaDependencies.push("@prisma/adapter-ppg");
8183
} else {
8284
prismaDependencies.push("@prisma/adapter-pg");
8385
prismaDependencies.push("pg");

apps/cli/src/helpers/core/post-installation.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function displayPostInstallInstructions(
6060
: "";
6161
const nativeInstructions =
6262
(frontend?.includes("native-bare") ||
63-
frontend?.includes("native-uniwind") ||
63+
frontend?.includes("native-uniwind") ||
6464
frontend?.includes("native-unistyles")) &&
6565
backend !== "none"
6666
? getNativeInstructions(isConvex, isBackendSelf, frontend || [])
@@ -165,34 +165,34 @@ export async function displayPostInstallInstructions(
165165
addons?.includes("fumadocs");
166166

167167
if (hasAnyService) {
168-
output += `${pc.bold("Your project will be available at:")}\n`;
169-
170-
if (hasWeb) {
171-
output += `${pc.cyan("•")} Frontend: http://localhost:${webPort}\n`;
172-
} else if (!hasNative && !addons?.includes("starlight")) {
173-
output += `${pc.yellow(
174-
"NOTE:",
175-
)} You are creating a backend-only app\n (no frontend selected)\n`;
176-
}
168+
output += `${pc.bold("Your project will be available at:")}\n`;
169+
170+
if (hasWeb) {
171+
output += `${pc.cyan("•")} Frontend: http://localhost:${webPort}\n`;
172+
} else if (!hasNative && !addons?.includes("starlight")) {
173+
output += `${pc.yellow(
174+
"NOTE:",
175+
)} You are creating a backend-only app\n (no frontend selected)\n`;
176+
}
177177

178178
if (!isConvex && !isBackendSelf && hasStandaloneBackend) {
179-
output += `${pc.cyan("•")} Backend API: http://localhost:3000\n`;
179+
output += `${pc.cyan("•")} Backend API: http://localhost:3000\n`;
180180

181-
if (api === "orpc") {
182-
output += `${pc.cyan("•")} OpenAPI (Scalar UI): http://localhost:3000/api-reference\n`;
183-
}
181+
if (api === "orpc") {
182+
output += `${pc.cyan("•")} OpenAPI (Scalar UI): http://localhost:3000/api-reference\n`;
184183
}
184+
}
185185

186-
if (isBackendSelf && api === "orpc") {
187-
output += `${pc.cyan("•")} OpenAPI (Scalar UI): http://localhost:${webPort}/api/rpc/api-reference\n`;
188-
}
186+
if (isBackendSelf && api === "orpc") {
187+
output += `${pc.cyan("•")} OpenAPI (Scalar UI): http://localhost:${webPort}/api/rpc/api-reference\n`;
188+
}
189189

190-
if (addons?.includes("starlight")) {
191-
output += `${pc.cyan("•")} Docs: http://localhost:4321\n`;
192-
}
190+
if (addons?.includes("starlight")) {
191+
output += `${pc.cyan("•")} Docs: http://localhost:4321\n`;
192+
}
193193

194-
if (addons?.includes("fumadocs")) {
195-
output += `${pc.cyan("•")} Fumadocs: http://localhost:4000\n`;
194+
if (addons?.includes("fumadocs")) {
195+
output += `${pc.cyan("•")} Fumadocs: http://localhost:4000\n`;
196196
}
197197
}
198198

apps/cli/src/helpers/database-providers/prisma-postgres-setup.ts

Lines changed: 9 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import path from "node:path";
2-
import { isCancel, log, select, spinner, text } from "@clack/prompts";
2+
import { isCancel, log, select, spinner } from "@clack/prompts";
33
import { consola } from "consola";
44
import { execa } from "execa";
55
import fs from "fs-extra";
66
import pc from "picocolors";
77
import type { PackageManager, ProjectConfig } from "../../types";
8-
import { exitCancelled } from "../../utils/errors";
98
import { getPackageExecutionCommand } from "../../utils/package-runner";
109
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
1110

@@ -83,56 +82,6 @@ async function setupWithCreateDb(
8382
}
8483
}
8584

86-
async function initPrismaDatabase(
87-
serverDir: string,
88-
packageManager: PackageManager,
89-
) {
90-
try {
91-
const prismaDir = path.join(serverDir, "prisma");
92-
await fs.ensureDir(prismaDir);
93-
94-
log.info("Starting Prisma PostgreSQL setup.");
95-
96-
const prismaInitCommand = getPackageExecutionCommand(
97-
packageManager,
98-
"prisma init --db",
99-
);
100-
101-
await execa(prismaInitCommand, {
102-
cwd: serverDir,
103-
stdio: "inherit",
104-
shell: true,
105-
});
106-
107-
log.info(
108-
pc.yellow(
109-
"Please copy the Prisma Postgres URL.\nIt looks like: postgresql://user:password@host:5432/db?sslmode=require",
110-
),
111-
);
112-
113-
const databaseUrl = await text({
114-
message: "Paste your Prisma Postgres database URL:",
115-
validate(value) {
116-
if (!value) return "Please enter a database URL";
117-
if (!value.startsWith("postgresql://")) {
118-
return "URL should start with postgresql://";
119-
}
120-
},
121-
});
122-
123-
if (isCancel(databaseUrl)) return null;
124-
125-
return {
126-
databaseUrl: databaseUrl as string,
127-
};
128-
} catch (error) {
129-
if (error instanceof Error) {
130-
consola.error(error.message);
131-
}
132-
return null;
133-
}
134-
}
135-
13685
async function writeEnvFile(
13786
projectDir: string,
13887
backend: ProjectConfig["backend"],
@@ -165,25 +114,6 @@ async function writeEnvFile(
165114
}
166115
}
167116

168-
async function addDotenvImportToPrismaConfig(
169-
projectDir: string,
170-
backend: ProjectConfig["backend"],
171-
) {
172-
try {
173-
const prismaConfigPath = path.join(
174-
projectDir,
175-
"packages/db/prisma.config.ts",
176-
);
177-
let content = await fs.readFile(prismaConfigPath, "utf8");
178-
const envPath =
179-
backend === "self" ? "../../apps/web/.env" : "../../apps/server/.env";
180-
content = `import dotenv from "dotenv";\ndotenv.config({ path: "${envPath}" });\n${content}`;
181-
await fs.writeFile(prismaConfigPath, content);
182-
} catch (_error) {
183-
consola.error("Failed to update prisma.config.ts");
184-
}
185-
}
186-
187117
function displayManualSetupInstructions(target: "apps/web" | "apps/server") {
188118
log.info(`Manual Prisma PostgreSQL Setup Instructions:
189119
@@ -199,7 +129,7 @@ export async function setupPrismaPostgres(
199129
config: ProjectConfig,
200130
cliInput?: { manualDb?: boolean },
201131
) {
202-
const { packageManager, projectDir, orm, backend } = config;
132+
const { packageManager, projectDir, backend } = config;
203133
const manualDb = cliInput?.manualDb ?? false;
204134
const dbDir = path.join(projectDir, "packages/db");
205135

@@ -214,72 +144,38 @@ export async function setupPrismaPostgres(
214144
return;
215145
}
216146

217-
const mode = await select({
147+
const setupMode = await select({
218148
message: "Prisma Postgres setup: choose mode",
219149
options: [
220150
{
221-
label: "Automatic",
151+
label: "Automatic (create-db)",
222152
value: "auto",
223-
hint: "Automated setup with provider CLI, sets .env",
153+
hint: "Provision a database via Prisma's create-db CLI",
224154
},
225155
{
226156
label: "Manual",
227157
value: "manual",
228-
hint: "Manual setup, add env vars yourself",
158+
hint: "Add your own DATABASE_URL later",
229159
},
230160
],
231161
initialValue: "auto",
232162
});
233163

234-
if (isCancel(mode)) return exitCancelled("Operation cancelled");
164+
if (isCancel(setupMode)) return;
235165

236-
if (mode === "manual") {
166+
if (setupMode === "manual") {
237167
await writeEnvFile(projectDir, backend);
238168
displayManualSetupInstructions(
239169
backend === "self" ? "apps/web" : "apps/server",
240170
);
241171
return;
242172
}
243173

244-
const setupOptions = [
245-
{
246-
label: "Quick setup with create-db",
247-
value: "create-db",
248-
hint: "Fastest, automated database creation (no auth)",
249-
},
250-
];
251-
252-
if (orm === "prisma") {
253-
setupOptions.push({
254-
label: "Custom setup with Prisma Init",
255-
value: "custom",
256-
hint: "More control (requires auth)",
257-
});
258-
}
259-
260-
const setupMethod = await select({
261-
message: "Choose your Prisma Postgres setup method:",
262-
options: setupOptions,
263-
initialValue: "create-db",
264-
});
265-
266-
if (isCancel(setupMethod)) return exitCancelled("Operation cancelled");
267-
268-
let prismaConfig: PrismaConfig | null = null;
269-
270-
if (setupMethod === "create-db") {
271-
prismaConfig = await setupWithCreateDb(dbDir, packageManager);
272-
} else {
273-
prismaConfig = await initPrismaDatabase(dbDir, packageManager);
274-
}
174+
const prismaConfig = await setupWithCreateDb(dbDir, packageManager);
275175

276176
if (prismaConfig) {
277177
await writeEnvFile(projectDir, backend, prismaConfig);
278178

279-
if (orm === "prisma") {
280-
await addDotenvImportToPrismaConfig(projectDir, backend);
281-
}
282-
283179
log.success(
284180
pc.green("Prisma Postgres database configured successfully!"),
285181
);

apps/cli/templates/db/prisma/postgres/prisma.config.ts.hbs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from "node:path";
22
import { defineConfig, env } from 'prisma/config'
3-
{{#unless (eq dbSetup "prisma-postgres")}}
43
import dotenv from 'dotenv'
54

65
dotenv.config({
@@ -10,7 +9,6 @@ dotenv.config({
109
path: "../../apps/server/.env",
1110
{{/if}}
1211
})
13-
{{/unless}}
1412

1513
export default defineConfig({
1614
schema: path.join("prisma", "schema"),

apps/cli/templates/db/prisma/postgres/src/index.ts.hbs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ const prisma = new PrismaClient({
1313
}),
1414
});
1515

16+
{{else if (eq dbSetup "prisma-postgres")}}
17+
import { PrismaPostgresAdapter } from "@prisma/adapter-ppg";
18+
19+
const adapter = new PrismaPostgresAdapter({
20+
connectionString: env.DATABASE_URL || "",
21+
});
22+
23+
const prisma = new PrismaClient({ adapter });
24+
1625
{{else}}
1726
import { PrismaPg } from "@prisma/adapter-pg";
1827

@@ -38,6 +47,15 @@ const adapter = new PrismaNeon({
3847

3948
const prisma = new PrismaClient({ adapter });
4049

50+
{{else if (eq dbSetup "prisma-postgres")}}
51+
import { PrismaPostgresAdapter } from "@prisma/adapter-ppg";
52+
53+
const adapter = new PrismaPostgresAdapter({
54+
connectionString: process.env.DATABASE_URL || "",
55+
});
56+
57+
const prisma = new PrismaClient({ adapter });
58+
4159
{{else}}
4260
import { PrismaPg } from "@prisma/adapter-pg";
4361

0 commit comments

Comments
 (0)