Method Chaining
supabase.table(...)
.select(...)
.eq(...)
.execute(...)
class SupabaseEx:
def __init__(self):
self.settings = []
def table(self, table):
self.settings.append(table)
return self
def select(self, fields):
self.settings.append(fields)
return self
def eq(self, name, value):
self.settings.append(f'{name}={value}')
return self
def execute(self):
print(self.settings)
print('실행..')
return '처리된 결과'
supabase = SupabaseEx()
response = (
supabase.table("users")
.select('*')
.eq('email', 'user01@test.org')
.execute()
)
class AiLog:
def __init__(self):
user = supabase.auth.get_user()
if not user:
raise PermissionError('로그인 필요함')
self.user = user.user
def create_documents(self, title, content):
response = (
supabase.table("documents")
.insert({
"title" : title,
"content" : content,
"user_id" : self.user.id
}).execute()
)
return response.data
def get_my_documents(self):
response = (
supabase.table("documents")
.select("*")
.eq("user_id", self.user.id)
.execute()
)
return response.data
ai_log = AiLog()
ai_log.create_documents("세 번째 문서", "본문 내용")
ai_log.get_my_documents()
RLS ( Row Level Security )
create policy "본인 문서 전체 관리"
on documents
for all
to authenticated
using (auth.uid() = user_id)
with check (auth.uid() = user_id);