-
Notifications
You must be signed in to change notification settings - Fork 27
Add useUpsert hook #6
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
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # useUpsert | ||
|
|
||
| Performs `INSERT` or `UPDATE` on table. | ||
|
|
||
| ```js highlight=4 | ||
| import { useUpsert } from 'react-supabase' | ||
|
|
||
| function Page() { | ||
| const [{ count, data, error, fetching }, execute] = useUpsert('users') | ||
|
|
||
| async function onClickMarkAllComplete() { | ||
| const { count, data, error } = await execute( | ||
| { completed: true }, | ||
| { onConflict: 'username' }, | ||
| (query) => query.eq('completed', false), | ||
| ) | ||
| } | ||
|
|
||
| return ... | ||
| } | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - By specifying the `onConflict` option, you can make `UPSERT` work on a column(s) that has a `UNIQUE` constraint. | ||
| - Primary keys should to be included in the data payload in order for an update to work correctly. | ||
| - Primary keys must be natural, not surrogate. There are however, workarounds for surrogate primary keys. | ||
| - Param `filter` makes sense only when operation is update | ||
| - Upsert supports sending array of elements, just like `useInsert` | ||
|
|
||
| ## Passing options | ||
|
|
||
| During hook initialization: | ||
|
|
||
| ```js | ||
| const [{ count, data, error, fetching }, execute] = useUpsert('users', { | ||
| filter: (query) => query.eq('completed', false), | ||
| options: { | ||
| returning: 'represenation', | ||
| onConflict: 'username', | ||
| count: 'exact', | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| Or execute function: | ||
|
|
||
| ```js | ||
| const { count, data, error } = await execute( | ||
| { completed: true }, | ||
| { | ||
| count: 'estimated', | ||
| onConflict: 'username', | ||
| returning: 'minimal', | ||
| }, | ||
| (query) => query.eq('completed', false), | ||
| ) | ||
| ``` | ||
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { useCallback, useEffect, useRef, useState } from 'react' | ||
|
|
||
| import { Count, Filter, PostgrestError, Returning } from '../../types' | ||
| import { useClient } from '../use-client' | ||
| import { initialState } from './state' | ||
|
|
||
| export type UseUpsertState<Data = any> = { | ||
| count?: number | null | ||
| data?: Data | Data[] | null | ||
| error?: PostgrestError | null | ||
| fetching: boolean | ||
| } | ||
|
|
||
| export type UseUpsertResponse<Data = any> = [ | ||
| UseUpsertState<Data>, | ||
| ( | ||
| values: Partial<Data> | Partial<Data>[], | ||
| options?: UseUpsertOptions, | ||
| filter?: Filter<Data>, | ||
| ) => Promise<Pick<UseUpsertState<Data>, 'count' | 'data' | 'error'>>, | ||
| ] | ||
|
|
||
| export type UseUpsertOptions = { | ||
| count?: null | Count | ||
| onConflict?: string | ||
| returning?: Returning | ||
| } | ||
|
|
||
| export type UseUpsertConfig<Data = any> = { | ||
| filter?: Filter<Data> | ||
| options?: UseUpsertOptions | ||
| } | ||
|
|
||
| export function useUpsert<Data = any>( | ||
| table: string, | ||
| config: UseUpsertConfig<Data> = { options: {} }, | ||
| ): UseUpsertResponse<Data> { | ||
| const client = useClient() | ||
| const isMounted = useRef(false) | ||
| const [state, setState] = useState<UseUpsertState>(initialState) | ||
|
|
||
| /* eslint-disable react-hooks/exhaustive-deps */ | ||
| const execute = useCallback( | ||
| async ( | ||
| values: Partial<Data> | Partial<Data>[], | ||
| options?: UseUpsertOptions, | ||
| filter?: Filter<Data>, | ||
| ) => { | ||
| const refine = filter ?? config.filter | ||
| setState({ ...initialState, fetching: true }) | ||
| const source = client | ||
| .from<Data>(table) | ||
| .upsert(values, options ?? config.options) | ||
|
|
||
| const { count, data, error } = await (refine | ||
| ? refine(source) | ||
| : source) | ||
|
|
||
| const res = { count, data, error } | ||
| if (isMounted.current) setState({ ...res, fetching: false }) | ||
| return res | ||
| }, | ||
| [client], | ||
| ) | ||
| /* eslint-enable react-hooks/exhaustive-deps */ | ||
|
|
||
| useEffect(() => { | ||
| isMounted.current = true | ||
| return () => { | ||
| isMounted.current = false | ||
| } | ||
| }, []) | ||
|
|
||
| return [state, execute] | ||
| } |
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,3 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`useUpsert should throw when not inside Provider 1`] = `"No client has been specified using Provider."`; |
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,10 @@ | ||
| import { renderHook } from '@testing-library/react-hooks' | ||
|
|
||
| import { useUpsert } from '../../../src' | ||
|
|
||
| describe('useUpsert', () => { | ||
| it('should throw when not inside Provider', () => { | ||
| const { result } = renderHook(() => useUpsert('todos')) | ||
| expect(() => result.current).toThrowErrorMatchingSnapshot() | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding these