Skip to content

Commit

Permalink
Started PSQL Tables
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed May 8, 2024
1 parent fafbac0 commit 352de5f
Show file tree
Hide file tree
Showing 34 changed files with 157 additions and 14 deletions.
13 changes: 13 additions & 0 deletions code/server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 'latest'
services:
postgres-db:
container_name: postgres-db
build:
context: .
dockerfile: docker/Dockerfile-db
environment:
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=notespace
- POSTGRES_DB=notespace
ports:
- 5432:5432
4 changes: 4 additions & 0 deletions code/server/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM ubuntu:latest
LABEL authors="primu"

ENTRYPOINT ["top", "-b"]
11 changes: 11 additions & 0 deletions code/server/docker/Dockerfile-db
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM postgres

USER postgres
WORKDIR /app

COPY ./src/sql/create_tables.sql /docker-entrypoint-initdb.d/1_create-schema.sql

COPY --chown=postgres:postgres ./docker/scripts/wait-for-postgres.sh ./bin/wait-for-postgres.sh
RUN chmod +x ./bin/wait-for-postgres.sh

EXPOSE 5432
17 changes: 17 additions & 0 deletions code/server/docker/scripts/wait-for-postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
# wait-for-postgres.sh host cmd

set -e

host="$1"
shift
cmd="$@"

>&2 echo "waiting for postgres on $host"
until pg_isready -h $host; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done

>&2 echo "Postgres is up - executing command '$cmd'"
exec $cmd
10 changes: 5 additions & 5 deletions code/server/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ module.exports = {
moduleNameMapper: pathsToModuleNameMapper(
{
/*Controllers*/
'@controllers/*': ['./src/controllers/*'],
'@controllers/*': ['./src/ts/controllers/*'],
/*Databases*/
'@database/*': ['./src/database/*'],
'@database/*': ['./src/ts/database/*'],
/*Others*/
'@src/*': ['./src/*'],
'@domain/*': ['./src/domain/*'],
'@services/*': ['./src/services/*'],
'@src/*': ['./src/ts/*'],
'@domain/*': ['./src/ts/domain/*'],
'@services/*': ['./src/ts/services/*'],
},
{ prefix: '<rootDir>/' }
),
Expand Down
4 changes: 3 additions & 1 deletion code/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "1.0.0",
"scripts": {
"serve": "tsc && node dist/server.js",
"start": "tsx watch -r tsconfig-paths/register src/server.ts",
"start": "tsx watch -r tsconfig-paths/register src/ts/server.ts",
"test": "jest --detectOpenHandles --config jest.config.js",
"knip": "knip",
"format": "prettier --write . && eslint . --ext .ts"
Expand All @@ -19,6 +19,7 @@
"express": "^4.19.2",
"firebase-admin": "^12.1.0",
"lodash": "^4.17.21",
"pg": "^8.11.5",
"socket.io": "^4.7.5",
"supertest": "^6.3.4",
"uuid": "^9.0.1"
Expand All @@ -31,6 +32,7 @@
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.1",
"@types/node": "^20.12.10",
"@types/pg": "^8.11.6",
"@types/supertest": "^6.0.2",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^7.8.0",
Expand Down
9 changes: 9 additions & 0 deletions code/server/src/sql/clean_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
begin ;

-- Delete all rows from the resources table
delete from resource ;

-- Delete all rows from the workspaces table
delete from workspace ;

commit ;
29 changes: 29 additions & 0 deletions code/server/src/sql/create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
begin;

create extension if not exists "uuid-ossp";

create type resource_type as enum ('D', 'F');

create table if not exists workspace (
id uuid primary key default uuid_generate_v4(),
name text not null,
created_at timestamp not null default now(),
updated_at timestamp not null default now()
);

create table if not exists resource (
id uuid primary key default uuid_generate_v4(),
workspace uuid not null references workspace(id) on delete cascade,
name text not null,
type resource_type not null,
created_at timestamp not null default now(),
updated_at timestamp not null default now()
);

create table if not exists resource_child (
parent uuid not null references resource(id) on delete cascade,
child uuid not null references resource(id) on delete cascade,
primary key (parent, child)
);

commit;
9 changes: 9 additions & 0 deletions code/server/src/sql/drop_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
begin ;

drop table if exists resources cascade ;

drop table if exists workspaces cascade ;

drop type if exists resource_type cascade ;

commit ;
Empty file.
15 changes: 15 additions & 0 deletions code/server/src/sql/triggers/resources_triggers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
begin ;

-- Create the trigger function
create or replace function delete_resource_on_child_delete() returns trigger as $$
begin
delete from resource where id = old.child;
return old;
end;
$$ language plpgsql;

create trigger delete_resource_trigger
after delete on resource_child
for each row execute function delete_resource_on_child_delete();

commit ;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions code/server/src/ts/database/pg/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { config } from 'dotenv';

config();

const PG_USER = process.env.PG_USER;
const PG_PASSWORD = process.env.PG_PASSWORD;
const PG_HOST = process.env.PG_HOST;
const PG_PORT = process.env.PG_PORT || '5432';
const PG_DATABASE = process.env.PG_DATABASE;

export default {
PG_USER,
PG_PASSWORD,
PG_HOST,
PG_PORT,
PG_DATABASE,
};
17 changes: 17 additions & 0 deletions code/server/src/ts/database/pg/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pg from 'pg';
import dbConfig from './config';

const credentials = {
user: dbConfig.PG_USER,
host: dbConfig.PG_HOST,
database: dbConfig.PG_DATABASE,
password: dbConfig.PG_PASSWORD,
port: parseInt(dbConfig.PG_PORT),
};

const connect = async () => {
const { Client } = pg;
const client = new Client(credentials);
await client.connect();
return client;
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion code/server/tests/conflict-resolution/crdt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InsertOperation, DeleteOperation, Operation } from '@notespace/shared/c
import { FugueTree } from '@notespace/shared/crdt/FugueTree';
import request = require('supertest');
import { Server } from 'socket.io';
import server from '../../src/server';
import server from '../../src/ts/server';
import { applyOperations } from './utils';

const { app, onConnectionHandler } = server;
Expand Down
14 changes: 7 additions & 7 deletions code/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"strict": true,
"skipLibCheck": true,
"downlevelIteration": true,
"baseUrl": "../",
"baseUrl": "./",
"paths": {
"@/*": ["./server/*"],
"@src/*": ["./server/src/*"],
"@controllers/*": ["./server/src/controllers/*"],
"@database/*": ["./server/src/database/*"],
"@domain/*": ["./server/src/domain/*"],
"@services/*": ["./server/src/services/*"]
"@/*": ["./*"],
"@src/*": ["./src/ts/*"],
"@controllers/*": ["./src/ts/controllers/*"],
"@database/*": ["./src/ts/database/*"],
"@domain/*": ["./src/ts/domain/*"],
"@services/*": ["./src/ts/services/*"]
}
},
"include": ["src/**/*"],
Expand Down

0 comments on commit 352de5f

Please sign in to comment.