-
-
Notifications
You must be signed in to change notification settings - Fork 145
feat(tanstack-query): add useTransaction hook for sequential transactions #2637
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7447968
feat(tanstack-query): add useTransaction hook for sequential transact…
ymc9 bbcea4f
Merge remote-tracking branch 'origin/dev' into feat/tanstack-query-us…
ymc9 4976409
merge test
ymc9 78bee26
fix: address pr comments
ymc9 29bf3e6
fix: address PR comments
ymc9 520cdf4
refactor(tanstack-query): strongly type TransactionOperation and norm…
ymc9 6b07448
fix test
ymc9 b965719
refactor(tanstack-query): tighten TransactionOperation typing and spl…
ymc9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| /** Route segment for custom procedures. */ | ||
| export const CUSTOM_PROC_ROUTE_NAME = '$procs'; | ||
|
|
||
| /** Route prefix for transaction endpoints. */ | ||
| export const TRANSACTION_ROUTE_PREFIX = '$transaction'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import type { Logger } from '@zenstackhq/client-helpers'; | ||
| import { createInvalidator, type InvalidateFunc } from '@zenstackhq/client-helpers'; | ||
| import type { FetchFn } from '@zenstackhq/client-helpers/fetch'; | ||
| import { fetcher, marshal } from '@zenstackhq/client-helpers/fetch'; | ||
| import { CoreReadOperations } from '@zenstackhq/orm'; | ||
| import type { SchemaDef } from '@zenstackhq/schema'; | ||
| import { TRANSACTION_ROUTE_PREFIX } from './constants.js'; | ||
| import type { TransactionOperation } from './types.js'; | ||
|
|
||
| /** | ||
| * Builds the mutation function for a sequential transaction request. | ||
| */ | ||
| export function makeTransactionMutationFn<Schema extends SchemaDef>(endpoint: string, fetch: FetchFn | undefined) { | ||
| return (operations: TransactionOperation<Schema>[]) => { | ||
| const reqUrl = `${endpoint}/${TRANSACTION_ROUTE_PREFIX}/sequential`; | ||
| const fetchInit = { | ||
| method: 'POST', | ||
| headers: { 'content-type': 'application/json' }, | ||
| body: marshal(operations), | ||
| }; | ||
| return fetcher<unknown[]>(reqUrl, fetchInit, fetch); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the `onSuccess` handler for a sequential transaction mutation that invalidates | ||
| * all queries affected by the operations in the transaction. | ||
| * | ||
| * @param schema The schema definition. | ||
| * @param invalidateFunc Function that invalidates queries matching a predicate. | ||
| * @param logging Logging option. | ||
| * @param origOnSuccess The user-provided `onSuccess` callback to call after invalidation. | ||
| */ | ||
| export function makeTransactionOnSuccess( | ||
| schema: SchemaDef, | ||
| invalidateFunc: InvalidateFunc, | ||
| logging: Logger | undefined, | ||
| origOnSuccess: ((...args: any[]) => any) | undefined, | ||
| ) { | ||
| return async (...args: any[]) => { | ||
| const variables = Array.isArray(args[1]) ? args[1] : []; | ||
| for (const op of variables) { | ||
| if (typeof op?.model !== 'string' || typeof op?.op !== 'string') { | ||
| continue; | ||
| } | ||
| // read-only ops don't mutate state, so they don't trigger invalidation | ||
| if (CoreReadOperations.includes(op.op)) { | ||
| continue; | ||
| } | ||
| const invalidator = createInvalidator(op.model, op.op, schema, invalidateFunc, logging); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| // pass op.args as mutation variables so the invalidator can analyze nested writes | ||
| await invalidator(args[0], op.args, args[2]); | ||
| } | ||
| await origOnSuccess?.(...args); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.