feat: implement library system database schema
- Add library_types table with ebooks, comics, manga types - Add libraries table for multiple library support - Add library_folders table for multi-folder libraries - Add library_visibility table for user access control - Add media_items table replacing ebooks for broader media support - Create backward compatibility views for existing API - Implement library service with type validation and file extension handling - Support modular extension for future media types Manga type includes cbz/cbr archives as requested
This commit is contained in:
+118
-32
@@ -1,5 +1,51 @@
|
||||
-- Consolidated Bookmann Database Schema
|
||||
-- This file contains the complete current schema for the Bookmann ebook management system
|
||||
-- This file contains the complete current schema for the Bookmann media library management system
|
||||
|
||||
-- Create library types table
|
||||
CREATE TABLE library_types (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(50) UNIQUE NOT NULL,
|
||||
description TEXT,
|
||||
allowed_extensions TEXT[] NOT NULL, -- Array of allowed file extensions for this type
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Insert default library types
|
||||
INSERT INTO library_types (name, description, allowed_extensions) VALUES
|
||||
('ebooks', 'Ebook files including EPUB, PDF, MOBI, etc.', ARRAY['.epub', '.pdf', '.mobi', '.azw', '.azw3', '.txt', '.rtf', '.doc', '.docx', '.lit', '.fb2', '.pdb']),
|
||||
('comics', 'Comic book archives and image formats', ARRAY['.cbz', '.cbr', '.cb7', '.cbt', '.pdf']),
|
||||
('manga', 'Manga files including archives and image folders', ARRAY['.cbz', '.cbr', '.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp']);
|
||||
|
||||
-- Create libraries table
|
||||
CREATE TABLE libraries (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
library_type_id UUID NOT NULL REFERENCES library_types(id) ON DELETE RESTRICT,
|
||||
created_by_admin_id UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create library_folders table for multiple folders per library
|
||||
CREATE TABLE library_folders (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
library_id UUID NOT NULL REFERENCES libraries(id) ON DELETE CASCADE,
|
||||
folder_path VARCHAR(500) NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(library_id, folder_path)
|
||||
);
|
||||
|
||||
-- Create library_visibility table for user-specific library visibility
|
||||
CREATE TABLE library_visibility (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
library_id UUID NOT NULL REFERENCES libraries(id) ON DELETE CASCADE,
|
||||
is_visible BOOLEAN NOT NULL DEFAULT true,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(user_id, library_id)
|
||||
);
|
||||
|
||||
-- Create users table
|
||||
CREATE TABLE users (
|
||||
@@ -17,12 +63,13 @@ CREATE TABLE users (
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create ebooks table
|
||||
CREATE TABLE ebooks (
|
||||
-- Create media_items table (replaces ebooks table for broader media support)
|
||||
CREATE TABLE media_items (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
library_id UUID NOT NULL REFERENCES libraries(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
author VARCHAR(255),
|
||||
isbn VARCHAR(13),
|
||||
isbn VARCHAR(13), -- Still relevant for ebooks
|
||||
description TEXT,
|
||||
file_path VARCHAR(500) NOT NULL,
|
||||
file_size BIGINT,
|
||||
@@ -31,7 +78,7 @@ CREATE TABLE ebooks (
|
||||
series VARCHAR(255),
|
||||
series_number INTEGER,
|
||||
tags TEXT,
|
||||
asin VARCHAR(20),
|
||||
asin VARCHAR(20), -- Still relevant for ebooks
|
||||
date_published DATE,
|
||||
publisher VARCHAR(255),
|
||||
contributors TEXT,
|
||||
@@ -40,57 +87,96 @@ CREATE TABLE ebooks (
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create ebooks view for backward compatibility
|
||||
CREATE VIEW ebooks AS
|
||||
SELECT mi.*
|
||||
FROM media_items mi
|
||||
JOIN libraries l ON mi.library_id = l.id
|
||||
JOIN library_types lt ON l.library_type_id = lt.id
|
||||
WHERE lt.name = 'ebooks';
|
||||
|
||||
-- Create reading_progress table
|
||||
CREATE TABLE reading_progress (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
ebook_id UUID NOT NULL REFERENCES ebooks(id) ON DELETE CASCADE,
|
||||
media_item_id UUID NOT NULL REFERENCES media_items(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
current_page INTEGER DEFAULT 0,
|
||||
total_pages INTEGER,
|
||||
last_read_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(ebook_id, user_id)
|
||||
UNIQUE(media_item_id, user_id)
|
||||
);
|
||||
|
||||
-- Create ebook_ratings table
|
||||
CREATE TABLE ebook_ratings (
|
||||
-- Create reading_progress view for backward compatibility
|
||||
CREATE VIEW ebook_reading_progress AS
|
||||
SELECT rp.*,
|
||||
mi.id as ebook_id -- Map media_item_id to ebook_id for compatibility
|
||||
FROM reading_progress rp
|
||||
JOIN media_items mi ON rp.media_item_id = mi.id
|
||||
JOIN libraries l ON mi.library_id = l.id
|
||||
JOIN library_types lt ON l.library_type_id = lt.id
|
||||
WHERE lt.name = 'ebooks';
|
||||
|
||||
-- Create media_ratings table (replaces ebook_ratings)
|
||||
CREATE TABLE media_ratings (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
ebook_id UUID NOT NULL REFERENCES ebooks(id) ON DELETE CASCADE,
|
||||
media_item_id UUID NOT NULL REFERENCES media_items(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 10), -- 10-point scale for half-star precision
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(ebook_id, user_id)
|
||||
UNIQUE(media_item_id, user_id)
|
||||
);
|
||||
|
||||
-- Create user_ebook_folders table for multiple folders per user
|
||||
CREATE TABLE user_ebook_folders (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
folder_path VARCHAR(500) NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(user_id, folder_path)
|
||||
-- Note: Admin-only access is enforced at application level in handlers/auth.go
|
||||
);
|
||||
-- Create ebook_ratings view for backward compatibility
|
||||
CREATE VIEW ebook_ratings AS
|
||||
SELECT mr.*,
|
||||
mi.id as ebook_id -- Map media_item_id to ebook_id for compatibility
|
||||
FROM media_ratings mr
|
||||
JOIN media_items mi ON mr.media_item_id = mi.id
|
||||
JOIN libraries l ON mi.library_id = l.id
|
||||
JOIN library_types lt ON l.library_type_id = lt.id
|
||||
WHERE lt.name = 'ebooks';
|
||||
|
||||
-- Note: user_ebook_folders table is replaced by library_folders table
|
||||
-- Libraries now handle folder management instead of individual users
|
||||
|
||||
-- Create indexes for better query performance
|
||||
CREATE INDEX idx_users_email ON users(email);
|
||||
CREATE INDEX idx_users_username ON users(username);
|
||||
CREATE INDEX idx_ebooks_title ON ebooks(title);
|
||||
CREATE INDEX idx_ebooks_author ON ebooks(author);
|
||||
CREATE INDEX idx_ebooks_added_by_admin_id ON ebooks(added_by_admin_id);
|
||||
CREATE INDEX idx_reading_progress_ebook_id ON reading_progress(ebook_id);
|
||||
CREATE INDEX idx_library_types_name ON library_types(name);
|
||||
|
||||
-- Library indexes
|
||||
CREATE INDEX idx_libraries_name ON libraries(name);
|
||||
CREATE INDEX idx_libraries_library_type_id ON libraries(library_type_id);
|
||||
CREATE INDEX idx_libraries_created_by_admin_id ON libraries(created_by_admin_id);
|
||||
CREATE INDEX idx_library_folders_library_id ON library_folders(library_id);
|
||||
CREATE INDEX idx_library_visibility_user_id ON library_visibility(user_id);
|
||||
CREATE INDEX idx_library_visibility_library_id ON library_visibility(library_id);
|
||||
|
||||
-- Media items indexes
|
||||
CREATE INDEX idx_media_items_title ON media_items(title);
|
||||
CREATE INDEX idx_media_items_author ON media_items(author);
|
||||
CREATE INDEX idx_media_items_library_id ON media_items(library_id);
|
||||
CREATE INDEX idx_media_items_added_by_admin_id ON media_items(added_by_admin_id);
|
||||
|
||||
-- Progress and ratings indexes
|
||||
CREATE INDEX idx_reading_progress_media_item_id ON reading_progress(media_item_id);
|
||||
CREATE INDEX idx_reading_progress_user_id ON reading_progress(user_id);
|
||||
CREATE INDEX idx_ebook_ratings_ebook_id ON ebook_ratings(ebook_id);
|
||||
CREATE INDEX idx_ebook_ratings_user_id ON ebook_ratings(user_id);
|
||||
CREATE INDEX idx_user_ebook_folders_user_id ON user_ebook_folders(user_id);
|
||||
CREATE INDEX idx_media_ratings_media_item_id ON media_ratings(media_item_id);
|
||||
CREATE INDEX idx_media_ratings_user_id ON media_ratings(user_id);
|
||||
|
||||
-- Add comment explaining the rating system
|
||||
COMMENT ON COLUMN ebook_ratings.rating IS 'Rating scale 1-10 (odd numbers = half-stars: 1,3,5,7,9 = 0.5,1.5,2.5,3.5,4.5 stars)';
|
||||
COMMENT ON COLUMN media_ratings.rating IS 'Rating scale 1-10 (odd numbers = half-stars: 1,3,5,7,9 = 0.5,1.5,2.5,3.5,4.5 stars)';
|
||||
|
||||
-- Library Type File Extensions Notes:
|
||||
-- - Ebooks: .epub, .pdf, .mobi, .azw, .azw3, .txt, .rtf, .doc, .docx, .lit, .fb2, .pdb
|
||||
-- - Comics: .cbz, .cbr, .cb7, .cbt, .pdf
|
||||
-- - Manga: .cbz, .cbr, .png, .jpg, .jpeg, .gif, .bmp, .webp (note: manga includes image folders)
|
||||
|
||||
-- Role System Notes:
|
||||
-- - All users default to 'user' role
|
||||
-- - Admin users can: add/edit/delete folders, scan ebooks, modify ebook metadata, delete ebooks
|
||||
-- - Regular users can: view all ebooks, rate ebooks, track reading progress, manage their profile
|
||||
-- - Admin users can: create/manage libraries and folders, scan media, modify media metadata, delete media, control library visibility
|
||||
-- - Regular users can: view visible libraries, rate media, track reading progress, manage their profile
|
||||
-- - To create first admin: UPDATE users SET role = 'admin' WHERE email = 'your-admin-email';
|
||||
-- - Only admins can manage folders and ebook metadata (enforced at application level)
|
||||
-- - user_ebook_folders table admin-only access is enforced by AdminMiddleware in handlers
|
||||
-- - Library visibility is controlled through library_visibility table - admins can hide/show libraries per user
|
||||
-- - Backward compatibility views (ebooks, ebook_ratings, ebook_reading_progress) maintain existing API contracts
|
||||
Reference in New Issue
Block a user