From 472b8b1c271ef8b12bf3c2ca80dfb0b983402d81 Mon Sep 17 00:00:00 2001 From: MWorld Deployer Date: Tue, 29 Sep 2026 17:33:50 +0530 Subject: [PATCH] chore: initial database schema --- .gitignore | 5 +++ database/01_init.sql | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 .gitignore create mode 100644 database/01_init.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5de5f73 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules +dist +.wrangler +.env +.next diff --git a/database/01_init.sql b/database/01_init.sql new file mode 100644 index 0000000..c840cc7 --- /dev/null +++ b/database/01_init.sql @@ -0,0 +1,74 @@ +CREATE TABLE IF NOT EXISTS users ( + id TEXT PRIMARY KEY, + email TEXT NOT NULL UNIQUE, + full_name TEXT, + display_name TEXT, + bio TEXT, + avatar_url TEXT, + role TEXT DEFAULT 'user', + created_at TEXT DEFAULT CURRENT_TIMESTAMP, + username TEXT UNIQUE, + mauth_token TEXT, + phone TEXT, + citizen_id TEXT, + is_banned INTEGER DEFAULT 0, + updated_at TEXT DEFAULT CURRENT_TIMESTAMP +); +CREATE TABLE IF NOT EXISTS posts ( + id TEXT PRIMARY KEY, + author_email TEXT NOT NULL, + content TEXT NOT NULL, + image_url TEXT, + like_count INTEGER NOT NULL DEFAULT 0, + comment_count INTEGER NOT NULL DEFAULT 0, + created_at TEXT DEFAULT CURRENT_TIMESTAMP, + updated_at TEXT DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (author_email) REFERENCES users(email) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS comments ( + id TEXT PRIMARY KEY, + post_id TEXT NOT NULL, + author_email TEXT NOT NULL, + content TEXT NOT NULL, + created_at TEXT DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE, + FOREIGN KEY (author_email) REFERENCES users(email) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS post_likes ( + id TEXT PRIMARY KEY, + post_id TEXT NOT NULL, + user_email TEXT NOT NULL, + created_at TEXT DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE, + FOREIGN KEY (user_email) REFERENCES users(email) ON DELETE CASCADE, + UNIQUE(post_id, user_email) +); +CREATE TABLE IF NOT EXISTS follows ( + id TEXT PRIMARY KEY, + follower_email TEXT NOT NULL, + following_email TEXT NOT NULL, + created_at TEXT DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (follower_email) REFERENCES users(email) ON DELETE CASCADE, + FOREIGN KEY (following_email) REFERENCES users(email) ON DELETE CASCADE, + UNIQUE(follower_email, following_email) +); +CREATE TABLE IF NOT EXISTS notifications ( + id TEXT PRIMARY KEY, + recipient_email TEXT NOT NULL, + actor_email TEXT NOT NULL, + type TEXT NOT NULL, + post_id TEXT, + read INTEGER NOT NULL DEFAULT 0, + created_at TEXT DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (recipient_email) REFERENCES users(email) ON DELETE CASCADE, + FOREIGN KEY (actor_email) REFERENCES users(email) ON DELETE CASCADE, + FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE SET NULL +); +CREATE INDEX IF NOT EXISTS idx_posts_author ON posts(author_email); +CREATE INDEX IF NOT EXISTS idx_posts_created ON posts(created_at); +CREATE INDEX IF NOT EXISTS idx_comments_post ON comments(post_id); +CREATE INDEX IF NOT EXISTS idx_post_likes_post_user ON post_likes(post_id, user_email); +CREATE INDEX IF NOT EXISTS idx_follows_follower ON follows(follower_email); +CREATE INDEX IF NOT EXISTS idx_follows_following ON follows(following_email); +CREATE INDEX IF NOT EXISTS idx_notifications_recipient ON notifications(recipient_email, created_at); +CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email ON users(email);