forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pg-connection-string): ClientConfig helper functions
Two new functions are introduced to make it easy for TypeScript users to use a PostgresSQL connection string with pg Client. Fixes brianc#2280
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const expect = chai.expect | ||
chai.should() | ||
|
||
const { parse, toClientConfig } = require('../') | ||
|
||
describe('toClientConfig', function () { | ||
it('converts connection info', function () { | ||
const config = parse('postgres://brian:pw@boom:381/lala') | ||
const clientConfig = toClientConfig(config) | ||
|
||
clientConfig.user.should.equal('brian') | ||
clientConfig.password.should.equal('pw') | ||
clientConfig.host.should.equal('boom') | ||
clientConfig.port.should.equal(381) | ||
clientConfig.database.should.equal('lala') | ||
}) | ||
|
||
it('converts query params', function () { | ||
const config = parse( | ||
'postgres:///?application_name=TheApp&fallback_application_name=TheAppFallback&client_encoding=utf8&options=-c geqo=off' | ||
) | ||
const clientConfig = toClientConfig(config) | ||
|
||
clientConfig.application_name.should.equal('TheApp') | ||
clientConfig.fallback_application_name.should.equal('TheAppFallback') | ||
clientConfig.client_encoding.should.equal('utf8') | ||
clientConfig.options.should.equal('-c geqo=off') | ||
}) | ||
|
||
it('converts SSL boolean', function () { | ||
const config = parse('pg:///?ssl=true') | ||
const clientConfig = toClientConfig(config) | ||
|
||
clientConfig.ssl.should.equal(true) | ||
}) | ||
|
||
it('converts sslmode=disable', function () { | ||
const config = parse('pg:///?sslmode=disable') | ||
const clientConfig = toClientConfig(config) | ||
|
||
clientConfig.ssl.should.equal(false) | ||
}) | ||
|
||
it('converts sslmode=noverify', function () { | ||
const config = parse('pg:///?sslmode=no-verify') | ||
const clientConfig = toClientConfig(config) | ||
|
||
clientConfig.ssl.rejectUnauthorized.should.equal(false) | ||
}) | ||
|
||
it('converts other sslmode options', function () { | ||
const config = parse('pg:///?sslmode=verify-ca') | ||
const clientConfig = toClientConfig(config) | ||
|
||
clientConfig.ssl.should.deep.equal({}) | ||
}) | ||
|
||
it('converts other sslmode options', function () { | ||
const config = parse('pg:///?sslmode=verify-ca') | ||
const clientConfig = toClientConfig(config) | ||
|
||
clientConfig.ssl.should.deep.equal({}) | ||
}) | ||
|
||
it('converts ssl cert options', function () { | ||
const connectionString = | ||
'pg:///?sslcert=' + | ||
__dirname + | ||
'/example.cert&sslkey=' + | ||
__dirname + | ||
'/example.key&sslrootcert=' + | ||
__dirname + | ||
'/example.ca' | ||
const config = parse(connectionString) | ||
const clientConfig = toClientConfig(config) | ||
|
||
clientConfig.ssl.should.deep.equal({ | ||
ca: 'example ca\n', | ||
cert: 'example cert\n', | ||
key: 'example key\n', | ||
}) | ||
}) | ||
|
||
it('converts unix domain sockets', function () { | ||
const config = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus') | ||
const clientConfig = toClientConfig(config) | ||
clientConfig.host.should.equal('/some path/') | ||
clientConfig.database.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"') | ||
clientConfig.client_encoding.should.equal('utf8') | ||
}) | ||
}) |