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

feat: mutation triggers #65

Merged
merged 1 commit into from
Jul 22, 2020
Merged

feat: mutation triggers #65

merged 1 commit into from
Jul 22, 2020

Conversation

wschurman
Copy link
Member

Why

This PR adds mutation triggers, which are similar to active record callbacks. They allow entity authors to specify code that runs before and after mutations, and a special callback that occurs after commit of the transaction.

Use cases:

Note that for now, this is separate from full entity validation (not yet implemented), but that will be done in a similar way. Depending on the transaction semantics we want for validation it may be exactly the same as beforeAll but it's unclear.

How

Add ability to specify features, add tests.

Test Plan

Run tests.

@wschurman wschurman requested review from ide and quinlanj July 22, 2020 18:41
@codecov
Copy link

codecov bot commented Jul 22, 2020

Codecov Report

Merging #65 into master will increase coverage by 0.12%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #65      +/-   ##
==========================================
+ Coverage   94.43%   94.55%   +0.12%     
==========================================
  Files          58       59       +1     
  Lines        1455     1488      +33     
  Branches      159      165       +6     
==========================================
+ Hits         1374     1407      +33     
  Misses         79       79              
  Partials        2        2              
Flag Coverage Δ
#integration 94.55% <100.00%> (+0.12%) ⬆️
#unittest 94.55% <100.00%> (+0.12%) ⬆️

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

Impacted Files Coverage Δ
packages/entity/src/EntityCompanion.ts 100.00% <ø> (ø)
packages/entity/src/EntityQueryContext.ts 94.44% <ø> (ø)
packages/entity/src/EntityCompanionProvider.ts 100.00% <100.00%> (ø)
packages/entity/src/EntityMutationTrigger.ts 100.00% <100.00%> (ø)
packages/entity/src/EntityMutator.ts 97.98% <100.00%> (+0.48%) ⬆️
packages/entity/src/EntityMutatorFactory.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a757867...e09eeee. Read the comment docs.

Copy link
Member

@quinlanj quinlanj left a comment

Choose a reason for hiding this comment

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

lgtm :)

@wschurman wschurman merged commit fd6060c into master Jul 22, 2020
@wschurman wschurman deleted the @wschurman/references-2 branch July 22, 2020 23:57
Comment on lines +60 to +77
/**
* A trigger is a way to specify entity mutation operation side-effects that run within the
* same transaction as the mutation itself. The one exception is afterCommit, which will run within
* the transaction if a transaction is supplied.
*/
export abstract class EntityMutationTrigger<
TFields,
TID,
TViewerContext extends ViewerContext,
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
TSelectedFields extends keyof TFields = keyof TFields
> {
abstract async executeAsync(
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
entity: TEntity
): Promise<void>;
}
Copy link
Member

Choose a reason for hiding this comment

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

Unless there's a meaningful reason to make this a class, plain async functions would be more lightweight:

type EntityMutationTrigger<
    TFields,
    TID,
    TViewerContext extends ViewerContext,
    TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
    TSelectedFields extends keyof TFields = keyof TFields
> = (
      viewerContext: TViewerContext,
      queryContext: EntityQueryContext,
      entity: TEntity
    ) => Promise<void>;

and the replacement for the constructor looks like:

function createThrowConditionallyTrigger<...>(parameters): EntityMutationTrigger<...> {
  return async function throwConditionallyAsync() {
    ... close over parameters if needed
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

So far the class approach has worked out fairly cleanly for EntityPrivacyPolicyRule, especially since feeding params into the constructor is common as is having overloaded constructors. I can imagine that these may turn out to be reasonably similar, but I'll definitely keep it in mind as we start writing triggers and seeing usage patterns.

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