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 a helper function to construct connection URI #45

Closed
gajus opened this issue Mar 14, 2019 · 6 comments
Closed

Add a helper function to construct connection URI #45

gajus opened this issue Mar 14, 2019 · 6 comments

Comments

@gajus
Copy link
Owner

gajus commented Mar 14, 2019

Connection URI is the most convenient way to store database configuration. However, as witnessed by submitted examples, others use object-configuration. The latter requires to manually construct connection URI, e.g.

const connectString = `postgresql://${encodeURIComponent(config.user)}:${encodeURIComponent(config.password)}@${config.host}:${config.port}/${config.database}`

#43 (comment)

This would obscure problems such as missing part of configuration until late in execution.

A solution would be a simple utility to construct the connection string and warn about missing fragments of the configuration.

@vitaly-t
Copy link

There is connection-string for that 😉

But there are a few reasons why this should be supported properly by the low-level driver. Too bad it is going nowhere with this. For example, see this issue.

@gajus
Copy link
Owner Author

gajus commented Mar 15, 2019

connection-string looks great.

Though this issue describes the inverse:

Take:

{ protocol: 'protocol', user: 'user', password: 'password', hosts: [ {name: 'host1', port: 123, isIPv6: false}, {name: 'abcd::', port: 456, isIPv6: true} ], path: ['one', 'two'], params: { p1: 'val1', msg: 'hello world!' } }

input and convert it to:

protocol://user:password@host1:123,[abcd::]:456/one/two?p1=val1&msg=hello+world!

I want Slonik to accept only one format for defining the connection settings and connection URI makes the most sense (easy to port configuration). But given the popular use of the object configuration, library should provide a way to at least create a connection URI using an object.

Your library does this here:
https://github.com/vitaly-t/connection-string/blob/d2d69897fc16f4d2ab081f1c749fafbbad6e1086/src/index.js#L149-L190

I have not checked if this works, but I guess this would work:

(configuration): string => {
  const a = new ConnectionString('';
  a.setDefaults(configuration);
  return a.toString();
};

@vitaly-t
Copy link

vitaly-t commented Mar 15, 2019

You can convert that object back into string, using toString() on the object, and you can create a new object via defaults, using setDefaults, and then again convert it into string, if you want. It is all very flexible.

@gajus
Copy link
Owner Author

gajus commented Nov 15, 2021

Added utilities parseDsn and stringifyDsn

6c3b178

@gajus gajus closed this as completed Nov 15, 2021
@dbousamra
Copy link

Hi @gajus. How do I connect to socket urls? parseDsn seems to throw an error

socket://username:password@/cloudsql/project-id:australia-southeast1:some-database?db=dbName

@gajus
Copy link
Owner Author

gajus commented Nov 23, 2021

@dbousamra Currently not supported, but would be a simple contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants