aboutsummaryrefslogtreecommitdiff
path: root/src/database/postgres.ts
blob: 9ea65dc4d2b5960541db29818bf1831026a361d0 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { Thread, Comment }  from '../models/Thread';
import { Client, types } from 'pg';

var connectionString = "postgres://scale:secret@db/scale?sslmode=disable";
let client: Client;

export function InitPostgres() {
    client = new Client(connectionString);
    client.connect();

    types.setTypeParser(types.builtins.INT8, (val) => {
        return Number(val)
    });
}

export function ClosePostgres() {
    client.end();
}

export function CustomQuery(query: string, callback: (r: { status: number, data: any }) => void) {
    client.query(query, (err, res) => {
        let r: { status: number, data: any } = { status: 500, data: undefined };
        if (err) {
            r.data = err.message;
        } else {
            r.status = 200; r.data = res.rows;
        }
        callback(r);
    });
}

export function GetBoards(callback: (r: { status: number, data: any }) => void) {
    let query = `
select s.nspname as board
from pg_catalog.pg_namespace s
where nspname != 'pg_toast' and nspname != 'pg_catalog'
and nspname != 'public' and nspname != 'information_schema';`; // to remove the default schemas

    client.query(query, (err, res) => {
        let r: { status: number, data: any } = { status: 404, data: undefined };
        if (err) {
            r.data = err.message;
        } else {
            r.status = 200; r.data = [];
            for(let board of res.rows)
                r.data.push(board.board);
        }
        callback(r);
    });
}

export function CreateBoard(name: string, callback: (r: { status: number, data: any }) => void) {
    let query = `
create schema ${name};
create table ${name}.thread(
  id serial primary key,
  timestamp bigint,
  title text NOT NULL,
  content text NOT NULL,
  image text,
  imagetype text
);
create table ${name}.comment(
  id serial primary key,
  tid int,
  timestamp bigint,
  content text NOT NULL,
  image text,
  imagetype text,
  constraints fk_tid
    foreign key(tid)
      references ${name}.thread(id)
);`;

    client.query(query, (err, res) => {
        let r: { status: number, data: any } = { status: 500, data: undefined };
        if (err) {
            r.data = err.message;
        } else {
            r.status = 200;
        }
        callback(r);
    });
}

export function GetBoard(board: string, callback: (r: { status: number, data: any }) => void) {
    let query = `
select * from ${board}.thread
order by timestamp desc;`;

    client.query(query, (err, res) => {
        let r: { status: number, data: any } = { status: 404, data: undefined };
        if (err) {
            r.data = err.message;
        } else {
            r.status = 200; r.data = res.rows;
        }
        callback(r);
    });
}


export function CreateThread(board: string, t: Thread, callback: (r: { status: number, data: any }) => void) {
    let query = `
insert into ${board}.thread (
  timestamp, title, content, image, imagetype
) values (
  ${Math.floor(Date.now() / 1000)},
  '${t.title}', '${t.content}', '${t.image}', '${t.imagetype}'
);`;

    client.query(query, (err, res) => {
        let r: { status: number, data: any } = { status: 404, data: undefined };
        if (err) {
            r.data = err.message;
        } else {
            r.status = 200;
        }
        callback(r);
    });
}

export function GetThread(board: string, tid: string, callback: (r: { status: number, data: any }) => void) {
    // copied from https://www.youtube.com/watch?v=_lLqt5exlMs&t=114s
    let query = `
with thread_comments as (
  select DISTINCT c.tid, (
    select jsonb_agg(_)
    from (select id, timestamp, content, image, imagetype
      from ${board}.comment cc where cc.tid = c.tid)
    as _ order by c.timestamp asc
    ) as comment
  from ${board}.comment c
)
select t.*, COALESCE(c.comment, '[]'::jsonb) as comments
from ${board}.thread t
left join thread_comments c on (c.tid = t.id)
where t.id = ${tid};`;

    client.query(query, (err, res) => {
        let r: { status: number, data: any } = { status: 404, data: undefined };
        if (err) {
            r.data = err.message;
        } else if(res.rows.length != 0) {
            r.status = 200; r.data = (res.rows as Array<Thread>)[0];
        }
        callback(r);
    });
}

export function CreateComment(board: string, tid: string, comment: Comment, callback: (r: { status: number, data: any }) => void) {
    let query = `
insert into ${board}.comment (
  tid, timestamp, content, image, imagetype
) values (
  ${tid}, ${Math.floor(Date.now() / 1000)},
  '${comment.content}', '${comment.image}', '${comment.imagetype}'
);`;

    client.query(query, (err, res) => {
        let r: { status: number, data: any } = { status: 404, data: undefined };
        if (err) {
            r.data = err.message;
        } else {
            r.status = 200;
        }
        callback(r);
    });
}