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

Feature/ts lean missing doc fixes #11761 without killing LeanDocument #11769

Merged
merged 5 commits into from
May 5, 2022
Merged

Feature/ts lean missing doc fixes #11761 without killing LeanDocument #11769

merged 5 commits into from
May 5, 2022

Conversation

taxilian
Copy link
Contributor

@taxilian taxilian commented May 4, 2022

Summary

Per #11761 .toObject doesn't add _id to the document if it is missing; this fixes that.

Examples

See unit tests in the code

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick suggestion to hopefully make changes backwards compatible.

@@ -225,7 +225,7 @@ declare module 'mongoose' {
toJSON<T = DocType>(options: ToObjectOptions & { flattenMaps: false }): LeanDocument<T>;

/** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
toObject<T = DocType>(options?: ToObjectOptions): LeanDocument<T>;
toObject<T = DocType>(options?: ToObjectOptions): Require_id<LeanDocument<T>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the interest of backwards compat, why not make this just toObject<T = LeanDocument<DocType>> and return Require_id<T>? That should allow people to bypass going through LeanDocument, which seems to be causing part of the problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely an option; by doing that you will not be converting any mongoose.Types.Array types to real javascript arrays and any embedded subdocuments won't get converted to POJOs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess that's my way of saying "it's up to you", but also answering "why not" =]

@taxilian
Copy link
Contributor Author

taxilian commented May 4, 2022

@vkarpov15 I added one more quick change -- .lean was not actually applying LeanDocument, which resulted in lean not actually propagating and fixing things like the arrays, subdocuments, etc.

@vkarpov15 vkarpov15 merged commit 16e5714 into Automattic:master May 5, 2022
vkarpov15 added a commit that referenced this pull request May 5, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants