forked from Charushi06/StudyPlan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
48 lines (43 loc) · 1.55 KB
/
database.js
File metadata and controls
48 lines (43 loc) · 1.55 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
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const db = new sqlite3.Database(path.join(__dirname, 'studyplan.db'));
function initDb() {
db.serialize(() => {
// Subjects Table
db.run(`CREATE TABLE IF NOT EXISTS subjects (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
short_code TEXT,
color TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`);
// Tasks Table
db.run(`CREATE TABLE IF NOT EXISTS tasks (
id TEXT PRIMARY KEY,
subject_id TEXT,
title TEXT NOT NULL,
description TEXT,
due_at DATETIME,
status TEXT DEFAULT 'Not Started',
priority TEXT DEFAULT 'medium',
confidence_score REAL,
notes TEXT,
archived INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (subject_id) REFERENCES subjects(id)
)`);
// Pre-populate some subjects if empty
db.get('SELECT COUNT(*) as count FROM subjects', (err, row) => {
if (row && row.count === 0) {
console.log("Seeding subjects...");
const stmt = db.prepare("INSERT INTO subjects (id, name, short_code, color) VALUES (?, ?, ?, ?)");
stmt.run('sub_1', 'Computer Science', 'CS', 'var(--color-text-info)');
stmt.run('sub_2', 'Mathematics', 'Maths', 'var(--color-text-success)');
stmt.run('sub_3', 'English Lit', 'English', 'var(--color-text-purple)');
stmt.run('sub_4', 'Physics', 'Physics', 'var(--color-text-warning)');
stmt.finalize();
}
});
});
}
module.exports = { db, initDb };