Skip to content
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

Add support for options parameter in connection URLs #547

Closed
thecodeboss opened this issue Mar 21, 2024 · 2 comments · Fixed by #551
Closed

Add support for options parameter in connection URLs #547

thecodeboss opened this issue Mar 21, 2024 · 2 comments · Fixed by #551
Labels
accepted-proposal The change proposal is accepted and contributions are welcome. enhancement released

Comments

@thecodeboss
Copy link
Contributor

Add support for options parameter in connection URLs

Desired Behavior

options added to a connection url get passed to the underlying pg library. For example:

postgres://...?options=-c search_path=test

(I'm omitting proper url-encoding to make the example clear!)

Motivation

We use different database schemas for various environments, especially locally. For our main app, we generally use the public schema, but for running tests we use the test schema.

A workaround has been setting the PGOPTIONS=-c search_path=test environment variable when running tests.

However, now we need to connect to a different database that uses another convention for schemas. Specifically, the test schema name looks like default$test instead of test. It's not possible to set the PGOPTIONS environment variable to two different settings, so ideally we could set options in the connection URL for each database that we connect to.

Note that we are able to work around this by specifying a schema within our queries, for example:

SELECT * FROM ${sql.identifier(['myschema', 'mytable'])}

but that syntax is a lot more verbose compared to simply writing

SELECT * FROM mytable

Thus there is no big rush to implement this, would just be nice to have.

Implementation

In the parseDsn utility, we could extract the options like so (maybe with a more specific name than options):

  const {
    application_name: applicationName,
    options,
    sslmode: sslMode,
    ...unsupportedOptions
  } = Object.fromEntries(url.searchParams);

  if (options) {
    connectionOptions.options = options;
  }

The underlying pg library already supports this parameter thanks to this PR from 2020: brianc/node-postgres#2216

Beyond that, some types would need updating and the param needs to be passed around in the correct places, but I don't anticipate it being a difficult thing to support.

@gajus
Copy link
Owner

gajus commented Mar 22, 2024

It is not something I have a use case for, so not something I will prioritize/work on, but contributions are welcome.

@gajus gajus added the accepted-proposal The change proposal is accepted and contributions are welcome. label Mar 22, 2024
Copy link

🎉 This issue has been resolved in version 37.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted-proposal The change proposal is accepted and contributions are welcome. enhancement released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants