[rfc] loadFirstByFieldEqualityConjunction, a convenience method #206
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why
Several places (about five) in our codebase call
loadFirstByFieldEqualityConjunction
with{ limit: 1 }
either to get just the first row or to test for the existence of any matching row. The intent of the callers is often to get the latest row (limit 1, order by a descending timestamp). This PR adds a convenience method for that use case, primarily to help the callers convey their intent better, rather than saving one line of code by removing the need to specifylimit: 1
.How
Added
loadFirstByFieldEqualityConjunction
to EntityLoader and EnforcingEntityLoader. This new method calls intoloadManyByFieldEqualityConjunction
and passes inlimit: 1
. It uses TS to preventlimit
from being specified from the outside, which indicates a programming error. It also requiresorderBy
to be specified. We have a caller that doesn't care about order, just existence -- in this case, the caller should pass inorderBy: []
to explicitly show that it doesn't care about order. The more common pattern was to get the first row of an ordered set, though.Test Plan
Added unit tests for both EntityLoader and EnforcingEntityLoader. Notably, the EntityLoader test ensures the privacy policy was evaluated only once even though two rows match, and also checks that the
orderBy
clause was honored.