Skip to content
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

Export type from projection #123

Closed
jmhmd opened this issue Nov 8, 2021 · 3 comments
Closed

Export type from projection #123

jmhmd opened this issue Nov 8, 2021 · 3 comments

Comments

@jmhmd
Copy link

jmhmd commented Nov 8, 2021

Is it possible to generate/export a type that is the result of a query with projection? I have a certain projection of a model that I use for a list format in the UI. I'd like to be able to create a new type/interface from that projection for type checking on the client. Thanks for this great library by the way!

@jmhmd
Copy link
Author

jmhmd commented Nov 8, 2021

After some digging into the source, I found ProjectionType, and did something like:

export const caseListProjection = {
  user: 1,
  project: 1,
  shortid: 1,
  // studies: 1,
  "studies._id": 1,
  "studies.series.modality": 1,
  "studies.series._id": 1,
  "studies.studyDate": 1,
  "studies.uploadStatus": 1,
  "studies.description": 1,
  description: 1,
  folder: 1,
  diagnosis: 1,
  tags: 1,
  published: 1,
  meta: 1,
  customLinks: 1,
  createdAt: 1,
  updatedAt: 1,
};
type CaseDefinitionDocument_List = ProjectionType<CaseDefinitionDocument, typeof caseListProjection>

This appears to work as expected. One question I still have is if the dot notation subfields will project to the type definition correctly. Thanks again!

@avaly
Copy link
Collaborator

avaly commented Nov 9, 2021

That's the correct usage of the ProjectionType type. We should add that in our docs.

Regarding the dot-notation support, unfortunately that's not supported right now. But once mongodb/node-mongodb-native#2972 lands in mongodb, we should be able to use the same code to add support here as well.

@jmhmd
Copy link
Author

jmhmd commented Nov 10, 2021

That's great to hear, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants