fix(NODE-4186): accept ReadPreferenceLike in TransactionOptions type #3425
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This commit expands the TransactionOptions type definition to allow values like
'primary'
.What is changing?
Before, the type definition only allowed passing an instance of the
ReadPreference
class. This differs from documented and common usage. We expand the type definition fromReadPreference
toReadPreferenceLike
.Updating this type annotation will reduce confusion for Typescipt users of the library.
Is there new documentation needed for these changes?
No, this makes the types match the existing documentation more closely. For instance, in https://www.mongodb.com/docs/drivers/node/current/fundamentals/transactions/#transaction-settings, this API is documented as accepting a string:
What is the motivation for this change?
The documented code snippet above should not cause a Typescript error.
We can also see in https://github.com/mongodb/node-mongodb-native/blob/48fa588362/src/transactions.ts#L108 that the transaction constructor will pass its options through to
ReadPreference.fromOptions
, which will handle coercing a string into a ReadPreference instance, so the documentation appears to be correct while the type definition is lacking.Double check the following
npm run check:lint
script<type>(NODE-xxxx)<!>: <description>