Skip to content

Add --json flag to db connect - #68

Merged
TrivCodez merged 3 commits into
IN3PIRE:mainfrom
SankeerthNara:feat/connect-json-flag
Jul 22, 2026
Merged

TrivCodez merged 3 commits into
IN3PIRE:mainfrom
SankeerthNara:feat/connect-json-flag

Conversation

@SankeerthNara

Copy link
Copy Markdown
Contributor

Summary

Adds a --json flag to db connect, following the same pattern already used in db branch inspect --json.

Fixes #60

Changes

src/commands/connect.ts

  • Added .option("--json", "Output as JSON")
  • When set, outputs { connectionString, branch, pooled, host, port } instead of the plain-text/spinner output
  • host/port are derived by parsing the connection string with new URL(), since getConnectionString() only returns a single string

src/lib/neon-api.ts (pre-existing bug fixes, found while testing)

  • parent_lsn / parent_timestamp on branches were typed as required-or-null in the Zod schema, but the real API omits these fields entirely for branches with no parent (e.g. the root/default branch) rather than sending null. This broke listBranches — and by extension branch list, branch inspect, and connect — for any project whose default branch has no parent. Made both fields .optional().
  • endpoint.port was required as a number, but the real API doesn't always return it. Made it optional and defaulted to 5432 (standard Postgres port) in getConnectionString, matching the existing fallback already used for pooled connections.

src/commands/endpoint.ts

  • Updated endpoint list table rendering to handle the now-optional port field (defaults to 5432 for display).

Why the schema fixes are included here

Both were blocking issues discovered while testing connect --json against a real project — listBranches/getConnectionString would throw a Zod validation error before the --json code path ever ran. They're narrow, single-purpose fixes (no new API calls, no behavior change beyond accepting valid API responses that were previously rejected), so I kept them in this PR rather than opening a separate one. Happy to split them out if preferred.

Testing

Tested against a live Neon project:

db connect production --json
db connect production --pooled --json

Both produce valid JSON with correct branch and pooled values, and pooled/non-pooled requests return distinct hosts as expected.

npm run build passes with no errors.

Not in scope (follow-up needed)

While testing, I also found that connection strings generated by getConnectionString() (used by connect, shell, query, seed, ci, reset, export, and branch diff/merge) embed a literal undefined password, because listRoles() doesn't return role passwords — Neon's API requires a separate reveal_password call per role. This is a larger, higher-severity issue affecting many commands, so I'm opening it separately rather than folding it into this PR.

- Add --json option to output connectionString, branch, pooled, host, port as JSON
- Fix Zod schema: parent_lsn/parent_timestamp on branches were required but the
  Neon API omits them (not null) for branches with no parent, causing validation
  to fail on every branch list/inspect call
- Fix Zod schema: endpoint.port was required but the Neon API doesn't always
  return it; default to 5432 (standard Postgres port) when absent

Fixes IN3PIRE#60

@TrivCodez TrivCodez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean implementation. Zod schema fixes (optional parent_lsn/parent_timestamp, optional port) are necessary and correct. Suggestion: port in JSON output emits null when absent — consider defaulting to 5432 for consistency with the existing pooled fallback in getConnectionString(). Not a blocker. Good to merge.

@TrivCodez
TrivCodez merged commit c514fa0 into IN3PIRE:main Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --json flag to db connect

2 participants