Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection strings are incorrectly parsed and overwritten by mongo db driver #8922

Open
samskiter opened this issue Apr 22, 2022 · 3 comments

Comments

@samskiter
Copy link

Relevant documentation: https://orkhan.gitbook.io/typeorm/docs/connection-options#mongodb-connection-options

Relevant quote:

url - Connection url where perform connection to. Please note that other connection options will override parameters set from url.

Issue 1:
Line of code where this seems not to be true:

return Object.assign({}, options, urlDriverOptions)

I think that this should be: return Object.assign({}, urlDriverOptions, options) in order for the implementation to match the docs. NOTE that this also seems to be the case for the other similar implementations across DriverUtils.ts

Issue 2:
When parsing the connection string, boolean values are pulled out as strings and not converted:

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

This is effectively interpreted as 'false' later on in the connection.

The combination of these two issues leads to a very hard to debug state. Which leads me on to:

Issue 3:
No type checking. There is a lot of use of any in the above code which makes these bugs harder to find - ideally the implementation would confirm type for options. For example here:

const options = DriverUtils.buildMongoDBDriverOptions(this.options)

In this file, the list of options are well known, and so should the types (I believe they are listed here)

@samskiter
Copy link
Author

Related: #6247 this was reverted for some reason

@sstone1
Copy link

sstone1 commented May 17, 2022

I've just hit this same issue - the url we specify gets most of its query parameters stripped out, and they are not passed to the underlying Mongo client library (or are passed as strings instead of booleans). This causes timeouts and connection closed issues when using the Mongo interface to access an Azure Cosmos DB instance.

I worked around it by patching the code at runtime to disable the URL processing logic in ormconfig.js:

const { DriverUtils } = require('typeorm/driver/DriverUtils');
const { MongoDriver } = require('typeorm/driver/mongodb/MongoDriver');

function patchDriverUtils() {
  DriverUtils.buildMongoDBDriverOptions = options => options;
}

function patchMongoDriver() {
  MongoDriver.prototype.buildConnectionUrl = () => MONGO_URL;
}

patchDriverUtils();
patchMongoDriver();

module.exports = {
  cli: {
    migrationsDir: 'src/migration',
  },
  database: MONGO_DATABASE,
  migrations: ['dist/migration/*.js'],
  type: 'mongodb',
  url: MONGO_URL,
  useNewUrlParser: true,
  useUnifiedTopology: true,
};

@kokushkin
Copy link

The last message from here #6247 was

It was reverted due to undefined values overwriting defined ones, but a PR to reimplement while addressing that issue would probably be accepted

As I understood the #6247 introduced a new buggy behavior ("undefined values overwriting defined ones"), while fixing the old bugs on which many people were relaying, back then (and maybe now ).

It seems not very hard to try to fix it again, anybody doing this?

As an alternative, I would suggest to change docs (delete "Please note that other data source options will override parameters set from url.").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@samskiter @sstone1 @kokushkin and others