Skip to content

Commit

Permalink
fix: correctly parse connection URI with query params (typeorm#6390)
Browse files Browse the repository at this point in the history
ref typeorm#6389

Co-authored-by: Coroliov Oleg <coroliov.o@goparrot.ai>
  • Loading branch information
2 people authored and Svetlozar committed Jan 12, 2021
1 parent 7ed4adb commit b1d6efa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/driver/DriverUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class DriverUtils {

/**
* Builds column alias from given alias name and column name.
*
*
* If alias length is greater than the limit (if any) allowed by the current
* driver, replaces it with a hashed string.
*
Expand Down Expand Up @@ -68,7 +68,11 @@ export class DriverUtils {
const preBase = url.substr(firstSlashes + 2);
const secondSlash = preBase.indexOf("/");
const base = (secondSlash !== -1) ? preBase.substr(0, secondSlash) : preBase;
const afterBase = (secondSlash !== -1) ? preBase.substr(secondSlash + 1) : undefined;
let afterBase = (secondSlash !== -1) ? preBase.substr(secondSlash + 1) : undefined;
// remove mongodb query params
if (afterBase && afterBase.indexOf("?") !== -1) {
afterBase = afterBase.substr(0, afterBase.indexOf("?"));
}

const lastAtSign = base.lastIndexOf("@");
const usernameAndPassword = base.substr(0, lastAtSign);
Expand Down
26 changes: 26 additions & 0 deletions test/github-issues/6389/issue-6389.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { DriverUtils } from "../../../src/driver/DriverUtils";
import { expect } from "chai";

describe("github issues > #6389 MongoDB URI Connection string with query params", () => {
it("should parse correctly mongodb URI", () => {
const obj: any = {
type: "mongodb",
username: "user",
password: "password",
host: "host",
database: "database",
port: 27017,
};

const url = `${obj.type}://${obj.username}:${obj.password}@${obj.host}:${obj.port}/${obj.database}?readPreference=primary`;
const options = DriverUtils.buildDriverOptions({url});

expect(options.type).to.eql(obj.type);
expect(options.username).to.eql(obj.username);
expect(options.username).to.eql(obj.username);
expect(options.password).to.eql(obj.password);
expect(options.host).to.eql(obj.host);
expect(options.port).to.eql(obj.port);
expect(options.database).to.eql(obj.database);
});
});

0 comments on commit b1d6efa

Please sign in to comment.