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

refactor: make query helpers more consistently fit our loose definition of helpers #14350

Merged
merged 3 commits into from
Feb 20, 2024

Conversation

vkarpov15
Copy link
Collaborator

Summary

This PR is more of an architecture discussion. In my mind, a "helper" ideally should be a function that takes as argument only non-Mongoose objects, with the potential exception of schemas and SchemaTypes. So no models or queries or constructors or anything like that, unless they are only used as context for some callback function like in castFilterPath.

Why is this important? Makes helpers much easier to test, reason about, and reuse. Once you have to instantiate a model and a connection, that makes testing slower.

In that vein, in this PR I'm moving a couple of functions in helpers/query out into query.js, refactored a couple of helpers to use just POJO options objects rather than a full model, and moved some constants into a constants.js file (helpers should always be functions).

WDYT @AbdelrahmanHafez @hasezoey @IslandRhythms ?

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

Looks good, some small documentation issues though.

though maybe consider putting this in the next minor version.

lib/query.js Outdated Show resolved Hide resolved
lib/query.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

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

LGTM, definitely feels lighter and helps with re-usability.

vkarpov15 and others added 2 commits February 19, 2024 16:29
Co-authored-by: hasezoey <hasezoey@gmail.com>
Co-authored-by: hasezoey <hasezoey@gmail.com>
@vkarpov15 vkarpov15 changed the base branch from master to 8.2 February 20, 2024 15:44
@vkarpov15 vkarpov15 added this to the 8.2 milestone Feb 20, 2024
@vkarpov15 vkarpov15 merged commit b10fc72 into 8.2 Feb 20, 2024
34 checks passed
@hasezoey hasezoey deleted the vkarpov15/helpers-refactor branch February 20, 2024 19:21
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.

3 participants