File size: 1,476 Bytes
c020c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c13f601
c020c98
 
 
 
c13f601
c020c98
 
c13f601
c020c98
 
c13f601
c020c98
 
d1f9048
c13f601
d1f9048
 
 
c13f601
 
 
 
 
 
 
 
d1f9048
 
c13f601
 
 
c020c98
 
 
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
import database from "better-sqlite3";
import dotenv from "dotenv";
import fs from "fs";

dotenv.config();

const db_path = process.env.DB_NAME as string;
console.log(db_path);
fs.unlink(db_path, (err) => {
    if (err) {
        console.error(err);
    }
    else {
        console.log(`deleted old database:\t${db_path}`)


        const db = database(db_path);

        db.exec(`
            create table users(
                id Text not null PRIMARY KEY,
                name varchar(40) NOT NULL,
                password varchar(20) NOT NULL
            );
        `);

        db.exec(`
            create table sessions(
                id Text not null PRIMARY KEY,
                expires_at INTEGER NOT NULL,
                user_id TEXT NOT NULL,
                FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
            );
        `);


        db.exec(`
            create table messages(
                id Text not null primary key,
                content Text not null,
                type Text NOT NULL DEFAULT "text",
                sender_id TEXT NOT NULL,
                receiver_id TEXT NOT NULL,
                timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                status DEFAULT "sent", /* "sent", "received", "seen" */
                FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE CASCADE,
                FOREIGN KEY (receiver_id) REFERENCES users(id) ON DELETE CASCADE
            );
        `);




    }
})