Skip to content

Commit

Permalink
fix: support MongoDB DNS seed list connection (#7136)
Browse files Browse the repository at this point in the history
As described in [MongoDB Docs](https://docs.mongodb.com/manual/reference/connection-string/#dns-seed-list-connection-format), an additional connection string format called DNS Seed List Connection format can be used. As this is the default format for MongoDB Atlas, the hosted service of MongoDB, this should be available also for typeorm. 
The connection format is identified by the url-schema "mongodb+srv" and does not allow specifying a port.
Fixes #3347
Fixes #3133
  • Loading branch information
nocheintobi committed Dec 22, 2020
1 parent f27622d commit f730bb9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/driver/mongodb/MongoDriver.ts
Expand Up @@ -438,11 +438,15 @@ export class MongoDriver implements Driver {
* Builds connection url that is passed to underlying driver to perform connection to the mongodb database.
*/
protected buildConnectionUrl(options: { [key: string]: any }): string {
const credentialsUrlPart = (options.username && options.password)
const schemaUrlPart = options.type.toLowerCase();
const credentialsUrlPart = (options.username && options.password)
? `${options.username}:${options.password}@`
: "";
const portUrlPart = (schemaUrlPart === "mongodb+srv")
? ""
: `:${options.port || "27017"}`;

return `mongodb://${credentialsUrlPart}${options.host || "127.0.0.1"}:${options.port || "27017"}/${options.database || ""}`;
return `${schemaUrlPart}://${credentialsUrlPart}${options.host || "127.0.0.1"}${portUrlPart}/${options.database || ""}`;
}

/**
Expand Down

0 comments on commit f730bb9

Please sign in to comment.