aboutsummaryrefslogtreecommitdiff
path: root/src/database/postgres.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/database/postgres.ts')
-rw-r--r--src/database/postgres.ts74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/database/postgres.ts b/src/database/postgres.ts
new file mode 100644
index 0000000..2ea19bd
--- /dev/null
+++ b/src/database/postgres.ts
@@ -0,0 +1,74 @@
+import { Client } from 'pg';
+var connectionString = "postgres://scale:secret@db/scale?sslmode=disable";
+let client: Client;
+
+export function InitPostgres() {
+ client = new Client(connectionString);
+ client.connect();
+}
+
+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: 500, data: undefined };
+ if (err) {
+ r.status = 404; 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}.threads(
+ id serial primary key,
+ timestamp bigint,
+ title text,
+ content text,
+ image text,
+ imagetype text
+);
+create table ${name}.comments(
+ id serial primary key,
+ timestamp bigint,
+ content text,
+ image text,
+ imagetype text
+);`;
+
+ 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);
+ });
+}