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

Add Schema#virtuals to docs as a public property #10829

Closed
chumager opened this issue Oct 1, 2021 · 3 comments
Closed

Add Schema#virtuals to docs as a public property #10829

chumager opened this issue Oct 1, 2021 · 3 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@chumager
Copy link

chumager commented Oct 1, 2021

Do you want to request a feature or report a bug?
Feature
What is the current behavior?
It's easy to loop around all paths with schema.eachPath but not for vituals, It'll be really helpfull to have somthing like schema.eachVirtualPath
If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
now you have to do:

for(const virtualPath in schema.virtuals){
  const virtualType = schema.virtual(virtualPath);
  //do something
}

instead of something like:

schema.eachVirtualPath((path, virtualType)=>{
  //do something
});

Obviously you can always do:

mongoose.Schema.prototype.eachVirtualPath = function eachVirtualPath(cb){
  Object.keys(this.virtuals).forEach(path => cb(path, this.virtual(path));
};

I don't like the first approach as it's a inner prop and you can change it whenever you want, even set it as private or use a Symbol.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

mongoose: 6.0.7
node: 16.10.0
mongodb atlas: 4.4.9

@IslandRhythms IslandRhythms added the new feature This change adds new functionality, like a new method or class label Oct 1, 2021
@vkarpov15
Copy link
Collaborator

Why not just do Object.entries(schema.virtuals).forEach(([path, virtualType]) => { ... }) ? We can add this feature pretty easily, but I'm not sure it provides much value over just iterating through the object using Object.entries()

@vkarpov15 vkarpov15 added this to the 6.1.0 milestone Oct 2, 2021
@chumager
Copy link
Author

chumager commented Oct 4, 2021

Hi, more that how easy could it be, schema.virtuals it's not documented, as such, there is no guarantee to keep it the same, if it were kept in the docs then I can think that it'll not change unless it's informed, that's all.

PS: BTW very good optimization, even I know Object.entries I never use it... 😄

@vkarpov15 vkarpov15 modified the milestones: 6.1.0, 6.0.9 Oct 4, 2021
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed new feature This change adds new functionality, like a new method or class labels Oct 4, 2021
@vkarpov15
Copy link
Collaborator

vkarpov15 commented Oct 4, 2021

We'll add Schema.virtuals to the docs. We're not going to change .virtuals, we have tests that rely on it and it is also part of our TypeScript index.d.ts file.

@vkarpov15 vkarpov15 changed the title add schema.eachVirtualPath Add Schema#virtuals to docs as a public property Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants