This commit is contained in:
xuqssq
2024-12-28 15:39:48 +08:00
parent b3ac241354
commit 6e8334ecaf
18 changed files with 5017 additions and 420 deletions

View File

@@ -1,50 +1,55 @@
import { supabase } from '@/config/supabase'
import { supabase } from "@/config/supabase";
class SupabaseService {
async select(table, options = {}) {
try {
let query = supabase
.from(table)
.select(options.select || '*', { count: 'exact' })
.select(options.select || "*", { count: "exact" });
// 处理精确匹配条件
if (options.match) {
query = query.match(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)
})
})
query = query.filter(key, operator, value);
});
});
}
// 处理排序
if (options.order) {
query = query.order(options.order.column, {
ascending: options.order.ascending
})
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)
const from = (options.page - 1) * options.pageSize;
const to = from + options.pageSize - 1;
query = query.range(from, to);
}
const { data, error, count } = await query
// 如果需要单条数据
if (options.single) {
query = query.single();
}
if (error) throw error
const { data, error, count } = await query;
if (error) throw error;
return {
data,
total: count || 0
}
total: count || 0,
};
} catch (error) {
console.error(`Error fetching from ${table}:`, error.message)
throw error
console.error(`Error fetching from ${table}:`, error.message);
throw error;
}
}
@@ -54,29 +59,27 @@ class SupabaseService {
const { data: result, error } = await supabase
.from(table)
.insert(data)
.select()
.select();
if (error) throw error
return result
if (error) throw error;
return result;
} catch (error) {
console.error(`Error inserting into ${table}:`, error.message)
throw error
console.error(`Error inserting into ${table}:`, error.message);
throw error;
}
}
// 优化 UPDATE 请求
async update(table, match, updates) {
try {
let query = supabase
.from(table)
.update(updates);
let query = supabase.from(table).update(updates);
// 如果是对象,使用 match 方法
if (typeof match === 'object') {
if (typeof match === "object") {
query = query.match(match);
} else {
// 如果是单个 id使用 eq 方法
query = query.eq('id', match);
query = query.eq("id", match);
}
const { data, error } = await query.select();
@@ -92,38 +95,32 @@ class SupabaseService {
// 通用 DELETE 请求
async delete(table, match) {
try {
const { error } = await supabase
.from(table)
.delete()
.match(match)
const { error } = await supabase.from(table).delete().match(match);
if (error) throw error
return true
if (error) throw error;
return true;
} catch (error) {
console.error(`Error deleting from ${table}:`, error.message)
throw error
console.error(`Error deleting from ${table}:`, error.message);
throw error;
}
}
// 通用 UPSERT 请求
async upsert(table, data, onConflict) {
// 通用 UPSERT 请求
async upsert(table, data, onConflict) {
try {
let query = supabase
.from(table)
.upsert(data)
.select()
let query = supabase.from(table).upsert(data).select();
if (onConflict) {
query = query.onConflict(onConflict)
query = query.onConflict(onConflict);
}
const { data: result, error } = await query
if (error) throw error
return result
const { data: result, error } = await query;
if (error) throw error;
return result;
} catch (error) {
console.error(`Error upserting into ${table}:`, error.message)
throw error
console.error(`Error upserting into ${table}:`, error.message);
throw error;
}
}
}
export const supabaseService = new SupabaseService()
export const supabaseService = new SupabaseService();