aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.ts
blob: b2c9c1ef489e48071984de5bf4e4c83659526aaf (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
import {
  createPool,
  QueryResultType,
  sql,
} from "slonik";
import config from "./config";

interface ActiveChat {
  id: number;
}

interface PermittedUser {
  id: number;
}

const connection = createPool(config.pgConnectionUri);

export const getActiveChats = async (): Promise<readonly ActiveChat[]> =>
  connection.any(sql`
    SELECT *
    FROM active_chats
  `);

export const addActiveChat = async (id: number): Promise<QueryResultType<void>> =>
  connection.query<void>(sql`
    INSERT INTO active_chats
    (id) VALUES
    (${id})
  `);

export const removeActiveChat = async (id: number): Promise<QueryResultType<void>> =>
  connection.query<void>(sql`
    DELETE FROM active_chats
    WHERE id = ${id}
  `);

export const getPermittedUsers = async (): Promise<readonly PermittedUser[]> =>
  connection.any(sql`
    SELECT *
    FROM permitted_users
  `);