(db: string, command: Document, options: CommandOptions)
| 367 | } |
| 368 | |
| 369 | private prepareCommand(db: string, command: Document, options: CommandOptions) { |
| 370 | let cmd = { ...command }; |
| 371 | |
| 372 | const readPreference = getReadPreference(options); |
| 373 | const session = options?.session; |
| 374 | |
| 375 | let clusterTime = this.clusterTime; |
| 376 | |
| 377 | if (this.serverApi) { |
| 378 | const { version, strict, deprecationErrors } = this.serverApi; |
| 379 | cmd.apiVersion = version; |
| 380 | if (strict != null) cmd.apiStrict = strict; |
| 381 | if (deprecationErrors != null) cmd.apiDeprecationErrors = deprecationErrors; |
| 382 | } |
| 383 | |
| 384 | if (this.hasSessionSupport && session) { |
| 385 | if ( |
| 386 | session.clusterTime && |
| 387 | clusterTime && |
| 388 | session.clusterTime.clusterTime.greaterThan(clusterTime.clusterTime) |
| 389 | ) { |
| 390 | clusterTime = session.clusterTime; |
| 391 | } |
| 392 | |
| 393 | const sessionError = applySession(session, cmd, options); |
| 394 | if (sessionError) throw sessionError; |
| 395 | } else if (session?.explicit) { |
| 396 | throw new MongoCompatibilityError('Current topology does not support sessions'); |
| 397 | } |
| 398 | |
| 399 | // if we have a known cluster time, gossip it |
| 400 | if (clusterTime) { |
| 401 | cmd.$clusterTime = clusterTime; |
| 402 | } |
| 403 | |
| 404 | // For standalone, drivers MUST NOT set $readPreference. |
| 405 | if (this.description.type !== ServerType.Standalone) { |
| 406 | if ( |
| 407 | !isSharded(this) && |
| 408 | !this.description.loadBalanced && |
| 409 | this.supportsOpMsg && |
| 410 | options.directConnection === true && |
| 411 | readPreference?.mode === 'primary' |
| 412 | ) { |
| 413 | // For mongos and load balancers with 'primary' mode, drivers MUST NOT set $readPreference. |
| 414 | // For all other types with a direct connection, if the read preference is 'primary' |
| 415 | // (driver sets 'primary' as default if no read preference is configured), |
| 416 | // the $readPreference MUST be set to 'primaryPreferred' |
| 417 | // to ensure that any server type can handle the request. |
| 418 | cmd.$readPreference = ReadPreference.primaryPreferred.toJSON(); |
| 419 | } else if (isSharded(this) && !this.supportsOpMsg && readPreference?.mode !== 'primary') { |
| 420 | // When sending a read operation via OP_QUERY and the $readPreference modifier, |
| 421 | // the query MUST be provided using the $query modifier. |
| 422 | cmd = { |
| 423 | $query: cmd, |
| 424 | $readPreference: readPreference.toJSON() |
| 425 | }; |
| 426 | } else if (readPreference?.mode !== 'primary') { |
no test coverage detected