Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/charmander/libpq-connection-string
libpq-compatible connection string parser
https://github.com/charmander/libpq-connection-string
connection-string libpq node-postgres pg postgresql
Last synced: about 1 month ago
JSON representation
libpq-compatible connection string parser
- Host: GitHub
- URL: https://github.com/charmander/libpq-connection-string
- Owner: charmander
- License: isc
- Created: 2020-05-17T19:54:36.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-12T07:43:40.000Z (3 months ago)
- Last Synced: 2024-10-14T03:42:07.063Z (3 months ago)
- Topics: connection-string, libpq, node-postgres, pg, postgresql
- Language: JavaScript
- Homepage:
- Size: 102 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
A libpq-compatible[^1] connection string parser, like [`PQconninfoParse`][PQconninfoParse].
[^1]: when the connection string is normally encoded as UTF-8 and the C locale is used for `isspace`
```js
import parseConnectionString from 'libpq-connection-string';parseConnectionString('postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp')
// { … dbname: 'somedb', host: 'host1,host2', port: '123,456' … }parseConnectionString('host=localhost port=5432 dbname=mydb connect_timeout=10')
// { … connect_timeout: '10', dbname: 'mydb', host: 'localhost', port: '5432' … }
```All provided option values are parsed as strings, and all non-provided option values are `null`.
## Service files
libpq-connection-string/add-service-defaults-sync is a function to read [service configuration][pgservice] synchronously.
[LDAP][ldap] isn’t supported.
## Environment defaults
libpq-connection-string/add-env-defaults is a function to add libpq defaults (excluding those that come from service files) to parsed options. It reads from:
- environment variables
- the current user’s username, if a user isn’t otherwise specified
- a static set of fallback defaults## Examples
Getting the defaults that `PQconnect` would use:
```js
import parseConnectionString from 'libpq-connection-string';
import addServiceDefaultsSync from 'libpq-connection-string/add-service-defaults-sync';
import addEnvDefaults from 'libpq-connection-string/add-env-defaults';const options = parseConnectionString('host=/run/postgres');
addServiceDefaultsSync(options);
addEnvDefaults(options);
```[PQconninfoParse]: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNINFOPARSE
[pgservice]: https://www.postgresql.org/docs/current/libpq-pgservice.html
[ldap]: https://www.postgresql.org/docs/current/libpq-ldap.html