Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand All @@ -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');
Expand All @@ -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;
};
50 changes: 50 additions & 0 deletions test/knexSetup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
26 changes: 22 additions & 4 deletions test/malformedConnectionString.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,48 @@ 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;';

expect(() => {
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;';

Expand Down