Skip to content

Add manual connection string setup option for Neon database #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/fair-streets-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-better-t-stack": minor
---

Add support for manual Neon setup with connection string
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ yarn-error.log*
*.pem
.vscode
.env*.local
.idea

.smoke
.smoke
32 changes: 21 additions & 11 deletions apps/cli/src/helpers/database-providers/neon-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ import {
type EnvVariable,
} from "../project-generation/env-setup";

type NeonConfig = {
connectionString: string;
projectId: string;
dbName: string;
roleName: string;
};

type NeonRegion = {
label: string;
value: string;
Expand Down Expand Up @@ -100,13 +93,13 @@ async function createNeonProject(
}
}

async function writeEnvFile(projectDir: string, config?: NeonConfig) {
async function writeEnvFile(projectDir: string, connectionString?: string) {
const envPath = path.join(projectDir, "apps/server", ".env");
const variables: EnvVariable[] = [
{
key: "DATABASE_URL",
value:
config?.connectionString ??
connectionString ??
"postgresql://postgres:postgres@localhost:5432/mydb?schema=public",
condition: true,
},
Expand Down Expand Up @@ -174,6 +167,11 @@ export async function setupNeonPostgres(config: ProjectConfig) {
value: "neonctl",
hint: "More control - choose project name and region",
},
{
label: "Manual setup",
value: "manual",
hint: "Enter connection string manually",
}
],
initialValue: "neondb",
});
Expand All @@ -182,7 +180,7 @@ export async function setupNeonPostgres(config: ProjectConfig) {

if (setupMethod === "neondb") {
await setupWithNeonDb(projectDir, packageManager);
} else {
} else if (setupMethod === 'neonctl') {
const suggestedProjectName = path.basename(projectDir);
const projectName = await text({
message: "Enter a name for your Neon project:",
Expand Down Expand Up @@ -215,9 +213,21 @@ export async function setupNeonPostgres(config: ProjectConfig) {
finalSpinner.start("Configuring database connection");

await fs.ensureDir(path.join(projectDir, "apps/server"));
await writeEnvFile(projectDir, neonConfig);
await writeEnvFile(projectDir, neonConfig.connectionString);

finalSpinner.stop("Neon database configured!");
} else if (setupMethod === "manual") {
const connectionString = await text({
message: "Enter your Neon connection string:",
validate(value) {
if (!value) return "Please enter a connection string";
},
});

if (isCancel(connectionString)) return exitCancelled("Operation cancelled");

await fs.ensureDir(path.join(projectDir, "apps/server"));
await writeEnvFile(projectDir, connectionString);
}
} catch (error) {
if (error instanceof Error) {
Expand Down