-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Typescript: Can't represent newly-constructed document as "populated" #12233
Comments
…er support manual population Fix #12233
@ajwootto please take a look at #12341, with that you'll be able to do the following. newParent.$assertPopulated<{nestedChild: NestedChild}>('nestedChild', {
nestedChild: new NestedChildModel({ name: 'NestedChild' })
}) As an alternative workaround that works today, you can do the following. newParent.set(nestedChild, new NestedChildModel({ name: 'NestedChild' }));
newParent.$assertPopulated<{ nestedChild: NestedChildModel }>('nestedChild'); |
@vkarpov15 I think I can make that work. In our code we tend to initialize the document first and then assign all the various fields to it later on before finally saving it. In this case, I imagine I'd be able to do something like this: const newChild = new NestedChild()
const newParent = (new Parent()).$assertPopulated<{nestedChild: NestedChild}>('nestedChild', {
nestedChild: newChild
})
nestedChild.name = 'some name'
newParent.save() So the associated child document would be modifyable later on in the code. const newChild = new NestedChild()
const newParent = (new Parent({nestedChild: newChild})).$assertPopulated<{nestedChild: NestedChild}>('nestedChild')
nestedChild.name = 'some name'
newParent.save() And would |
Yeah |
Cool. I generally want to avoid using |
fix(document): allow calling `$assertPopulated()` with values to better support manual population
Mongoose does type casting, so the "type safety" of |
Prerequisites
🚀 Feature Proposal
related to #11758
When constructing a new document, there currently isn't a way (that I can find) to tell Mongoose that I want the document to be represented as "populated". This presents a problem when constructing a new one using related documents:
Basically the $assertPopulated method that was recently added doesn't help in this case, because the document isn't actually "populated" technically so the internal $populated field is false.
A workaround I found is to basically perform a no-op "populate" call to receive the new type that contains the populated field.
Should there be some kind of built-in way to represent this case without workarounds? Could
$assertPopulated
just proceed if the document is "new" and the field being asserted on hasn't been modified yet? (since it then has the "potential" to be populated)Motivation
I'm writing an API that creates new documents and returns them in populated form. Currently I don't have a way of constructing a populated document from scratch and having it be typed correctly.
Example
The easiest way I see would just be to make
$assertPopulated
not throw when the document is new:The text was updated successfully, but these errors were encountered: