forked from ashishkujoy/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
132 lines (109 loc) · 3.98 KB
/
schema.sql
File metadata and controls
132 lines (109 loc) · 3.98 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
-- Enable extensions for better text search performance
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE TABLE IF NOT EXISTS books (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
authors TEXT NOT NULL,
isbn10 VARCHAR(10) UNIQUE,
isbn13 VARCHAR(13) UNIQUE,
count INTEGER DEFAULT 0,
borrowed_count INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS book_copies (
id SERIAL PRIMARY KEY,
book_id INTEGER REFERENCES books(id) ON DELETE CASCADE,
borrowed BOOLEAN DEFAULT FALSE,
qr_code TEXT UNIQUE
);
CREATE TABLE IF NOT EXISTS borrowed_books (
id SERIAL PRIMARY KEY,
book_copy_id INTEGER REFERENCES book_copies(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL,
borrowed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
returned_at TIMESTAMP
);
CREATE TABLE IF NOT EXISTS library_users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
is_admin BOOLEAN DEFAULT FALSE,
batch_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS books_db (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
authors TEXT,
isbn10 VARCHAR(10) UNIQUE,
isbn13 VARCHAR(13) UNIQUE
);
CREATE TABLE IF NOT EXISTS verification_token
(
identifier TEXT NOT NULL,
expires TIMESTAMPTZ NOT NULL,
token TEXT NOT NULL,
PRIMARY KEY (identifier, token)
);
CREATE TABLE IF NOT EXISTS accounts
(
id SERIAL,
"userId" INTEGER NOT NULL,
type VARCHAR(255) NOT NULL,
provider VARCHAR(255) NOT NULL,
"providerAccountId" VARCHAR(255) NOT NULL,
refresh_token TEXT,
access_token TEXT,
expires_at BIGINT,
id_token TEXT,
scope TEXT,
session_state TEXT,
token_type TEXT,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS sessions
(
id SERIAL,
"userId" INTEGER NOT NULL,
expires TIMESTAMPTZ NOT NULL,
"sessionToken" VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS users
(
id SERIAL,
name VARCHAR(255),
email VARCHAR(255),
"emailVerified" TIMESTAMPTZ,
image TEXT,
PRIMARY KEY (id)
);
-- Performance Indexes
-- Library Users table indexes
CREATE INDEX IF NOT EXISTS idx_library_users_email ON library_users(email);
-- Books table indexes
CREATE INDEX IF NOT EXISTS idx_books_title ON books(title);
-- Text search index for fuzzy search performance
CREATE INDEX IF NOT EXISTS idx_books_title_gin ON books USING gin(title gin_trgm_ops);
-- Composite index for pagination with search
CREATE INDEX IF NOT EXISTS idx_books_id_title ON books(id, title);
CREATE INDEX IF NOT EXISTS idx_books_isbn10 ON books(isbn10);
CREATE INDEX IF NOT EXISTS idx_books_isbn13 ON books(isbn13);
CREATE INDEX IF NOT EXISTS idx_books_db_isbn10 ON books_db(isbn10);
CREATE INDEX IF NOT EXISTS idx_books_db_isbn13 ON books_db(isbn13);
-- Book copies table indexes
CREATE INDEX IF NOT EXISTS idx_book_copies_book_id ON book_copies(book_id);
CREATE INDEX IF NOT EXISTS idx_book_copies_borrowed ON book_copies(borrowed);
CREATE INDEX IF NOT EXISTS idx_book_copies_qr_code ON book_copies(qr_code);
-- Borrowed books table indexes
CREATE INDEX IF NOT EXISTS idx_borrowed_books_book_copy_id ON borrowed_books(book_copy_id);
CREATE INDEX IF NOT EXISTS idx_borrowed_books_user_id ON borrowed_books(user_id);
CREATE INDEX IF NOT EXISTS idx_borrowed_books_user_borrowed ON borrowed_books(user_id, borrowed_at) WHERE returned_at IS NULL;
-- Authentication table indexes
CREATE INDEX IF NOT EXISTS idx_accounts_user_id ON accounts("userId");
CREATE INDEX IF NOT EXISTS idx_accounts_provider ON accounts(provider, "providerAccountId");
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions("userId");
CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions("sessionToken");
CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions(expires);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_verification_token_identifier ON verification_token(identifier);
CREATE INDEX IF NOT EXISTS idx_verification_token_expires ON verification_token(expires);