supabase-node-kit
Version:
A backend utility package for Supabase authentication and database functionality.
37 lines (31 loc) • 1.33 kB
text/typescript
import { supabaseClient, supabaseAdmin } from "../config/supabaseClient";
import { SupabaseClient } from "@supabase/supabase-js";
// Helper to determine which client to use
const getClient = (useAdmin: boolean = false): SupabaseClient => {
if (useAdmin && !supabaseAdmin) {
throw new Error("Admin operations can only be performed server-side");
}
return useAdmin ? supabaseAdmin! : supabaseClient;
};
export const dbService = {
async getAll(table: string, useAdmin: boolean = false) {
const client = getClient(useAdmin);
return await client.from(table).select("*");
},
async getById(table: string, id: number, useAdmin: boolean = false) {
const client = getClient(useAdmin);
return await client.from(table).select("*").eq("id", id).single();
},
async insert(table: string, values: object, useAdmin: boolean = false) {
const client = getClient(useAdmin);
return await client.from(table).insert(values);
},
async update(table: string, id: number, values: object, useAdmin: boolean = false) {
const client = getClient(useAdmin);
return await client.from(table).update(values).eq("id", id);
},
async remove(table: string, id: number, useAdmin: boolean = false) {
const client = getClient(useAdmin);
return await client.from(table).delete().eq("id", id);
},
};