-
Notifications
You must be signed in to change notification settings - Fork 23
feat: Add mongodb v5 support with strict filter types #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,3 +190,28 @@ const postSchema = schema( | |
| } | ||
| ); | ||
| ``` | ||
|
|
||
| ## Filter Types | ||
|
|
||
| `mongodb` exposes two important TypeScript types for the most common operations you can do with the driver: the `Filter` (used for querying documents) and `UpdateFilter` (used for updating documents) types. | ||
|
|
||
| Starting with `mongodb` v4.3.0, these types were enhanced to support dot notation field access and type checking. | ||
|
|
||
| However, `mongodb` v5.0.0 [removed these types](https://github.com/mongodb/node-mongodb-native/blob/main/etc/notes/CHANGES_5.0.0.md#dot-notation-typescript-support-removed-by-default) as the default ones used in their methods and reverted to the old ones without dot notation support. The previous enhanced types were not removed, instead they were renamed to `StrictFilter` and `StrictUpdateFilter`, but they aren't referenced in any of their methods. | ||
|
|
||
| Papr is using the strict types to provide type safety for all query and update filters. | ||
|
|
||
| This comes with a caveat: whenever you need to interact with the `mongodb` driver collections, you need to cast filter types to their simple counterparts, since `Filter` is not compatible with `StrictFilter`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bit of the docs is very nicely written, well done! |
||
|
|
||
| ```ts | ||
| import { Filter, StrictFitler } from 'mongodb'; | ||
| import User, { UserDocument } from './user'; | ||
|
|
||
| const filter: StrictFilter<UserDocument> = { | ||
| firstName: 'John', | ||
| }; | ||
|
|
||
| await User.find(filter); | ||
|
|
||
| await User.collection.find(filter as Filter<UserDocument>); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,7 +78,7 @@ | |
| "jsdoc-api": "8.0.0", | ||
| "jsdoc-parse": "6.2.0", | ||
| "lint-staged": "13.1.0", | ||
| "mongodb": "4.13.0", | ||
| "mongodb": "5.0.1", | ||
| "mongodb-memory-server": "8.11.0", | ||
| "mongoose": "6.3.5", | ||
| "pinst": "3.0.0", | ||
|
|
@@ -89,7 +89,7 @@ | |
| "typescript": "4.9.3" | ||
| }, | ||
| "peerDependencies": { | ||
| "mongodb": ">=4.11.0" | ||
| "mongodb": "^5.0.0" | ||
| }, | ||
| "commitlint": { | ||
| "extends": [ | ||
|
|
@@ -125,4 +125,4 @@ | |
| "yarn": "1.22.19" | ||
| }, | ||
| "packageManager": "[email protected]" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some weird reason, the ESLint TS plugin now thinks the type of the update filter variables are
any, so let's temporarily downgrade these to warning.