Skip to content

MongoDB options never parse in connectionUrl and after my fix was parse incorrect #7437

@gogotaro

Description

@gogotaro

Issue Description

My understanding original of "parseConnectionUrl" method never parse options from connectionUrl as below.

return obeject at parseConnectionUrl method (original)

return {
            type: type,
            host: host,
            username: decodeURIComponent(username),
            password: decodeURIComponent(password),
            port: port ? parseInt(port) : undefined,
            database: afterBase || undefined
        };

But I try to use replicaSet option in connectionUrl and I ever fix issue #7401 with PR #7402 by separate method "parseMongoDBConnectionUrl" in class "DriverUtils" to parse connection mongodb only as below.

return obeject at parseMongoDBConnectionUrl method (my fixed #7402)

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

It's working with option replicaSet only but another options wasn't working and parse incorrect.

PS. I know we can specified option key in connection object like { url: "mongodb://test-primary.example.com:27017/testdb", retryWrites: true, w: "majority", useUnifiedTopology: true} but some use case is inconvenience to do it.

Expected Behavior

My example connection string

mongodb://test-primary.example.com:27017/testdb?retryWrites=true&w=majority&useUnifiedTopology=true

I test by console.log(connectionString) at method "buildConnectionUrl" and console.log(mongoOptions) at method "buildConnectionOptions" in class "MongoDriver" and when connect was process result must be.

mongodb://test-primary.example.com:27017/testdb
{ retryWrites: 'true', w: 'majority', retryWrites: 'true' }

Actual Behavior

mongodb://test-primary.example.com:27017/testdb
{ replicaSet: 'true&w' }

As above my fix #7402 was make It parse incorrect. (Because I focus to parse replicaSet only, Sorry I'll fix It as fast as possible)

Steps to Reproduce

  1. Create connection typescript source code
createConnection({
    type: "mongodb",
    url: "mongodb://test-primary.example.com:27017/testdb?retryWrites=true&w=majority&useUnifiedTopology=true"
}).then(connection => { 

   console.log("TypeORM with MongoDB"); 
}).catch(error => console.log(error));
  1. console.log(connectionString) at method "buildConnectionUrl" and console.log(mongoOptions) at method "buildConnectionOptions" in class "MongoDriver"
  2. run these connection code.

My Environment

Dependency Version
Operating System Ubuntu 20.04
Node.js version v14.16.0
Typescript version ^v4.2.2
TypeORM version v0.2.31

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions