chore: initial database schema

This commit is contained in:
MWorld Deployer 2026-09-29 17:33:50 +05:30
commit 472b8b1c27
2 changed files with 79 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
node_modules
dist
.wrangler
.env
.next

74
database/01_init.sql Normal file
View File

@ -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);