-
-
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
reopened: BREAKING CHANGE from 8.7.2: lean query return full mongoose object type for populated doc instead of FlattenMaps #15102
Comments
Here is another example to demonstrate that this is a bug: if I declare a function that accepts a parameter of type IParent, and then execute a lean query and pass the query result as a parameter to this function, there should be no compilation errors. However, after updating to version 8.7.2 and later, the error appears. const test = (p: IParent) => {
// TODO
};
const parent = await Parent.findOne().lean();
if (parent) {
test(parent);
} The compilation error:
Please check this behavior, because before version 8.7.2, my repository did not return compilation errors for functions that accepted a parameter of type IParent when passing the results of lean queries. Thank you for your patience. |
Ah ok, the compilation error you provided is due to #15057 and would be fixed by #15072. The error message has nothing to do with Mongoose not leaning out // child.ts
import {
connection,
Connection,
HydratedDocument,
Model,
Schema,
SchemaTypes,
Types,
PopulatedDoc,
Document
} from 'mongoose';
export interface IChild {
_id: Types.ObjectId;
name: string;
}
type ChildDocumentOverrides = {};
export interface IChildVirtuals {
id: string;
}
export type ChildInstance = HydratedDocument<
IChild,
ChildDocumentOverrides & IChildVirtuals
>;
type ChildModelType = Model<
IChild,
{},
ChildDocumentOverrides,
IChildVirtuals,
ChildInstance
>;
export default (connection: Connection) => {
const childSchema = new Schema<IChild, ChildModelType>(
{
name: {
type: SchemaTypes.String,
required: true,
trim: true,
},
},
{},
);
return connection.model<IChild, ChildModelType>('Child', childSchema);
};
export interface IParent {
_id: Types.ObjectId;
name: string;
child: PopulatedDoc<Document<Types.ObjectId> & IChild>;
createdAt: Date;
updatedAt: Date;
}
type ParentDocumentOverrides = {};
export interface IParentVirtuals {
id: string;
}
export type ParentInstance = HydratedDocument<
IParent,
ParentDocumentOverrides & IParentVirtuals
>;
type ParentModelType = Model<
IParent,
{},
ParentDocumentOverrides,
IParentVirtuals,
ParentInstance
>;
function createParentModel (connection: Connection) {
const parentSchema = new Schema<IParent, ParentModelType>(
{
name: {
type: SchemaTypes.String,
required: true,
trim: true,
},
child: {
type: SchemaTypes.ObjectId,
ref: 'Child',
required: true,
},
},
{},
);
return connection.model<IParent, ParentModelType>('Parent', parentSchema);
};
const Parent = createParentModel(connection);
const test = (p: IParent) => {
// TODO
};
(async function() {
test(parent);
})(); You can confirm by using the following: const parent = await Parent.findOne().lean().orFail();
// No compiler error in Mongoose 8.7.1, 8.5.0, 7.8.3, 7.0.0
if (parent.child != null && !(parent.child instanceof Types.ObjectId)) {
parent.child.$parent();
} We are working on improving #15072 and will hopefully have a fix for that in the next couple of days |
Fixed by #15103 |
Prerequisites
Mongoose version
8.9.0
Node.js version
20.10.0
MongoDB server version
6.0.2
Typescript version (if applicable)
5.5.4
Description
I'm reopening this issue because it was closed without resolution. You can find the closed issue at this link.
I'll try to use a simpler example to explain what the bug is:
My query:
If I execute the query in versions prior to 8.7.2, the returned type is
![Image](https://private-user-images.githubusercontent.com/71180446/396137941-2308086c-46e7-4b7b-aa93-451c7ca30625.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5MDE2ODMsIm5iZiI6MTczODkwMTM4MywicGF0aCI6Ii83MTE4MDQ0Ni8zOTYxMzc5NDEtMjMwODA4NmMtNDZlNy00YjdiLWFhOTMtNDUxYzdjYTMwNjI1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA3VDA0MDk0M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdmZTI1YjI3MGY5MTY1ZjY5ZWE1OThlMGQwZmFiOGFlZjZmNmEwNWRhN2RhMjAxZDQyYTlkMGI4NWE1NTY4NzImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.f2zcj9bWloGmzhCKiXrPKgiuh2x8l82y4yFaFTW0VKo)
FlattenMaps<IParent>
✅:From version 8.7.2 onwards, the type within the parent for child is Types.ObjectId or an instance of child ❌. However, this is incorrect because I executed a lean query, so child should be
![Image](https://private-user-images.githubusercontent.com/71180446/396138011-d491c9cc-e360-477f-8055-2198b1d5e4c4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5MDE2ODMsIm5iZiI6MTczODkwMTM4MywicGF0aCI6Ii83MTE4MDQ0Ni8zOTYxMzgwMTEtZDQ5MWM5Y2MtZTM2MC00NzdmLTgwNTUtMjE5OGIxZDVlNGM0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA3VDA0MDk0M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM5ZGYxODlkZmRmM2RhNWY4ZTc2N2YxZGNkOTUxYjQ4ZTFlZWJmNjViOGFiZTU4NWU0MmI4MTQ2ZWU3NDQxNjMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.TjZh1KZymohc7yyhIgd7cfpskti3UCaj4H_00vg-198)
Types.ObjectId | FlattenMaps<IChild>
:In your comment, you suggested updating the code and explicitly setting the type of the variable to be populated. However, if I do not want to populate the variable and use a lean query, the returned type should be FlattenMaps for all PopulatedDoc.
Please, review this situation and the example I provided.
Steps to Reproduce
Execute a lean query (without using "populate") on a model that contains variables of type PopulatedDoc.
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: