-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/make config backwards compatible (#71)
* Add back utils file and work on parsing default config * Tidy up the connect function * Add URI backwards compatability test * Add a test for invalid config object * Update package to 8.2.1, improve default handling
- Loading branch information
Showing
6 changed files
with
238 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,39 @@ | ||
'use strict' | ||
|
||
/** | ||
* @function buildDefaultConfig | ||
* | ||
* @desciption Converts a deprecated Database Connection URI based config to the newer, Knex compatible version | ||
* | ||
* @returns {Object} - Knex compatible config | ||
*/ | ||
const buildDefaultConfig = (defaultConfig, configStr) => { | ||
const connectionRegex = /^(mysql|psql)(?::\/\/)(.*)(?::)(.*)@(.*)(?::)(\d*)(?:\/)(.*)$/ | ||
const matches = configStr.match(connectionRegex) | ||
|
||
// Check that we got the expected amount: 6 capture groups + 1 js defaults = 7 | ||
if (!matches || matches.length !== 7) { | ||
throw new Error(`Invalid database config string: ${configStr}`) | ||
} | ||
|
||
// skip the first, .match() puts the original string at index 0 | ||
matches.shift() | ||
const [clientStr, user, password, host, port, database] = matches | ||
|
||
return { | ||
...defaultConfig, | ||
client: clientStr, | ||
connection: { | ||
...defaultConfig.connection, | ||
user, | ||
password, | ||
host, | ||
port, | ||
database | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
buildDefaultConfig | ||
} |
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,114 @@ | ||
'use strict' | ||
|
||
const src = '../../src' | ||
const Test = require('tapes')(require('tape')) | ||
const Utils = require(`${src}/utils`) | ||
|
||
Test('utils', utilsTest => { | ||
utilsTest.test('buildDefaultConfig should', defaultTest => { | ||
defaultTest.test('handle a mysql connection string', test => { | ||
// Arrange | ||
const defaultConfig = { | ||
connection: { | ||
host: 'localhost', | ||
port: 3306 | ||
}, | ||
pool: { | ||
field1: true | ||
} | ||
} | ||
const URI = 'mysql://central_ledger:password@mysql-cl:3307/central_ledger_db' | ||
const expected = { | ||
client: 'mysql', | ||
connection: { | ||
host: 'mysql-cl', | ||
port: '3307', | ||
user: 'central_ledger', | ||
password: 'password', | ||
database: 'central_ledger_db' | ||
}, | ||
pool: { | ||
field1: true | ||
} | ||
} | ||
// Act | ||
const config = Utils.buildDefaultConfig(defaultConfig, URI) | ||
|
||
// Assert | ||
test.deepEqual(config, expected) | ||
test.end() | ||
}) | ||
|
||
defaultTest.test('not override all values in defaultConfig.connection', test => { | ||
// Arrange | ||
const defaultConfig = { | ||
connection: { | ||
newFieldNotOverriden: true | ||
}, | ||
pool: { | ||
field1: true | ||
} | ||
} | ||
const URI = 'mysql://central_ledger:password@mysql-cl:3307/central_ledger_db' | ||
const expected = { | ||
client: 'mysql', | ||
connection: { | ||
newFieldNotOverriden: true, | ||
host: 'mysql-cl', | ||
port: '3307', | ||
user: 'central_ledger', | ||
password: 'password', | ||
database: 'central_ledger_db' | ||
}, | ||
pool: { | ||
field1: true | ||
} | ||
} | ||
// Act | ||
const config = Utils.buildDefaultConfig(defaultConfig, URI) | ||
|
||
// Assert | ||
test.deepEqual(config, expected) | ||
test.end() | ||
}) | ||
|
||
defaultTest.test('fails when connections string does not use mysql or psql', test => { | ||
// Arrange | ||
const defaultConfig = {} | ||
const URI = 'mssql://central_ledger:password@mysql-cl:3307/central_ledger_db' | ||
|
||
// Act | ||
try { | ||
Utils.buildDefaultConfig(defaultConfig, URI) | ||
test.fail('Should have thrown error') | ||
} catch (err) { | ||
// Assert | ||
test.equal(err.message, 'Invalid database config string: mssql://central_ledger:password@mysql-cl:3307/central_ledger_db') | ||
} | ||
|
||
test.end() | ||
}) | ||
|
||
defaultTest.test('fail when the connection string is invalid', test => { | ||
// Arrange | ||
const defaultConfig = {} | ||
// Missing a `:` | ||
const URI = 'mssql://central_ledgerpassword@mysql-cl:3307/central_ledger_db' | ||
|
||
// Act | ||
try { | ||
Utils.buildDefaultConfig(defaultConfig, URI) | ||
test.fail('Should have thrown error') | ||
} catch (err) { | ||
// Assert | ||
test.equal(err.message, 'Invalid database config string: mssql://central_ledgerpassword@mysql-cl:3307/central_ledger_db') | ||
} | ||
|
||
test.end() | ||
}) | ||
|
||
defaultTest.end() | ||
}) | ||
|
||
utilsTest.end() | ||
}) |