Skip to content

Instantly share code, notes, and snippets.

@silentworks
Created November 9, 2021 10:10
Show Gist options
  • Save silentworks/64190681812677c9fbfb2b74e2ea9ea6 to your computer and use it in GitHub Desktop.
Save silentworks/64190681812677c9fbfb2b74e2ea9ea6 to your computer and use it in GitHub Desktop.
Creating a Table with RLS enabled and a View with only the data you want exposed
// Get poll data and slug for voting path but no results slug
const { data, error } = await supabase.from('poll_without_results')
.select('*')
// Normal Table
CREATE TABLE public.polls (
id BIGINT generated by default AS identity primary key,
question TEXT NOT NULL,
vote_slug VARCHAR (16) UNIQUE NOT NULL,
result_slug VARCHAR (16) UNIQUE NOT NULL,
user_id uuid NOT NULL
);
// Enable Row Level Security (RLS)
ALTER TABLE public.polls enable row level security;
// Add RLS so only auth users can vote and see the result_slug
CREATE POLICY "Enable all actions for users based on user_id" ON public.polls FOR ALL USING (auth.uid() = user_id) WITH CHECK (auth.uid() = user_id);
// Postgres View without the result_slug column
CREATE VIEW public.poll_without_results AS
SELECT id, question, vote_slug, user_id
FROM polls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment