-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
242 lines (216 loc) · 7.49 KB
/
db.js
File metadata and controls
242 lines (216 loc) · 7.49 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const fs = require('fs');
const path = require('path');
const sqlite3 = require('sqlite3');
const { open } = require('sqlite');
const DB_PATH = path.join(__dirname, 'data', 'app.db');
async function tableExists(db, name) {
const row = await db.get(
"SELECT name FROM sqlite_master WHERE type='table' AND name = ?",
[name]
);
return Boolean(row);
}
async function columnExists(db, table, column) {
const columns = await db.all(`PRAGMA table_info(${table})`);
return columns.some((col) => col.name === column);
}
async function getOrCreateDefaultList(db, userId) {
const existing = await db.get(
'SELECT id FROM lists WHERE owner_user_id = ? ORDER BY id LIMIT 1',
[userId]
);
if (existing) return existing.id;
const now = new Date().toISOString();
const result = await db.run(
'INSERT INTO lists (owner_user_id, name, created_at) VALUES (?, ?, ?)',
[userId, 'Moje zadania', now]
);
return result.lastID;
}
async function ensureBaseTables(db) {
await db.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS lists (
id INTEGER PRIMARY KEY AUTOINCREMENT,
owner_user_id INTEGER NOT NULL,
name TEXT NOT NULL,
created_at TEXT NOT NULL,
FOREIGN KEY (owner_user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS list_members (
list_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
role TEXT NOT NULL DEFAULT 'viewer',
PRIMARY KEY (list_id, user_id),
FOREIGN KEY (list_id) REFERENCES lists(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS list_invites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
list_id INTEGER NOT NULL,
inviter_user_id INTEGER NOT NULL,
invitee_user_id INTEGER NOT NULL,
role TEXT NOT NULL DEFAULT 'viewer',
status TEXT NOT NULL DEFAULT 'pending',
created_at TEXT NOT NULL,
UNIQUE (list_id, invitee_user_id),
FOREIGN KEY (list_id) REFERENCES lists(id) ON DELETE CASCADE,
FOREIGN KEY (inviter_user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (invitee_user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
list_id INTEGER,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
description TEXT,
priority INTEGER NOT NULL DEFAULT 2,
deadline TEXT,
completed INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (list_id) REFERENCES lists(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS task_user_status (
task_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
completed INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL,
PRIMARY KEY (task_id, user_id),
FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS subtask_user_status (
subtask_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
completed INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL,
PRIMARY KEY (subtask_id, user_id),
FOREIGN KEY (subtask_id) REFERENCES subtasks(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS tags (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
UNIQUE (user_id, name),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS task_tags (
task_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
PRIMARY KEY (task_id, tag_id),
FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS subtasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_id INTEGER NOT NULL,
title TEXT NOT NULL,
completed INTEGER NOT NULL DEFAULT 0,
position INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL,
FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE
);
`);
}
async function ensureIndexes(db) {
await db.exec('CREATE INDEX IF NOT EXISTS idx_tasks_user ON tasks(user_id)');
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_tasks_deadline ON tasks(deadline)'
);
if (await columnExists(db, 'tasks', 'list_id')) {
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_tasks_list ON tasks(list_id)'
);
}
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_subtasks_task ON subtasks(task_id)'
);
if (await columnExists(db, 'subtasks', 'position')) {
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_subtasks_order ON subtasks(task_id, position)'
);
}
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_list_members_user ON list_members(user_id)'
);
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_invites_invitee ON list_invites(invitee_user_id)'
);
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_invites_list ON list_invites(list_id)'
);
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_task_user_status_user ON task_user_status(user_id)'
);
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_task_user_status_task ON task_user_status(task_id)'
);
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_subtask_user_status_user ON subtask_user_status(user_id)'
);
await db.exec(
'CREATE INDEX IF NOT EXISTS idx_subtask_user_status_subtask ON subtask_user_status(subtask_id)'
);
}
async function migrateSchema(db) {
const tasksExists = await tableExists(db, 'tasks');
if (tasksExists && !(await columnExists(db, 'tasks', 'list_id'))) {
await db.exec('ALTER TABLE tasks ADD COLUMN list_id INTEGER');
}
const subtasksExists = await tableExists(db, 'subtasks');
if (subtasksExists && !(await columnExists(db, 'subtasks', 'position'))) {
await db.exec('ALTER TABLE subtasks ADD COLUMN position INTEGER');
const taskRows = await db.all('SELECT DISTINCT task_id FROM subtasks');
for (const row of taskRows) {
const subs = await db.all(
'SELECT id FROM subtasks WHERE task_id = ? ORDER BY created_at ASC, id ASC',
[row.task_id]
);
let index = 1;
for (const sub of subs) {
await db.run('UPDATE subtasks SET position = ? WHERE id = ?', [
index,
sub.id,
]);
index += 1;
}
}
}
const users = await db.all('SELECT id FROM users');
for (const user of users) {
await getOrCreateDefaultList(db, user.id);
}
if (tasksExists) {
const orphanTasks = await db.all(
'SELECT id, user_id FROM tasks WHERE list_id IS NULL'
);
for (const task of orphanTasks) {
const listId = await getOrCreateDefaultList(db, task.user_id);
await db.run('UPDATE tasks SET list_id = ? WHERE id = ?', [
listId,
task.id,
]);
}
}
}
async function initDb() {
fs.mkdirSync(path.dirname(DB_PATH), { recursive: true });
const db = await open({
filename: DB_PATH,
driver: sqlite3.Database,
});
await db.exec('PRAGMA foreign_keys = ON;');
await ensureBaseTables(db);
await migrateSchema(db);
await ensureIndexes(db);
return db;
}
module.exports = { initDb };