Migracao Logto + Supabase - backend e frontend atualizados para nova autenticação
This commit is contained in:
69
src/server/config/supabase.ts
Normal file
69
src/server/config/supabase.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
const supabaseUrl = process.env.SUPABASE_URL || 'https://supabase.reifonas.cloud';
|
||||
const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE3NDYwMTMyMDAsImV4cCI6MTc3NzU0OTIwMCwiYXNkIjoidHJ1ZSIsInN1YiI6ImFkbW10cmFja3N0ZWVsIn0.H4ZcZI3kaZclQJlRj3a3b0VbVrL3R2GzT8l5t5jL3Yc';
|
||||
|
||||
export const supabase = createClient(supabaseUrl, supabaseServiceKey, {
|
||||
db: {
|
||||
schema: 'gpi'
|
||||
},
|
||||
auth: {
|
||||
autoRefreshToken: false,
|
||||
persistSession: false
|
||||
}
|
||||
});
|
||||
|
||||
export const GPI_SCHEMA = 'gpi';
|
||||
|
||||
export async function queryGpi(table: string, query?: any) {
|
||||
let dbQuery = supabase.from(table).select('*');
|
||||
|
||||
if (query) {
|
||||
if (query.filter) {
|
||||
Object.entries(query.filter).forEach(([key, value]) => {
|
||||
dbQuery = dbQuery.eq(key, value);
|
||||
});
|
||||
}
|
||||
if (query.order) {
|
||||
dbQuery = dbQuery.order(query.order.by || 'created_at', { ascending: query.order.asc ?? false });
|
||||
}
|
||||
if (query.limit) {
|
||||
dbQuery = dbQuery.limit(query.limit);
|
||||
}
|
||||
if (query.offset) {
|
||||
dbQuery = dbQuery.range(query.offset, query.offset + (query.limit || 10) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return await dbQuery;
|
||||
}
|
||||
|
||||
export async function insertGpi(table: string, data: any) {
|
||||
return await supabase.from(table).insert(data).select();
|
||||
}
|
||||
|
||||
export async function updateGpi(table: string, id: string, data: any) {
|
||||
return await supabase.from(table).update(data).eq('id', id).select();
|
||||
}
|
||||
|
||||
export async function deleteGpi(table: string, id: string) {
|
||||
return await supabase.from(table).delete().eq('id', id);
|
||||
}
|
||||
|
||||
export async function findOneGpi(table: string, filters: Record<string, any>) {
|
||||
let query = supabase.from(table).select('*');
|
||||
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
query = query.eq(key, value);
|
||||
});
|
||||
|
||||
const { data, error } = await query.single();
|
||||
|
||||
if (error && error.code !== 'PGRST116') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
console.log('✅ Supabase client initialized for GPI schema');
|
||||
Reference in New Issue
Block a user