Skip to content

Commit

Permalink
added an implementation for types
Browse files Browse the repository at this point in the history
  • Loading branch information
barrymichaeldoyle committed Apr 19, 2023
1 parent a3bba14 commit 895fd22
Show file tree
Hide file tree
Showing 17 changed files with 607 additions and 126 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
scripts
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"rules": {
"eqeqeq": "error",
"@typescript-eslint/ban-types": "off",
"linebreak-style": ["error", "unix"],
"prettier/prettier": "error",
"unused-imports/no-unused-imports": "error",
Expand Down
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"useTabs": false,
"tabWidth": 2,
"singleQuote": true,
"semi": true,
"printWidth": 80,
"trailingComma": "es5",
"endOfLine": "lf"
Expand Down
63 changes: 63 additions & 0 deletions example/database.types.ts
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;
};
};
}
238 changes: 238 additions & 0 deletions example/generated.ts
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');
},
}
);
}
5 changes: 5 additions & 0 deletions example/supabase.ts
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');
Loading

0 comments on commit 895fd22

Please sign in to comment.