* Construct a connection URL using nodejs's whatwg URL similar to how connection_string.ts * works * * @param options - overrides and settings for URI generation
(
options?: UrlOptions & {
useMultipleMongoses?: boolean;
db?: string;
replicaSet?: string;
proxyURIParams?: ProxyParams;
username?: string;
password?: string;
auth?: {
username?: string;
password?: string;
};
authSource?: string;
authMechanism?: string;
authMechanismProperties?: Record<string, any>;
}
)
| 331 | * @param options - overrides and settings for URI generation |
| 332 | */ |
| 333 | url( |
| 334 | options?: UrlOptions & { |
| 335 | useMultipleMongoses?: boolean; |
| 336 | db?: string; |
| 337 | replicaSet?: string; |
| 338 | proxyURIParams?: ProxyParams; |
| 339 | username?: string; |
| 340 | password?: string; |
| 341 | auth?: { |
| 342 | username?: string; |
| 343 | password?: string; |
| 344 | }; |
| 345 | authSource?: string; |
| 346 | authMechanism?: string; |
| 347 | authMechanismProperties?: Record<string, any>; |
| 348 | } |
| 349 | ) { |
| 350 | options = { |
| 351 | db: this.options.db, |
| 352 | replicaSet: this.options.replicaSet, |
| 353 | proxyURIParams: this.options.proxyURIParams, |
| 354 | ...options |
| 355 | }; |
| 356 | |
| 357 | const FILLER_HOST = 'fillerHost'; |
| 358 | |
| 359 | const protocol = 'mongodb'; |
| 360 | const url = new URL(`${protocol}://${FILLER_HOST}`); |
| 361 | |
| 362 | if (options.replicaSet) { |
| 363 | url.searchParams.append('replicaSet', options.replicaSet); |
| 364 | } |
| 365 | |
| 366 | if (options.proxyURIParams) { |
| 367 | for (const [name, value] of Object.entries(options.proxyURIParams)) { |
| 368 | if (value) { |
| 369 | url.searchParams.append(name, value); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | url.pathname = `/${options.db}`; |
| 375 | |
| 376 | const username = options.username || this.options.auth?.username; |
| 377 | const password = options.password || this.options.auth?.password; |
| 378 | |
| 379 | if (username) { |
| 380 | url.username = username; |
| 381 | } |
| 382 | |
| 383 | if (password) { |
| 384 | url.password = password; |
| 385 | } |
| 386 | |
| 387 | if (this.isLoadBalanced) { |
| 388 | url.searchParams.append('loadBalanced', 'true'); |
| 389 | } |
| 390 |