* @param mode - A string describing the read preference mode (primary|primaryPreferred|secondary|secondaryPreferred|nearest) * @param tags - A tag set used to target reads to members with the specified tag(s). tagSet is not available if using read preference mode primary. * @param options - Ad
(mode: ReadPreferenceMode, tags?: TagSet[], options?: ReadPreferenceOptions)
| 82 | * @param options - Additional read preference options |
| 83 | */ |
| 84 | constructor(mode: ReadPreferenceMode, tags?: TagSet[], options?: ReadPreferenceOptions) { |
| 85 | if (!ReadPreference.isValid(mode)) { |
| 86 | throw new MongoInvalidArgumentError(`Invalid read preference mode ${JSON.stringify(mode)}`); |
| 87 | } |
| 88 | if (options == null && typeof tags === 'object' && !Array.isArray(tags)) { |
| 89 | options = tags; |
| 90 | tags = undefined; |
| 91 | } else if (tags && !Array.isArray(tags)) { |
| 92 | throw new MongoInvalidArgumentError('ReadPreference tags must be an array'); |
| 93 | } |
| 94 | |
| 95 | this.mode = mode; |
| 96 | this.tags = tags; |
| 97 | this.hedge = options?.hedge; |
| 98 | this.maxStalenessSeconds = undefined; |
| 99 | |
| 100 | options = options ?? {}; |
| 101 | if (options.maxStalenessSeconds != null) { |
| 102 | if (options.maxStalenessSeconds <= 0) { |
| 103 | throw new MongoInvalidArgumentError('maxStalenessSeconds must be a positive integer'); |
| 104 | } |
| 105 | |
| 106 | this.maxStalenessSeconds = options.maxStalenessSeconds; |
| 107 | } |
| 108 | |
| 109 | if (this.mode === ReadPreference.PRIMARY) { |
| 110 | if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) { |
| 111 | throw new MongoInvalidArgumentError('Primary read preference cannot be combined with tags'); |
| 112 | } |
| 113 | |
| 114 | if (this.maxStalenessSeconds) { |
| 115 | throw new MongoInvalidArgumentError( |
| 116 | 'Primary read preference cannot be combined with maxStalenessSeconds' |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | if (this.hedge) { |
| 121 | throw new MongoInvalidArgumentError( |
| 122 | 'Primary read preference cannot be combined with hedge' |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Support the deprecated `preference` property introduced in the porcelain layer |
| 129 | get preference(): ReadPreferenceMode { |