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

fix: move runInTransaction to ViewerContext #108

Merged
merged 1 commit into from
Jan 20, 2021

Conversation

wschurman
Copy link
Member

Why

This has been suggested by @jkhales and @ide a few times as a better pattern. At its core this does the same thing as the previous implementation, it just removes the unnecessary association with concrete entity types.

How

Move the methods to ViewerContext. Note that this will most likely be used in methods in ViewerContext subclasses in the following manner:

// in ExpoViewerContext.ts

async runInPostgresTransactionAsync<TResult>(
  transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<TResult>
): Promise<TResult> {
  return await this.runInTransactionForDatabaseAdaptorFlavorAsync(
    DatabaseAdapterFlavor.POSTGRES,
    transactionScope
  );
}

so that callsites don't need to specify the DatabaseAdapterFlavor.

Questions for Reviewers

Do you think it's worth added these DatabaseAdapterFlavor-hardcoded methods to ViewerContext? Since the DatabaseAdapterFlavor is hardcoded in this package, it can be done cleanly. The upside is that consumers don't need to specify them. The downside is that the number of methods would grow if we add more DatabaseAdapterFlavors.

Test Plan

Run all tests.

@wschurman wschurman requested review from ide and jkhales January 20, 2021 02:26
@codecov
Copy link

codecov bot commented Jan 20, 2021

Codecov Report

Merging #108 (e62f2fd) into master (01b3bfe) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #108   +/-   ##
=======================================
  Coverage   94.95%   94.96%           
=======================================
  Files          66       66           
  Lines        1624     1627    +3     
  Branches      195      177   -18     
=======================================
+ Hits         1542     1545    +3     
  Misses         80       80           
  Partials        2        2           
Flag Coverage Δ
integration 94.96% <100.00%> (+<0.01%) ⬆️
unittest 94.96% <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/ReadonlyEntity.ts 100.00% <ø> (ø)
packages/entity/src/EntityCompanionProvider.ts 100.00% <100.00%> (ø)
packages/entity/src/ViewerContext.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 01b3bfe...e62f2fd. Read the comment docs.

Copy link

@jkhales jkhales left a comment

Choose a reason for hiding this comment

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

nice. ✨

I would be curious to know what other flavors will be added. For example if MySQL is added then things would be similar enough for transactions that a runInSQLTransactionAsync method seems like the cleaner abstraction.

@wschurman
Copy link
Member Author

wschurman commented Jan 20, 2021

I would be curious to know what other flavors will be added. For example if MySQL is added then things would be similar enough for transactions that a runInSQLTransactionAsync method seems like the cleaner abstraction.

We'd need one method per flavor just in case an application uses multiple flavors. This flavor concept as a whole though probably needs to be removed and just replaced with strings so that client applications can have as many "flavors" as they want and configure them however they like.

@wschurman wschurman merged commit b7309e1 into master Jan 20, 2021
@wschurman wschurman deleted the @wschurman/transaction-helper branch January 20, 2021 18:07
@ide
Copy link
Member

ide commented Jan 20, 2021

Since the number of database flavors will likely stay small, having hardcoded methods seems good so that we don't need to pass the same parameter in everywhere. It also might help with type hints a bit if the Postgres query context were to be different than a MySQL query context.

Longer-term, if we have multiple flavors, I think we will want to automatically detect what flavors are used and just have a generic "runInTransactionAsync" method that creates the correct query contexts on the fly as needed, or at least warn if you started a Postgres transaction but tried to access MySQL. There might be cases where we need to let developers opt out (e.g. they want a PG transaction but not a MySQL one) but those cases seem rare.

@@ -226,6 +226,13 @@ export default class EntityCompanionProvider {
});
}

getQueryContextProviderForDatabaseAdaptorFlavor(
Copy link
Member

Choose a reason for hiding this comment

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

Might want to make getTableDataCoordinatorForEntity call this so that the behavior between the two methods stays in sync.

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