diff --git a/index.js b/index.js index 4ea19d4..324b1c1 100644 --- a/index.js +++ b/index.js @@ -20,11 +20,30 @@ module.exports = function (connectionString) { let port; const dataSource = result['data source']; if (dataSource) { - const regex = /.*:(.*),([0-9]+)/; - const match = regex.exec(dataSource); - if (match) { - host = match[1]; - port = match[2]; + const regexFull = /.*:(.*)/; + const regexHostPort = /(.*),([0-9]+)/; + const matchFull = regexFull.exec(dataSource); + + if (matchFull) { + const matchHostPort1 = regexHostPort.exec(matchFull[1]); + if (matchHostPort1) { + host = matchHostPort1[1]; + port = matchHostPort1[2]; + } + else if (isNaN(matchFull[1])) { + + host = matchFull[1]; + } + } + else{ + const matchHostPort2 = regexHostPort.exec(dataSource); + if (matchHostPort2) { + host = matchHostPort2[1]; + port = matchHostPort2[2]; + } + else if (isNaN(dataSource)) { + host = dataSource; + } } } @@ -42,8 +61,8 @@ module.exports = function (connectionString) { } // check if all data was found - if (!port || !host) { - throw new Error('Port or host not found'); + if (!host) { + throw new Error('Host not found'); } if (!user) { throw new Error('User not found'); @@ -55,16 +74,21 @@ module.exports = function (connectionString) { throw new Error('Password not found'); } - return { + var config = { host, options: { database: result['initial catalog'], - encrypt: true, - port: port, + encrypt: true }, /* tslint:disable */ password: result['password'], /* tslint:enable */ user, }; + + if (port){ + config.options.port = port; + } + + return config; }; diff --git a/test/knexSetup.test.js b/test/knexSetup.test.js index 7fb47d9..94355b7 100644 --- a/test/knexSetup.test.js +++ b/test/knexSetup.test.js @@ -52,4 +52,54 @@ describe('#knexSetup', () => { const result = parser(connectionString); expect(result).to.deep.equal(expectedSetup); }); + + it('should return proper config when protocol is missing', () => { + const connectionString = 'Data Source=database.com,1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + const expectedSetup = { + 'host': 'database.com', + 'options': { + 'database': 'numbers', + 'encrypt': true, + 'port': '1433', + }, + 'password': 'fjsflregewbfldsfhsew3', + 'user': 'service', + }; + + const result = parser(connectionString); + expect(result).to.deep.equal(expectedSetup); + }); + + it('should return proper config when port is missing', () => { + const connectionString = 'Data Source=tcp:database.com;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + const expectedSetup = { + 'host': 'database.com', + 'options': { + 'database': 'numbers', + 'encrypt': true, + }, + 'password': 'fjsflregewbfldsfhsew3', + 'user': 'service', + }; + + const result = parser(connectionString); + expect(result).to.deep.equal(expectedSetup); + }); + + it('should return proper config when protocol and port are missing', () => { + const connectionString = 'Data Source=database.com;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + const expectedSetup = { + 'host': 'database.com', + 'options': { + 'database': 'numbers', + 'encrypt': true, + }, + 'password': 'fjsflregewbfldsfhsew3', + 'user': 'service', + }; + + const result = parser(connectionString); + expect(result).to.deep.equal(expectedSetup); + }); + }); diff --git a/test/malformedConnectionString.test.js b/test/malformedConnectionString.test.js index a1413e9..02d624a 100644 --- a/test/malformedConnectionString.test.js +++ b/test/malformedConnectionString.test.js @@ -2,22 +2,41 @@ const expect = require('chai').expect; const parser = require('../index'); describe('#malformedConnectionString', () => { - it('should find missing port', () => { + it('should allow missing port', () => { const connectionString = 'Data Source=tcp:database.com;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; expect(() => { parser(connectionString); - }).to.throw(Error); + }).to.not.throw(Error); }); + it('should allow missing protocol', () => { + const connectionString = 'Data Source=database.com,1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; - it('should find missing host', () => { + expect(() => { + parser(connectionString); + }).to.not.throw(Error); + }); + it('should allow missing port and protocol', () => { + const connectionString = 'Data Source=database.com;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + + expect(() => { + parser(connectionString); + }).to.not.throw(Error); + }); + it('should find missing host with protocol', () => { const connectionString = 'Data Source=tcp:1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; expect(() => { parser(connectionString); }).to.throw(Error); }); + it('should find missing host without protocol', () => { + const connectionString = 'Data Source=1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + expect(() => { + parser(connectionString); + }).to.throw(Error); + }); it('should find missing "Data Source"', () => { const connectionString = 'Data=tcp:database.com,1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; @@ -25,7 +44,6 @@ describe('#malformedConnectionString', () => { parser(connectionString); }).to.throw(Error); }); - it('should find missing "User Id"', () => { const connectionString = 'Data Source=tcp:database.com,1433;Initial Catalog=numbers;User=service@database.com;Password=fjsflregewbfldsfhsew3;';