Item add
This commit is contained in:
58
database/create_items_table.sql
Normal file
58
database/create_items_table.sql
Normal file
@@ -0,0 +1,58 @@
|
||||
-- Create items table
|
||||
CREATE TABLE items (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
category_id TEXT NOT NULL,
|
||||
subcategory_id TEXT,
|
||||
context_tags TEXT[] DEFAULT '{}',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create index on user_id for faster queries
|
||||
CREATE INDEX idx_items_user_id ON items(user_id);
|
||||
|
||||
-- Create index on category_id for filtering
|
||||
CREATE INDEX idx_items_category_id ON items(category_id);
|
||||
|
||||
-- Create index on context_tags for tag-based searches
|
||||
CREATE INDEX idx_items_context_tags ON items USING GIN(context_tags);
|
||||
|
||||
-- Enable Row Level Security
|
||||
ALTER TABLE items ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policy to allow users to see their own items
|
||||
CREATE POLICY "Users can view own items"
|
||||
ON items FOR SELECT
|
||||
USING (auth.uid() = user_id);
|
||||
|
||||
-- Create policy to allow users to insert their own items
|
||||
CREATE POLICY "Users can insert own items"
|
||||
ON items FOR INSERT
|
||||
WITH CHECK (auth.uid() = user_id);
|
||||
|
||||
-- Create policy to allow users to update their own items
|
||||
CREATE POLICY "Users can update own items"
|
||||
ON items FOR UPDATE
|
||||
USING (auth.uid() = user_id);
|
||||
|
||||
-- Create policy to allow users to delete their own items
|
||||
CREATE POLICY "Users can delete own items"
|
||||
ON items FOR DELETE
|
||||
USING (auth.uid() = user_id);
|
||||
|
||||
-- Create trigger to update updated_at timestamp
|
||||
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = NOW();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ language 'plpgsql';
|
||||
|
||||
CREATE TRIGGER update_items_updated_at
|
||||
BEFORE UPDATE ON items
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_updated_at_column();
|
||||
Reference in New Issue
Block a user