add neondb support (#308)

This commit is contained in:
Aman Varshney
2025-06-07 21:55:00 +05:30
committed by GitHub
parent 5cf7ddba11
commit 591affcd07
2 changed files with 89 additions and 28 deletions

View File

@@ -0,0 +1,5 @@
---
"create-better-t-stack": patch
---
add neondb cli support

View File

@@ -114,6 +114,36 @@ async function writeEnvFile(projectDir: string, config?: NeonConfig) {
return true; return true;
} }
async function setupWithNeonDb(
projectDir: string,
packageManager: PackageManager,
) {
try {
const s = spinner();
s.start("Creating Neon database using neondb...");
const serverDir = path.join(projectDir, "apps/server");
await fs.ensureDir(serverDir);
const packageCmd = getPackageExecutionCommand(
packageManager,
"neondb --yes",
);
await execa(packageCmd, {
shell: true,
cwd: serverDir,
});
s.stop(pc.green("Neon database created successfully!"));
return true;
} catch (error) {
consola.error(pc.red("Failed to create database with neondb"));
throw error;
}
}
function displayManualSetupInstructions() { function displayManualSetupInstructions() {
log.info(`Manual Neon PostgreSQL Setup Instructions: log.info(`Manual Neon PostgreSQL Setup Instructions:
@@ -129,43 +159,69 @@ export async function setupNeonPostgres(config: ProjectConfig): Promise<void> {
const { packageManager, projectDir } = config; const { packageManager, projectDir } = config;
try { try {
const suggestedProjectName = path.basename(projectDir); const setupMethod = await select({
const projectName = await text({ message: "Choose your Neon setup method:",
message: "Enter a name for your Neon project:", options: [
defaultValue: suggestedProjectName, {
initialValue: suggestedProjectName, label: "Quick setup with neondb",
value: "neondb",
hint: "fastest, no auth required",
},
{
label: "Custom setup with neonctl",
value: "neonctl",
hint: "More control - choose project name and region",
},
],
initialValue: "neondb",
}); });
const regionId = await select({ if (isCancel(setupMethod)) {
message: "Select a region for your Neon project:",
options: NEON_REGIONS,
initialValue: NEON_REGIONS[0].value,
});
if (isCancel(projectName) || isCancel(regionId)) {
cancel(pc.red("Operation cancelled")); cancel(pc.red("Operation cancelled"));
process.exit(0); process.exit(0);
} }
const config = await createNeonProject( if (setupMethod === "neondb") {
projectName as string, await setupWithNeonDb(projectDir, packageManager);
regionId, } else {
packageManager, const suggestedProjectName = path.basename(projectDir);
); const projectName = await text({
message: "Enter a name for your Neon project:",
defaultValue: suggestedProjectName,
initialValue: suggestedProjectName,
});
if (!config) { const regionId = await select({
throw new Error( message: "Select a region for your Neon project:",
"Failed to create project - couldn't get connection information", options: NEON_REGIONS,
initialValue: NEON_REGIONS[0].value,
});
if (isCancel(projectName) || isCancel(regionId)) {
cancel(pc.red("Operation cancelled"));
process.exit(0);
}
const neonConfig = await createNeonProject(
projectName as string,
regionId,
packageManager,
); );
if (!neonConfig) {
throw new Error(
"Failed to create project - couldn't get connection information",
);
}
const finalSpinner = spinner();
finalSpinner.start("Configuring database connection");
await fs.ensureDir(path.join(projectDir, "apps/server"));
await writeEnvFile(projectDir, neonConfig);
finalSpinner.stop("Neon database configured!");
} }
const finalSpinner = spinner();
finalSpinner.start("Configuring database connection");
await fs.ensureDir(path.join(projectDir, "apps/server"));
await writeEnvFile(projectDir, config);
finalSpinner.stop("Neon database configured!");
} catch (error) { } catch (error) {
if (error instanceof Error) { if (error instanceof Error) {
consola.error(pc.red(error.message)); consola.error(pc.red(error.message));