chatbot-ui/db/profile.ts

72 lines
1.5 KiB
TypeScript

import { supabase } from "@/lib/supabase/browser-client"
import { TablesInsert, TablesUpdate } from "@/supabase/types"
export const getProfileByUserId = async (userId: string) => {
const { data: profile, error } = await supabase
.from("profiles")
.select("*")
.eq("user_id", userId)
.single()
if (!profile) {
throw new Error(error.message)
}
return profile
}
export const getProfilesByUserId = async (userId: string) => {
const { data: profiles, error } = await supabase
.from("profiles")
.select("*")
.eq("user_id", userId)
if (!profiles) {
throw new Error(error.message)
}
return profiles
}
export const createProfile = async (profile: TablesInsert<"profiles">) => {
const { data: createdProfile, error } = await supabase
.from("profiles")
.insert([profile])
.select("*")
.single()
if (error) {
throw new Error(error.message)
}
return createdProfile
}
export const updateProfile = async (
profileId: string,
profile: TablesUpdate<"profiles">
) => {
const { data: updatedProfile, error } = await supabase
.from("profiles")
.update(profile)
.eq("id", profileId)
.select("*")
.single()
if (error) {
throw new Error(error.message)
}
return updatedProfile
}
export const deleteProfile = async (profileId: string) => {
const { error } = await supabase.from("profiles").delete().eq("id", profileId)
if (error) {
throw new Error(error.message)
}
return true
}