Spaces:
Paused
Paused
File size: 3,406 Bytes
959b027 ca368c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
import { supabase } from './supabase';
import { camelToSnake, snakeToCamel } from '../utils';
// Utility function to fetch data from a table
export const fetchDataFromTable = async <T>(
tableName: string,
limit: number,
offset: number,
filters?: { [key: string]: any }
): Promise<{
[x: string]: any; data: T[]; totalCount: number
}> => {
try {
let query = supabase
.from(tableName)
.select('*', { count: 'exact' })
.order('updated_at', { ascending: false })
.range(offset, offset + limit - 1);
// Apply filters if provided
if (filters) {
for (const [key, value] of Object.entries(filters)) {
query = query.eq(key, value);
}
}
const { data, count, error } = await query;
if (error) {
throw error;
}
const camelData = data?.map(snakeToCamel) || [];
return { data: camelData as T[], totalCount: count || 0 };
} catch (error: any) {
console.error(`Error fetching data from ${tableName}:`, error.message);
throw error;
}
};
// Utility function to insert data into a table
export const insertDataIntoTable = async <T>(
tableName: string,
dataInserted: T
): Promise<T> => {
try {
const snakeData = [dataInserted as any].map(camelToSnake);
const { data, error } = await supabase
.from(tableName)
.insert(snakeData)
.select();
if (error) {
throw error;
}
const inserted = data ? data[0] : null;
if (!inserted) {
throw new Error('No data returned after insertion');
}
const camelData = [inserted].map(snakeToCamel);
return camelData[0] as T;
} catch (error: any) {
throw error;
}
};
// Utility function to delete data from a table
export const deleteDataFromTable = async (tableName: string, id: string): Promise<void> => {
try {
const { error } = await supabase
.from(tableName)
.delete()
.eq('id', id);
if (error) {
throw error;
}
} catch (error: any) {
throw error;
}
};
// Utility function to update data in a table
export const updateDataInTable = async <T>(
tableName: string,
data: T
): Promise<void> => {
try {
const snakeData = [data as any].map(camelToSnake);
const { error } = await supabase
.from(tableName)
.update(snakeData[0])
.eq('id', (data as any).id);
if (error) {
throw error;
}
} catch (error: any) {
throw error;
}
};
// Utility function to get image URL from Supabase
export const getImageUrl = async (bucket: string, imageName: string): Promise<string> => {
try {
const { data } = supabase
.storage
.from(bucket)
.getPublicUrl(imageName);
return data.publicUrl;
} catch (error: any) {
console.error('Error fetching image URL:', error.message);
throw error;
}
};
// Utility function to fetch all payment methods
export const fetchAllPaymentMethods = async (): Promise<any[]> => {
try {
const { data, error } = await supabase
.from('payment_methods')
.select('*')
.eq('is_active', true)
.order('is_default', { ascending: false })
.order('created_at', { ascending: false });
if (error) {
throw error;
}
const camelData = data?.map(snakeToCamel) || [];
return camelData;
} catch (error: any) {
console.error('Error fetching payment methods:', error.message);
throw error;
}
};
|