@@ -133,6 +133,22 @@ function isReadPreferenceSet(connectionString: string): boolean {
133133 ) ;
134134}
135135
136+ function isReadPreferenceTagsSet ( connectionString : string ) : boolean {
137+ return ! ! new ConnectionStringUrl ( connectionString ) . searchParams . get (
138+ 'readPreferenceTags'
139+ ) ;
140+ }
141+
142+ function maybeOverrideReadPreference ( connectionString : string ) : {
143+ readPreference ?: 'secondaryPreferred' ;
144+ } {
145+ if ( isReadPreferenceTagsSet ( connectionString ) ) {
146+ return { readPreference : 'secondaryPreferred' } ;
147+ }
148+
149+ return { } ;
150+ }
151+
136152let id = 0 ;
137153
138154type ClientType = 'CRUD' | 'META' ;
@@ -1264,7 +1280,12 @@ class DataServiceImpl extends WithLogContext implements DataService {
12641280 try {
12651281 const cursor = this . _database ( databaseName , 'CRUD' ) . listCollections (
12661282 filter ,
1267- { nameOnly }
1283+ {
1284+ nameOnly,
1285+ ...maybeOverrideReadPreference (
1286+ this . _connectionOptions . connectionString
1287+ ) ,
1288+ }
12681289 ) ;
12691290 // Iterate instead of using .toArray() so we can emit
12701291 // collection info update events as they come in.
@@ -1399,7 +1420,12 @@ class DataServiceImpl extends WithLogContext implements DataService {
13991420 } as {
14001421 listDatabases : 1 ;
14011422 } ,
1402- { enableUtf8Validation : false }
1423+ {
1424+ enableUtf8Validation : false ,
1425+ ...maybeOverrideReadPreference (
1426+ this . _connectionOptions . connectionString
1427+ ) ,
1428+ }
14031429 ) ;
14041430 return databases . map ( ( x ) => ( {
14051431 ...x ,
@@ -2542,7 +2568,12 @@ class DataServiceImpl extends WithLogContext implements DataService {
25422568 const stats = await runCommand (
25432569 db ,
25442570 { dbStats : 1 } ,
2545- { enableUtf8Validation : false }
2571+ {
2572+ enableUtf8Validation : false ,
2573+ ...maybeOverrideReadPreference (
2574+ this . _connectionOptions . connectionString
2575+ ) ,
2576+ }
25462577 ) ;
25472578 const normalized = adaptDatabaseInfo ( stats ) ;
25482579 return { name, ...normalized } ;
0 commit comments