Skip to content

Commit

Permalink
fix: mongodb connectionURL parse options (#7560)
Browse files Browse the repository at this point in the history
  • Loading branch information
gogotaro committed Apr 17, 2021
1 parent acc7994 commit b2ac41a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/driver/DriverUtils.ts
Expand Up @@ -149,12 +149,27 @@ export class DriverUtils {
let port = undefined;
let hostReplicaSet = undefined;
let replicaSet = undefined;
// remove mongodb query params

let optionsObject: any = {};

if (afterBase && afterBase.indexOf("?") !== -1) {
// split params to get replica set

// split params
afterQuestionMark = afterBase.substr((afterBase.indexOf("?") + 1), afterBase.length);
replicaSet = afterQuestionMark.split("=")[1];

const optionsList = afterQuestionMark.split("&");
let optionKey: string;
let optionValue: string;

// create optionsObject for merge with connectionUrl object before return
optionsList.forEach(optionItem => {
optionKey = optionItem.split("=")[0];
optionValue = optionItem.split("=")[1];
optionsObject[optionKey] = optionValue;
});

// specific replicaSet value to set options about hostReplicaSet
replicaSet = optionsObject["replicaSet"];
afterBase = afterBase.substr(0, afterBase.indexOf("?"));
}

Expand All @@ -170,21 +185,28 @@ export class DriverUtils {
password = usernameAndPassword.substr(firstColon + 1);
}

// If replicaSet have value set It as hostlist, If not set like standalone host
if (replicaSet) {
hostReplicaSet = hostAndPort;
} else {
[host, port] = hostAndPort.split(":");
}

return {
let connectionUrl: any = {
type: type,
host: host,
hostReplicaSet: hostReplicaSet,
username: decodeURIComponent(username),
password: decodeURIComponent(password),
port: port ? parseInt(port) : undefined,
database: afterBase || undefined,
replicaSet: replicaSet || undefined
database: afterBase || undefined
};

// Loop to set every options in connectionUrl to object
for (const [key, value] of Object.entries(optionsObject)) {
connectionUrl[key] = value;
}

return connectionUrl;
}
}
18 changes: 18 additions & 0 deletions test/github-issues/7401/issue-7401.ts
@@ -0,0 +1,18 @@
import "reflect-metadata";
import { DriverUtils } from "../../../src/driver/DriverUtils"
import { expect } from "chai";

describe("github issues > #7401 MongoDB replica set connection string not support with method \"parseConnectionUrl\" & \"buildConnectionUrl\"", () => {

it("should parse replicaSet and host list in ConnectionUrl", () => {

var options = DriverUtils.buildMongoDBDriverOptions({url: "mongodb://testuser:testpwd@test-primary.example.com:27017,test-secondary-1.example.com:27017,test-secondary-2.example.com:27017/testdb?replicaSet=testreplicaset"})

expect((options.hostReplicaSet ? options.hostReplicaSet as string : '')).to.equal('test-primary.example.com:27017,test-secondary-1.example.com:27017,test-secondary-2.example.com:27017');
expect((options.username ? options.username as string : '')).to.equal('testuser');
expect((options.password ? options.password as string : '')).to.equal('testpwd');
expect((options.database ? options.database as string : '')).to.equal('testdb');
expect((options.replicaSet ? options.replicaSet as string : '')).to.equal('testreplicaset');
});

});
22 changes: 22 additions & 0 deletions test/github-issues/7437/issue-7437.ts
@@ -0,0 +1,22 @@
import "reflect-metadata";
import { DriverUtils } from "../../../src/driver/DriverUtils"
import { expect } from "chai";

describe("github issues > #7437 MongoDB options never parse in connectionUrl and after my fix was parse incorrect", () => {

it("should parse options in ConnectionUrl", () => {

var options = DriverUtils.buildMongoDBDriverOptions({url: "mongodb://testuser:testpwd@test-primary.example.com:27017/testdb?retryWrites=true&w=majority&useUnifiedTopology=true"})

expect((options.host ? options.host as string : '')).to.equal('test-primary.example.com');
expect((options.username ? options.username as string : '')).to.equal('testuser');
expect((options.password ? options.password as string : '')).to.equal('testpwd');
expect((options.port ? options.port as number : 0)).to.equal(27017);
expect((options.database ? options.database as string : '')).to.equal('testdb');

expect((options.retryWrites ? options.retryWrites as string : '')).to.equal('true');
expect((options.w ? options.w as string : '')).to.equal('majority');
expect((options.useUnifiedTopology ? options.useUnifiedTopology as string : '')).to.equal('true');
});

});

0 comments on commit b2ac41a

Please sign in to comment.