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
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"request": "launch",
"type": "node",
"name": "Run mocha",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": ["test"]
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ A small library that parses mssql connection string and returns database configu


## Tests

`npm install`
`npm test`

## Contributing
Expand Down
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ module.exports = function (connectionString) {
connectionString.split(';').forEach((x) => {
const arr = x.split('=');
if (arr[1]) {
result[arr[0]] = arr[1];
result[arr[0].toLowerCase()] = arr[1];
}
});

// extract host and port from 'Data Source'
let host;
let port;
const dataSource = result['Data Source'];
const dataSource = result['data source'];
if (dataSource) {
const regex = /.*:(.*),([0-9]+)/;
const match = regex.exec(dataSource);
Expand All @@ -30,12 +30,14 @@ module.exports = function (connectionString) {

// extract user from 'User Id'
let user;
const userId = result['User Id'];
const userId = result['user id'];
if (userId) {
const regex = /(.*)@.*/;
const match = regex.exec(userId);
if (match) {
user = match[1];
} else {
user = userId;
}
}

Expand All @@ -46,22 +48,22 @@ module.exports = function (connectionString) {
if (!user) {
throw new Error('User not found');
}
if (!result['Initial Catalog']) {
if (!result['initial catalog']) {
throw new Error('Database not found');
}
if (!result['Password']) {
if (!result['password']) {
throw new Error('Password not found');
}

return {
host,
options: {
database: result['Initial Catalog'],
database: result['initial catalog'],
encrypt: true,
port: port,
},
/* tslint:disable */
password: result['Password'],
password: result['password'],
/* tslint:enable */
user,
};
Expand Down
34 changes: 34 additions & 0 deletions test/knexSetup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,38 @@ describe('#knexSetup', () => {
const result = parser(connectionString);
expect(result).to.deep.equal(expectedSetup);
});

it('should be case agnostic to the keys in the connection string', () => {
const connectionString = 'Data souRCE=tcp: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 user that has no @', () => {
const connectionString = 'Data souRCE=tcp:database.com,1433;Initial CataloG=numbers;User ID=service;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);
});
});
8 changes: 0 additions & 8 deletions test/malformedConnectionString.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ describe('#malformedConnectionString', () => {
}).to.throw(Error);
});

it('should find missing user', () => {
const connectionString = 'Data Source=tcp:database.com,1433;Initial Catalog=numbers;User Id=database.com;Password=fjsflregewbfldsfhsew3;';

expect(() => {
parser(connectionString);
}).to.throw(Error);
});

it('should find missing "Initial Catalog"', () => {
const connectionString = 'Data Source=tcp:database.com,1433;Initial=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;';

Expand Down