Untitled

 avatar
unknown
plain_text
10 months ago
2.2 kB
16
Indexable
-- =================================
-- EXTENSIONS (untuk UUID)
-- =================================
create extension if not exists "pgcrypto";

-- =================================
-- PRODUCTS TABLE
-- =================================
create table if not exists public.products (
  id uuid primary key default gen_random_uuid(),         -- ID unik pakai UUID
  name text not null,                                    -- Nama produk
  slug text not null unique,                             -- Slug unik untuk URL
  description text,                                      -- Deskripsi produk
  price_cents integer not null check (price_cents >= 0), -- Harga dalam "cents"
  currency text not null default 'IDR',                  -- Mata uang (default IDR)
  image_url text,                                        -- URL gambar dari Supabase Storage
  file_url text,                                         -- (opsional) file digital / download link
  is_active boolean not null default true,               -- Status produk aktif/tidak
  created_at timestamptz not null default now(),         -- Timestamp dibuat
  updated_at timestamptz not null default now()          -- Timestamp update
);

-- =================================
-- TRIGGER UNTUK UPDATED_AT
-- =================================
create or replace function update_updated_at_column()
returns trigger as $$
begin
   new.updated_at = now();
   return new;
end;
$$ language plpgsql;

create trigger update_products_updated_at
before update on public.products
for each row
execute function update_updated_at_column();

-- =================================
-- ENABLE RLS
-- =================================
alter table public.products enable row level security;

-- =================================
-- POLICIES
-- =================================

-- Publik bisa baca hanya produk aktif
create policy "Public can read active products"
on public.products
for select
to public
using (is_active = true);

-- Hanya service_role yang boleh insert/update/delete produk
create policy "Service role can modify products"
on public.products
for all
to service_role
using (true)
with check (true);
Editor is loading...
Leave a Comment