Files
manage/src/hooks/supabaseService.js
2025-01-15 15:31:08 +08:00

136 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { supabase } from "@/config/supabase";
import { message } from "antd";
class SupabaseService {
async select(table, options = {}) {
try {
let query = supabase
.from(table)
.select(options.select || "*", { count: "exact" });
// 处理精确匹配条件
if (options.match) {
query = query.match(options.match);
}
// 处理过滤条件
if (options.filter) {
Object.entries(options.filter).forEach(([key, condition]) => {
Object.entries(condition).forEach(([operator, value]) => {
query = query.filter(key, operator, value);
});
});
}
// 处理排序
if (options.order) {
query = query.order(options.order.column, {
ascending: options.order.ascending,
});
}
// 处理分页
if (options.page && options.pageSize) {
const from = (options.page - 1) * options.pageSize;
const to = from + options.pageSize - 1;
query = query.range(from, to);
}
// 如果需要单条数据
if (options.single) {
query = query.single();
}
const { data, error, count } = await query;
if (error) throw error;
return {
data,
total: count || 0,
};
} catch (error) {
console.error(`Error fetching from ${table}:`, error.message);
throw error;
}
}
// 通用 INSERT 请求
async insert(table, data) {
try {
const { data: result, error } = await supabase
.from(table)
.insert(data)
.select();
if (error) {
if (error.code === '42501' || error.status === 403) {
throw new Error('暂无权限:您没有执行此操作的权限');
}
throw new Error(error.message || '数据库操作失败');
}
if (result?.length === 0) {
throw new Error('暂无权限:您没有执行此操作的权限');
}
return result;
} catch (error) {
// 确保错误始终是 Error 对象
const errorMessage = error instanceof Error ? error.message : String(error);
console.error(`Insert error for table ${table}:`, errorMessage);
throw new Error(errorMessage);
}
}
// 优化 UPDATE 请求
async update(table, match, updates) {
try {
let query = supabase.from(table).update(updates);
// 如果是对象,使用 match 方法
if (typeof match === "object") {
query = query.match(match);
} else {
// 如果是单个 id使用 eq 方法
query = query.eq("id", match);
}
const { data, error } = await query.select();
if(data.length===0) throw '暂无权限' ;
if (error) throw error;
return data;
} catch (error) {
throw new Error('更新失败');
}
}
// 通用 DELETE 请求
async delete(table, match) {
try {
const { data,error } = await supabase.from(table).delete().match(match).select();
if(data.length===0) throw '暂无权限';
if (error) throw error;
return data;
} catch (error) {
console.error(`Error deleting from ${table}:`, error.message);
throw error;
}
}
// 通用 UPSERT 请求
async upsert(table, data, onConflict) {
try {
let query = supabase.from(table).upsert(data).select();
if (onConflict) {
query = query.onConflict(onConflict);
}
const { data: result, error } = await query;
if (error) throw error;
return result;
} catch (error) {
console.error(`Error upserting into ${table}:`, error.message);
throw error;
}
}
}
export const supabaseService = new SupabaseService();