1
1
import path from "node:path" ;
2
- import { isCancel , log , select , text } from "@clack/prompts" ;
2
+ import { isCancel , log , select , spinner , text } from "@clack/prompts" ;
3
3
import { consola } from "consola" ;
4
4
import { execa } from "execa" ;
5
5
import fs from "fs-extra" ;
@@ -12,56 +12,77 @@ import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
12
12
13
13
type PrismaConfig = {
14
14
databaseUrl : string ;
15
+ claimUrl ?: string ;
15
16
} ;
16
17
18
+ type CreateDbResponse = {
19
+ connectionString : string ;
20
+ directConnectionString : string ;
21
+ claimUrl : string ;
22
+ deletionDate : string ;
23
+ region : string ;
24
+ name : string ;
25
+ projectId : string ;
26
+ } ;
27
+
28
+ const AVAILABLE_REGIONS = [
29
+ { value : "ap-southeast-1" , label : "Asia Pacific (Singapore)" } ,
30
+ { value : "ap-northeast-1" , label : "Asia Pacific (Tokyo)" } ,
31
+ { value : "eu-central-1" , label : "Europe (Frankfurt)" } ,
32
+ { value : "eu-west-3" , label : "Europe (Paris)" } ,
33
+ { value : "us-east-1" , label : "US East (N. Virginia)" } ,
34
+ { value : "us-west-1" , label : "US West (N. California)" } ,
35
+ ] ;
36
+
17
37
async function setupWithCreateDb (
18
38
serverDir : string ,
19
39
packageManager : PackageManager ,
20
40
orm : ORM ,
21
41
) {
22
42
try {
23
43
log . info (
24
- "Starting Prisma Postgres setup. Please follow the instructions below:" ,
44
+ "Starting Prisma Postgres setup with create-db . Please follow the instructions below:" ,
25
45
) ;
26
46
47
+ const selectedRegion = await select ( {
48
+ message : "Select your preferred region:" ,
49
+ options : AVAILABLE_REGIONS ,
50
+ initialValue : "ap-southeast-1" ,
51
+ } ) ;
52
+
53
+ if ( isCancel ( selectedRegion ) ) return null ;
54
+
27
55
const createDbCommand = getPackageExecutionCommand (
28
56
packageManager ,
29
- " create-db@latest -i" ,
57
+ ` create-db@latest --json --region ${ selectedRegion } ` ,
30
58
) ;
31
59
32
- await execa ( createDbCommand , {
60
+ const s = spinner ( ) ;
61
+ s . start ( "Creating Prisma Postgres database..." ) ;
62
+
63
+ const { stdout } = await execa ( createDbCommand , {
33
64
cwd : serverDir ,
34
- stdio : "inherit" ,
35
65
shell : true ,
36
66
} ) ;
37
67
38
- log . info (
39
- orm === "drizzle"
40
- ? pc . yellow (
41
- "Please copy the database URL from the output above and append ?sslmode=require for Drizzle." ,
42
- )
43
- : pc . yellow (
44
- "Please copy the Prisma Postgres URL from the output above." ,
45
- ) ,
46
- ) ;
68
+ s . stop ( "Database created successfully!" ) ;
47
69
48
- const databaseUrl = await text ( {
49
- message :
50
- orm === "drizzle"
51
- ? "Paste your database URL (append ?sslmode=require for Drizzle):"
52
- : "Paste your Prisma Postgres database URL:" ,
53
- validate ( value ) {
54
- if ( ! value ) return "Please enter a database URL" ;
55
- if ( orm === "drizzle" && ! value . includes ( "?sslmode=require" ) ) {
56
- return "Please append ?sslmode=require to your database URL when using Drizzle" ;
57
- }
58
- } ,
59
- } ) ;
70
+ let createDbResponse : CreateDbResponse ;
71
+ try {
72
+ createDbResponse = JSON . parse ( stdout ) as CreateDbResponse ;
73
+ } catch {
74
+ consola . error ( "Failed to parse create-db response" ) ;
75
+ return null ;
76
+ }
60
77
61
- if ( isCancel ( databaseUrl ) ) return null ;
78
+ const databaseUrl =
79
+ orm === "drizzle"
80
+ ? createDbResponse . directConnectionString
81
+ : createDbResponse . connectionString ;
62
82
63
83
return {
64
- databaseUrl : databaseUrl as string ,
84
+ databaseUrl,
85
+ claimUrl : createDbResponse . claimUrl ,
65
86
} ;
66
87
} catch ( error ) {
67
88
if ( error instanceof Error ) {
@@ -135,6 +156,15 @@ async function writeEnvFile(projectDir: string, config?: PrismaConfig) {
135
156
condition : true ,
136
157
} ,
137
158
] ;
159
+
160
+ if ( config ?. claimUrl ) {
161
+ variables . push ( {
162
+ key : "CLAIM_URL" ,
163
+ value : config . claimUrl ,
164
+ condition : true ,
165
+ } ) ;
166
+ }
167
+
138
168
await addEnvVariablesToFile ( envPath , variables ) ;
139
169
} catch ( _error ) {
140
170
consola . error ( "Failed to update environment configuration" ) ;
@@ -254,9 +284,18 @@ export async function setupPrismaPostgres(config: ProjectConfig) {
254
284
await addDotenvImportToPrismaConfig ( projectDir ) ;
255
285
await addPrismaAccelerateExtension ( serverDir ) ;
256
286
}
287
+
288
+ const connectionType =
289
+ orm === "drizzle" ? "direct connection" : "Prisma Accelerate" ;
257
290
log . success (
258
- pc . green ( "Prisma Postgres database configured successfully!" ) ,
291
+ pc . green (
292
+ `Prisma Postgres database configured successfully with ${ connectionType } !` ,
293
+ ) ,
259
294
) ;
295
+
296
+ if ( prismaConfig . claimUrl ) {
297
+ log . info ( pc . blue ( `Claim URL saved to .env: ${ prismaConfig . claimUrl } ` ) ) ;
298
+ }
260
299
} else {
261
300
await writeEnvFile ( projectDir ) ;
262
301
displayManualSetupInstructions ( ) ;
0 commit comments