diff options
author | Akshay <[email protected]> | 2024-11-16 22:08:05 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2024-11-16 22:08:05 +0000 |
commit | 42409ecb39b5bb7bb98a8f3c0515876dcba40326 (patch) | |
tree | 0e161467a7ffa9fd66ce9df0631ceb6db238b86c /src/db.js | |
parent | 1ae401253c54a0a195257df95281bf64760f809c (diff) |
add dashboard view, invites
Diffstat (limited to 'src/db.js')
-rw-r--r-- | src/db.js | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -3,6 +3,18 @@ const db = new Database("readit.db", { | |||
3 | strict: true, | 3 | strict: true, |
4 | }); | 4 | }); |
5 | 5 | ||
6 | function 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 | ||
6 | db.query(` | 18 | db.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 | ||
14 | db.query(` | 27 | db.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 | ||
38 | db.query(` | ||
39 | CREATE TABLE IF NOT EXISTS migrations ( | ||
40 | id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
41 | name TEXT UNIQUE | ||
42 | ) | ||
43 | `).run(); | ||
44 | |||
45 | runMigration("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 | |||
24 | module.exports = { db }; | 59 | module.exports = { db }; |