File size: 1,572 Bytes
c020c98
 
 
 
c13f601
c020c98
 
 
 
 
 
c13f601
c020c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c13f601
 
 
 
 
 
 
d1f9048
 
 
 
 
 
c13f601
d1f9048
c13f601
 
d1f9048
 
c020c98
c13f601
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
import database from "better-sqlite3";
import dotenv from "dotenv";
import fs from "fs";
dotenv.config();
const db_path = process.env.DB_NAME;
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,
                username varchar(40) UNIQUE NOT NULL,
                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)
            );
        `);
        db.exec(`
            create table chats(
                id Text not null primary key,
                member_id TEXT NOT NULL,
                FOREIGN KEY (member_id) REFERENCES users(id)
            );
        `);
        db.exec(`
            create table messages(
                id Text not null primary key,
                send_at INTEGER NOT NULL,
                message Text not null,
                send_from TEXT NOT NULL,
                chat_id TEXT NOT NULL,
                is_seen TINYINT DEFAULT 0,
                FOREIGN KEY (send_from) REFERENCES users(id),
                FOREIGN KEY (chat_id) REFERENCES users(id)
            );
        `);
    }
});