-
Notifications
You must be signed in to change notification settings - Fork 23
feat: Support excluding fields in ProjectionType #360
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
feat: Support excluding fields in ProjectionType #360
Conversation
|
We appreciate your time on opening this PR and writing all the tests. We do have some hesitation on introduction the requirement that all projections should be defined If you could refactor the code to work without this requirement, it'd make accepting this PR much easier. |
|
Hi @avaly Thanks for your comment. Although the original problem was suppressing _id, the current change goes further and provides a better alignment with MongoDB projections. In MongoDB, if you want to get all the fields except one, you can put that field with 0 and then the operation will return all fields except the one you don't want. In this way, you don't need to make complex projections or worry about changes in the schema in the future. For me, having this alignment with MongoDB definition is quite interesting and something we should aim for. About the breaking change, I was playing a bit more with the types and I could find a good solution. If developers doesn't put I think this solution brings the best of both worlds. I would like to include some doc about the new projections way if I have your approval. |
504f653 to
f69d2bd
Compare
avaly
left a comment
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.
This looks much better! Thanks for the changes and keeping backwards compatibility with the old non-const projections.
We'll definitely need a detailed section about this in our docs. Maybe it fits in the ProjectionType section, since we link to that from all the find* methods.
src/utils.ts
Outdated
| type FilterKeys<T, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T]; | ||
| type FilterProperties<T, U> = { [K in FilterKeys<T, U>]: T[K] }; |
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.
Please use longer names for the generic types here. And add some newlines between each type.
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.
Done!
|
@avaly Please check my latest changes. I added also the documentation. If everything is ok, please go ahead. :) Have you had time to test it and see it in live? |
avaly
left a comment
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.
This looks great! Thanks for all the work and taking our feedback!
I'll test this internally and merge this after the holidays period.
|
@CarlosLozanoHealthCaters I just got around to trying this on our private repository and I ran into a blocker: Any ideas on workarounds? We're using |
|
@avaly Don't know why that error is being thrown in your code. I've just run the script 'test:types' from the package.json with I have also tested the new papr version in our product and it's also compiling and checking all the types correctly. Could it be caused by other piece of code? |
No. Unfortunately, this is blocker for us and won't be able to merge this as is. I'll try to debug this in our repo when I get a chance, but I can't give you any timeline. |
|
@avaly can you check now? I've simplified one of the types and most probably won't throw Maximum call stack size exceeded |
|
@CarlosLozanoHealthCaters I checked this branch now with the new commit, but I'm still getting the maximum call stack error. |
|
Can you provide the tsconfig.json and package.json to check which lib are you using? |
|
We're using {
"compilerOptions": {
"allowJs": true,
"baseUrl": "./",
"checkJs": true,
"esModuleInterop": true,
"lib": ["ES2020"],
"module": "CommonJS",
"moduleResolution": "Node",
"noEmit": true,
"noErrorTruncation": false,
"noImplicitAny": false,
"noImplicitReturns": false,
"noImplicitThis": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strictBindCallApply": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
"target": "ESNext",
"typeRoots": ["./@types", "./node_modules/@types"],
"types": ["node", "jest"]
},
"exclude": [
"**/*.cjs",
"node_modules",
"**/__tests__/*.js",
"**/__tests__/**/*.js"
],
"include": [
"./"
]
} |
|
Could you please check the compilation also crash if you set allowJs to false in your tsconfig? Probably tons of errors could appear in console, but we can guess if the compilation also crash or not. |
|
@avaly I created a new type StrictProjectionType to separate the old type and new type. I think it's a good approach and solution. |
|
@CarlosLozanoHealthCaters I tried this again on our repo, and initially it failed again with the same error - which puzzled me very much. I then tried to comment out your types changes and ran it again, and I got the same error. At this point, I realized something must be off when trying to use this library from your branch. My method of testing this was that I ran So I tried a different approach: Once I did this, your changes no longer trigger that weird error. I even went back to your So we could move ahead with either approach, but if you're open to it, I would prefer going with the latest version of the separate |
|
I think I changed my mind, so we can try to merge this without the separate strict type. @CarlosLozanoHealthCaters if you want to revert your commits until this commit 4034125, I can run through it again and finally land this (but probably after v10 goes out #422). |
…nges" This reverts commit 7c4a2f0.
|
@avaly done! |
|
@CarlosLozanoHealthCaters when you have some time, can you please rebase this on the latest |
|
@avaly is it now correct? |
|
I have simplified
by
I think it will help compiler |
Closes #356
Please take a look! I will include tests and modify the documentation when I have 5 minutes.