-
-
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
How to write a generic function to resolve PopulatedDoc type variables? #15121
Comments
The approach is reasonable, you just need to type Take a look at the following script, would this approach work for you? import mongoose, { Schema } from 'mongoose';
const ParentModel = mongoose.model('Parent', new Schema({
child: { type: Schema.Types.ObjectId, ref: 'Child' },
name: String
}));
const childSchema: Schema = new Schema({ name: String });
const ChildModel = mongoose.model('Child', childSchema);
type ChildHydratedDocType = ReturnType<(typeof ChildModel)['hydrate']>;
(async function() {
const doc = await ParentModel.findOne({}).populate<{ child: ChildHydratedDocType }>('child').orFail();
const res1: ChildHydratedDocType = await findOrReturnInstance(doc.child, ChildModel);
const doc2 = await ParentModel.findOne().orFail();
const res2: ChildHydratedDocType = await findOrReturnInstance(doc.child, ChildModel);
})();
async function findOrReturnInstance<HydratedDocType extends mongoose.Document>(
docOrId: HydratedDocType | mongoose.Types.ObjectId,
Model: mongoose.Model<any, any, any, any, HydratedDocType>
) {
if (docOrId instanceof mongoose.Document) {
return docOrId;
}
return Model.findById(docOrId).orFail();
} |
The script is fine, although I would have preferred the type to be inferred already by I have another question: if I avoid using function fn(p: ParentInstance) {
// TODO
}
const parent = await Parent.findOne({}).populate<{ child: ChildInstance }>('child').orFail();
// There appears to be a compilation error here, because if I populate child, it will no longer be of type ObjectId.
fn(parent); |
Can you please elaborate on "although I would have preferred the type to be inferred already by findOrReturnInstance and not specified manually."? Re: |
Let me explain better: in the example you wrote, for |
Prerequisites
Mongoose version
8.9.1
Node.js version
20.10.0
MongoDB version
6.0.2
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
In my project, I often find myself having to implement this code:
Is it possible to write a generic function so that I don't have to rewrite the same exact code but with different models?
I tried with this code but it doesn't work:
Is it the right approach or am I doing something wrong?
Attention, with this generic function I want to ensure that any virtual variables and instance methods, etc., are preserved.
Thank you for your help. 🙏🏻
The text was updated successfully, but these errors were encountered: