Skip to content

Commit 591affc

Browse files
add neondb support (#308)
1 parent 5cf7ddb commit 591affc

File tree

2 files changed

+88
-27
lines changed

2 files changed

+88
-27
lines changed

.changeset/poor-loops-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-better-t-stack": patch
3+
---
4+
5+
add neondb cli support

apps/cli/src/helpers/database-providers/neon-setup.ts

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ async function writeEnvFile(projectDir: string, config?: NeonConfig) {
114114
return true;
115115
}
116116

117+
async function setupWithNeonDb(
118+
projectDir: string,
119+
packageManager: PackageManager,
120+
) {
121+
try {
122+
const s = spinner();
123+
s.start("Creating Neon database using neondb...");
124+
125+
const serverDir = path.join(projectDir, "apps/server");
126+
await fs.ensureDir(serverDir);
127+
128+
const packageCmd = getPackageExecutionCommand(
129+
packageManager,
130+
"neondb --yes",
131+
);
132+
133+
await execa(packageCmd, {
134+
shell: true,
135+
cwd: serverDir,
136+
});
137+
138+
s.stop(pc.green("Neon database created successfully!"));
139+
140+
return true;
141+
} catch (error) {
142+
consola.error(pc.red("Failed to create database with neondb"));
143+
throw error;
144+
}
145+
}
146+
117147
function displayManualSetupInstructions() {
118148
log.info(`Manual Neon PostgreSQL Setup Instructions:
119149
@@ -129,43 +159,69 @@ export async function setupNeonPostgres(config: ProjectConfig): Promise<void> {
129159
const { packageManager, projectDir } = config;
130160

131161
try {
132-
const suggestedProjectName = path.basename(projectDir);
133-
const projectName = await text({
134-
message: "Enter a name for your Neon project:",
135-
defaultValue: suggestedProjectName,
136-
initialValue: suggestedProjectName,
137-
});
138-
139-
const regionId = await select({
140-
message: "Select a region for your Neon project:",
141-
options: NEON_REGIONS,
142-
initialValue: NEON_REGIONS[0].value,
162+
const setupMethod = await select({
163+
message: "Choose your Neon setup method:",
164+
options: [
165+
{
166+
label: "Quick setup with neondb",
167+
value: "neondb",
168+
hint: "fastest, no auth required",
169+
},
170+
{
171+
label: "Custom setup with neonctl",
172+
value: "neonctl",
173+
hint: "More control - choose project name and region",
174+
},
175+
],
176+
initialValue: "neondb",
143177
});
144178

145-
if (isCancel(projectName) || isCancel(regionId)) {
179+
if (isCancel(setupMethod)) {
146180
cancel(pc.red("Operation cancelled"));
147181
process.exit(0);
148182
}
149183

150-
const config = await createNeonProject(
151-
projectName as string,
152-
regionId,
153-
packageManager,
154-
);
155-
156-
if (!config) {
157-
throw new Error(
158-
"Failed to create project - couldn't get connection information",
184+
if (setupMethod === "neondb") {
185+
await setupWithNeonDb(projectDir, packageManager);
186+
} else {
187+
const suggestedProjectName = path.basename(projectDir);
188+
const projectName = await text({
189+
message: "Enter a name for your Neon project:",
190+
defaultValue: suggestedProjectName,
191+
initialValue: suggestedProjectName,
192+
});
193+
194+
const regionId = await select({
195+
message: "Select a region for your Neon project:",
196+
options: NEON_REGIONS,
197+
initialValue: NEON_REGIONS[0].value,
198+
});
199+
200+
if (isCancel(projectName) || isCancel(regionId)) {
201+
cancel(pc.red("Operation cancelled"));
202+
process.exit(0);
203+
}
204+
205+
const neonConfig = await createNeonProject(
206+
projectName as string,
207+
regionId,
208+
packageManager,
159209
);
160-
}
161210

162-
const finalSpinner = spinner();
163-
finalSpinner.start("Configuring database connection");
211+
if (!neonConfig) {
212+
throw new Error(
213+
"Failed to create project - couldn't get connection information",
214+
);
215+
}
216+
217+
const finalSpinner = spinner();
218+
finalSpinner.start("Configuring database connection");
164219

165-
await fs.ensureDir(path.join(projectDir, "apps/server"));
166-
await writeEnvFile(projectDir, config);
220+
await fs.ensureDir(path.join(projectDir, "apps/server"));
221+
await writeEnvFile(projectDir, neonConfig);
167222

168-
finalSpinner.stop("Neon database configured!");
223+
finalSpinner.stop("Neon database configured!");
224+
}
169225
} catch (error) {
170226
if (error instanceof Error) {
171227
consola.error(pc.red(error.message));

0 commit comments

Comments
 (0)