-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
98 lines (87 loc) · 3.75 KB
/
supabase-schema.sql
File metadata and controls
98 lines (87 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
-- PrepJS Database Schema for Supabase
-- Run this SQL in your Supabase SQL Editor to set up the database
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Users table
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
access_key TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
current_streak INTEGER DEFAULT 0,
total_points INTEGER DEFAULT 0,
level INTEGER DEFAULT 1,
last_quiz_date TIMESTAMP WITH TIME ZONE
);
-- Quiz attempts table
CREATE TABLE IF NOT EXISTS quiz_attempts (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
day_number INTEGER NOT NULL,
questions JSONB NOT NULL,
user_answers JSONB NOT NULL,
score INTEGER NOT NULL,
time_taken INTEGER NOT NULL,
completed_at TIMESTAMP WITH TIME ZONE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Topic performance table
CREATE TABLE IF NOT EXISTS topic_performance (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
topic_name TEXT NOT NULL,
total_attempts INTEGER DEFAULT 0,
correct_attempts INTEGER DEFAULT 0,
last_updated TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, topic_name)
);
-- Achievements table
CREATE TABLE IF NOT EXISTS achievements (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
achievement_type TEXT NOT NULL,
unlocked_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, achievement_type)
);
-- Covered sub-topics tracking table
CREATE TABLE IF NOT EXISTS covered_subtopics (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
main_topic TEXT NOT NULL,
subtopic TEXT NOT NULL,
covered_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, main_topic, subtopic)
);
-- Create indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_quiz_attempts_user_id ON quiz_attempts(user_id);
CREATE INDEX IF NOT EXISTS idx_quiz_attempts_completed_at ON quiz_attempts(completed_at DESC);
CREATE INDEX IF NOT EXISTS idx_topic_performance_user_id ON topic_performance(user_id);
CREATE INDEX IF NOT EXISTS idx_achievements_user_id ON achievements(user_id);
CREATE INDEX IF NOT EXISTS idx_covered_subtopics_user_id ON covered_subtopics(user_id);
-- Row Level Security (RLS) policies
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE quiz_attempts ENABLE ROW LEVEL SECURITY;
ALTER TABLE topic_performance ENABLE ROW LEVEL SECURITY;
ALTER TABLE achievements ENABLE ROW LEVEL SECURITY;
ALTER TABLE covered_subtopics ENABLE ROW LEVEL SECURITY;
-- Allow public read/write for now (single-user app)
-- In production, you may want more restrictive policies
CREATE POLICY "Allow all operations on users" ON users FOR ALL USING (true);
CREATE POLICY "Allow all operations on quiz_attempts" ON quiz_attempts FOR ALL USING (true);
CREATE POLICY "Allow all operations on topic_performance" ON topic_performance FOR ALL USING (true);
CREATE POLICY "Allow all operations on achievements" ON achievements FOR ALL USING (true);
CREATE POLICY "Allow all operations on covered_subtopics" ON covered_subtopics FOR ALL USING (true);
-- Insert a default user with a hashed access key
-- The default access key is: "prepjs2024"
-- This is the bcrypt hash of "prepjs2024"
-- You should change this to your own secure key!
INSERT INTO users (access_key, current_streak, total_points, level)
VALUES (
'$2a$10$rQEY7QYmG8vH0xJ2zWQZQeKXmO7bNqW8hO9xR5vT6yA3zL1oP4cI6',
0,
0,
1
) ON CONFLICT DO NOTHING;
-- To generate a new hashed password, you can use this Node.js code:
-- const bcrypt = require('bcryptjs');
-- const hash = bcrypt.hashSync('your-secret-key', 10);
-- console.log(hash);