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

[rfc] loadFirstByFieldEqualityConjunction, a convenience method #206

Merged
merged 1 commit into from
Jan 25, 2023

Conversation

ide
Copy link
Member

@ide ide commented Jan 24, 2023

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 specify limit: 1.

How

Added loadFirstByFieldEqualityConjunction to EntityLoader and EnforcingEntityLoader. This new method calls into loadManyByFieldEqualityConjunction and passes in limit: 1. It uses TS to prevent limit from being specified from the outside, which indicates a programming error. It also requires orderBy to be specified. We have a caller that doesn't care about order, just existence -- in this case, the caller should pass in orderBy: [] 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.

@ide ide requested a review from wschurman as a code owner January 24, 2023 06:27
@codecov
Copy link

codecov bot commented Jan 24, 2023

Codecov Report

Merging #206 (2a2c2a2) into main (92f5529) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #206      +/-   ##
==========================================
+ Coverage   96.17%   96.18%   +0.01%     
==========================================
  Files          81       81              
  Lines        2065     2071       +6     
  Branches      269      271       +2     
==========================================
+ Hits         1986     1992       +6     
+ Misses         79       74       -5     
- Partials        0        5       +5     
Flag Coverage Δ
integration 96.18% <100.00%> (+0.01%) ⬆️
unittest 96.18% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/entity/src/EnforcingEntityLoader.ts 100.00% <100.00%> (ø)
packages/entity/src/EntityLoader.ts 89.69% <100.00%> (+0.32%) ⬆️
...ty-example/src/adapters/InMemoryDatabaseAdapter.ts 45.83% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@wschurman wschurman left a comment

Choose a reason for hiding this comment

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

I think this sounds reasonable.

@ide ide merged commit 1934216 into main Jan 25, 2023
@ide ide deleted the @ide/loadfirst branch January 25, 2023 00:41
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.

2 participants