aboutsummaryrefslogtreecommitdiff
path: root/src/db.js
diff options
context:
space:
mode:
authorAkshay <[email protected]>2024-11-16 22:08:05 +0000
committerAkshay <[email protected]>2024-11-16 22:08:05 +0000
commit42409ecb39b5bb7bb98a8f3c0515876dcba40326 (patch)
tree0e161467a7ffa9fd66ce9df0631ceb6db238b86c /src/db.js
parent1ae401253c54a0a195257df95281bf64760f809c (diff)
add dashboard view, invites
Diffstat (limited to 'src/db.js')
-rw-r--r--src/db.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/db.js b/src/db.js
index 24bba3d..747168a 100644
--- a/src/db.js
+++ b/src/db.js
@@ -3,6 +3,18 @@ const db = new Database("readit.db", {
3 strict: true, 3 strict: true,
4}); 4});
5 5
6function runMigration(name, migrationFn) {
7 const exists = db
8 .query("SELECT * FROM migrations WHERE name = $name")
9 .get({ name });
10
11 if (!exists) {
12 migrationFn();
13 db.query("INSERT INTO migrations (name) VALUES ($name)").run({ name });
14 }
15}
16
17// users table
6db.query(` 18db.query(`
7 CREATE TABLE IF NOT EXISTS users ( 19 CREATE TABLE IF NOT EXISTS users (
8 id INTEGER PRIMARY KEY AUTOINCREMENT, 20 id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -11,6 +23,7 @@ db.query(`
11 ) 23 )
12`).run(); 24`).run();
13 25
26// subs table
14db.query(` 27db.query(`
15 CREATE TABLE IF NOT EXISTS subscriptions ( 28 CREATE TABLE IF NOT EXISTS subscriptions (
16 id INTEGER PRIMARY KEY AUTOINCREMENT, 29 id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -21,4 +34,26 @@ db.query(`
21 ) 34 )
22`).run(); 35`).run();
23 36
37// migrations table
38db.query(`
39 CREATE TABLE IF NOT EXISTS migrations (
40 id INTEGER PRIMARY KEY AUTOINCREMENT,
41 name TEXT UNIQUE
42 )
43`).run();
44
45runMigration("add-isAdmin-column", () => {
46 db.query(`
47 ALTER TABLE users
48 ADD COLUMN isAdmin INTEGER DEFAULT 0
49 `).run();
50
51 // first user is admin
52 db.query(`
53 UPDATE users
54 SET isAdmin = 1
55 WHERE id = (SELECT MIN(id) FROM users)
56 `).run();
57});
58
24module.exports = { db }; 59module.exports = { db };