-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3bba14
commit 895fd22
Showing
17 changed files
with
607 additions
and
126 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
node_modules | ||
node_modules | ||
scripts |
This file contains 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 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 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,63 @@ | ||
export type Json = | ||
| string | ||
| number | ||
| boolean | ||
| null | ||
| { [key: string]: Json } | ||
| Json[]; | ||
|
||
export interface Database { | ||
public: { | ||
Tables: { | ||
todo_items: { | ||
Row: { | ||
created_at: string; | ||
description: string; | ||
id: string; | ||
name: string; | ||
}; | ||
Insert: { | ||
created_at?: string; | ||
description: string; | ||
id?: string; | ||
name: string; | ||
}; | ||
Update: { | ||
created_at?: string; | ||
description?: string; | ||
id?: string; | ||
name?: string; | ||
}; | ||
}; | ||
profiles: { | ||
Row: { | ||
first_name: string | null; | ||
id: string; | ||
last_name: string | null; | ||
}; | ||
Insert: { | ||
first_name?: string | null; | ||
id: string; | ||
last_name?: string | null; | ||
}; | ||
Update: { | ||
first_name?: string | null; | ||
id?: string; | ||
last_name?: string | null; | ||
}; | ||
}; | ||
}; | ||
Views: { | ||
[_ in never]: never; | ||
}; | ||
Functions: { | ||
[_ in never]: never; | ||
}; | ||
Enums: { | ||
[_ in never]: never; | ||
}; | ||
CompositeTypes: { | ||
[_ in never]: never; | ||
}; | ||
}; | ||
} |
This file contains 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,238 @@ | ||
import { useMutation, useQuery, useQueryClient } from 'react-query'; | ||
import { Database } from './database.types'; | ||
import { supabase } from './supabase'; | ||
|
||
export type GetTodoItemRequest = string; | ||
export type GetTodoItemResponse = { | ||
created_at: string; | ||
description: string; | ||
id: string; | ||
name: string; | ||
}; | ||
export type GetAllTodoItemsResponse = { | ||
created_at: string; | ||
description: string; | ||
id: string; | ||
name: string; | ||
}[]; | ||
export type AddTodoItemRequest = { | ||
created_at?: string; | ||
description: string; | ||
id?: string; | ||
name: string; | ||
}; | ||
export type UpdateTodoItemRequest = { | ||
id: string; | ||
changes: { | ||
created_at?: string; | ||
description?: string; | ||
id?: string; | ||
name?: string; | ||
}; | ||
}; | ||
export type DeleteTodoItemRequest = string; | ||
export type GetProfileRequest = string; | ||
export type GetProfileResponse = { | ||
first_name: string; | ||
id: string; | ||
last_name: string; | ||
}; | ||
export type GetAllProfilesResponse = { | ||
first_name: string; | ||
id: string; | ||
last_name: string; | ||
}[]; | ||
export type AddProfileRequest = { | ||
first_name?: string; | ||
id: string; | ||
last_name?: string; | ||
}; | ||
export type UpdateProfileRequest = { | ||
id: string; | ||
changes: { first_name?: string; id?: string; last_name?: string }; | ||
}; | ||
export type DeleteProfileRequest = string; | ||
|
||
export function useGetTodoItem(id: string) { | ||
return useQuery<Database['public']['Tables']['todo_items']['Row'], Error>( | ||
['todo_items', id], | ||
async () => { | ||
const { data, error } = await supabase | ||
.from<Database['public']['Tables']['todo_items']['Row']>('todo_items') | ||
.select('*') | ||
.eq('id', id) | ||
.single(); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error('No data found'); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
enabled: !!id, | ||
} | ||
); | ||
} | ||
|
||
export function useGetAllTodoItems() { | ||
return useQuery<Database['public']['Tables']['todo_items']['Row'][], Error>( | ||
['todo_items'], | ||
async () => { | ||
const { data, error } = await supabase | ||
.from<Database['public']['Tables']['todo_items']['Row']>('todo_items') | ||
.select(); | ||
if (error) throw error; | ||
return data as Database['public']['Tables']['todo_items']['Row'][]; | ||
} | ||
); | ||
} | ||
|
||
export function useAddTodoItem() { | ||
const queryClient = useQueryClient(); | ||
return useMutation( | ||
(item: Database['public']['Tables']['todo_items']['Insert']) => | ||
supabase | ||
.from<Database['public']['Tables']['todo_items']['Row']>('todo_items') | ||
.insert(item) | ||
.single(), | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries('todo_items'); | ||
}, | ||
} | ||
); | ||
} | ||
|
||
export function useUpdateTodoItem() { | ||
const queryClient = useQueryClient(); | ||
return useMutation( | ||
(item: { | ||
id: string; | ||
changes: Database['public']['Tables']['todo_items']['Update']; | ||
}) => | ||
supabase | ||
.from<Database['public']['Tables']['todo_items']['Row']>('todo_items') | ||
.update(item.changes) | ||
.eq('id', item.id) | ||
.single(), | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries('todo_items'); | ||
}, | ||
} | ||
); | ||
} | ||
|
||
export function useDeleteTodoItem() { | ||
const queryClient = useQueryClient(); | ||
return useMutation( | ||
(id: string) => | ||
supabase | ||
.from<Database['public']['Tables']['todo_items']['Row']>('todo_items') | ||
.delete() | ||
.eq('id', id) | ||
.single(), | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries('todo_items'); | ||
}, | ||
} | ||
); | ||
} | ||
|
||
export function useGetProfile(id: string) { | ||
return useQuery<Database['public']['Tables']['profiles']['Row'], Error>( | ||
['profiles', id], | ||
async () => { | ||
const { data, error } = await supabase | ||
.from<Database['public']['Tables']['profiles']['Row']>('profiles') | ||
.select('*') | ||
.eq('id', id) | ||
.single(); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!data) { | ||
throw new Error('No data found'); | ||
} | ||
|
||
return data; | ||
}, | ||
{ | ||
enabled: !!id, | ||
} | ||
); | ||
} | ||
|
||
export function useGetAllProfiles() { | ||
return useQuery<Database['public']['Tables']['profiles']['Row'][], Error>( | ||
['profiles'], | ||
async () => { | ||
const { data, error } = await supabase | ||
.from<Database['public']['Tables']['profiles']['Row']>('profiles') | ||
.select(); | ||
if (error) throw error; | ||
return data as Database['public']['Tables']['profiles']['Row'][]; | ||
} | ||
); | ||
} | ||
|
||
export function useAddProfile() { | ||
const queryClient = useQueryClient(); | ||
return useMutation( | ||
(item: Database['public']['Tables']['profiles']['Insert']) => | ||
supabase | ||
.from<Database['public']['Tables']['profiles']['Row']>('profiles') | ||
.insert(item) | ||
.single(), | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries('profiles'); | ||
}, | ||
} | ||
); | ||
} | ||
|
||
export function useUpdateProfile() { | ||
const queryClient = useQueryClient(); | ||
return useMutation( | ||
(item: { | ||
id: string; | ||
changes: Database['public']['Tables']['profiles']['Update']; | ||
}) => | ||
supabase | ||
.from<Database['public']['Tables']['profiles']['Row']>('profiles') | ||
.update(item.changes) | ||
.eq('id', item.id) | ||
.single(), | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries('profiles'); | ||
}, | ||
} | ||
); | ||
} | ||
|
||
export function useDeleteProfile() { | ||
const queryClient = useQueryClient(); | ||
return useMutation( | ||
(id: string) => | ||
supabase | ||
.from<Database['public']['Tables']['profiles']['Row']>('profiles') | ||
.delete() | ||
.eq('id', id) | ||
.single(), | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries('profiles'); | ||
}, | ||
} | ||
); | ||
} |
This file contains 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,5 @@ | ||
import { createClient } from '@supabase/supabase-js'; | ||
|
||
import type { Database } from './database.types'; | ||
|
||
export const supabase = createClient<Database>('supabsase-url', 'supabase-key'); |
Oops, something went wrong.