From ff77c14beca90be7a63fb862a91bb72120b09cb8 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Sun, 22 Sep 2019 20:14:04 +0200 Subject: [PATCH] Fixed Database Integration - fixed wrong table creation path - added table update path --- src/lib/QueryHelper.ts | 16 ++++++++++++++-- src/lib/dataaccess/DataObject.ts | 8 +++++++- src/lib/dataaccess/Post.ts | 6 ++++-- src/lib/dataaccess/User.ts | 1 + src/lib/dataaccess/index.ts | 7 +++++-- src/sql/create-tables.sql | 2 +- src/sql/update-tables.sql | 3 +++ 7 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 src/sql/update-tables.sql diff --git a/src/lib/QueryHelper.ts b/src/lib/QueryHelper.ts index 7586276..5970f70 100644 --- a/src/lib/QueryHelper.ts +++ b/src/lib/QueryHelper.ts @@ -68,9 +68,10 @@ export class QueryHelper { /** * Constructor. * @param pgPool - * @param tableCreationFile + * @param [tableCreationFile] + * @param [tableUpdateFile] */ - constructor(pgPool: Pool, private tableCreationFile?: string) { + constructor(pgPool: Pool, private tableCreationFile?: string, private tableUpdateFile?: string) { this.pool = pgPool; } @@ -85,6 +86,17 @@ export class QueryHelper { } } + /** + * Updates the definition of the tables if the table update file was passed in the constructor + */ + public async updateTableDefinitions() { + if (this.tableUpdateFile) { + logger.info("Updating table definitions..."); + const tableSql = await fsx.readFile(this.tableUpdateFile, "utf-8"); + await this.query({text: tableSql}); + } + } + /** * executes the sql query with values and returns all results. * @param query diff --git a/src/lib/dataaccess/DataObject.ts b/src/lib/dataaccess/DataObject.ts index afabc32..1890425 100644 --- a/src/lib/dataaccess/DataObject.ts +++ b/src/lib/dataaccess/DataObject.ts @@ -1,4 +1,7 @@ -abstract class DataObject { +/** + * abstact DataObject class + */ +export abstract class DataObject { protected dataLoaded: boolean = false; constructor(public id: number, protected row?: any) { @@ -6,6 +9,9 @@ abstract class DataObject { protected abstract loadData(): Promise; + /** + * Loads data from the database if data has not been loaded + */ protected loadDataIfNotExists() { if (this.dataLoaded) { this.loadData(); diff --git a/src/lib/dataaccess/Post.ts b/src/lib/dataaccess/Post.ts index 51c6b4c..b4193c4 100644 --- a/src/lib/dataaccess/Post.ts +++ b/src/lib/dataaccess/Post.ts @@ -1,4 +1,6 @@ -import {queryHelper, VoteType} from "./index"; +import {DataObject} from "./DataObject"; +import {queryHelper} from "./index"; +import dataaccess from "./index"; import {User} from "./User"; export class Post extends DataObject { @@ -63,7 +65,7 @@ export class Post extends DataObject { /** * The type of vote the user performed on the post. */ - public async userVote(userId: number): Promise { + public async userVote(userId: number): Promise { const result = await queryHelper.first({ text: "SELECT vote_type FROM votes WHERE user_id = $1 AND item_id = $2", values: [userId, this.id], diff --git a/src/lib/dataaccess/User.ts b/src/lib/dataaccess/User.ts index ea7c7c9..bb680c4 100644 --- a/src/lib/dataaccess/User.ts +++ b/src/lib/dataaccess/User.ts @@ -1,3 +1,4 @@ +import {DataObject} from "./DataObject"; import {queryHelper} from "./index"; import {Post} from "./Post"; diff --git a/src/lib/dataaccess/index.ts b/src/lib/dataaccess/index.ts index 8019329..1d610ef 100644 --- a/src/lib/dataaccess/index.ts +++ b/src/lib/dataaccess/index.ts @@ -4,7 +4,9 @@ import {QueryHelper} from "../QueryHelper"; import {User} from "./User"; const config = globals.config; -const tableCreationFile = __dirname + "/../sql/create-tables.sql"; +const tableCreationFile = __dirname + "/../../sql/create-tables.sql"; +const tableUpdateFile = __dirname + "/../../sql/update-tables.sql"; + const dbClient: Pool = new Pool({ database: config.database.database, host: config.database.host, @@ -12,13 +14,14 @@ const dbClient: Pool = new Pool({ port: config.database.port, user: config.database.user, }); -export const queryHelper = new QueryHelper(dbClient, tableCreationFile); +export const queryHelper = new QueryHelper(dbClient, tableCreationFile, tableUpdateFile); namespace dataaccess { /** * Initializes everything that needs to be initialized asynchronous. */ export async function init() { + await queryHelper.updateTableDefinitions(); await queryHelper.createTables(); } diff --git a/src/sql/create-tables.sql b/src/sql/create-tables.sql index 7a6292e..7c1c97a 100644 --- a/src/sql/create-tables.sql +++ b/src/sql/create-tables.sql @@ -21,7 +21,7 @@ CREATE TABLE IF NOT EXISTS posts ( CREATE TABLE IF NOT EXISTS votes ( user_id SERIAL REFERENCES users (id) ON DELETE CASCADE, item_id BIGSERIAL REFERENCES posts (id) ON DELETE CASCADE, - vote_type varchar(8) DEFAULT 1 + vote_type varchar(8) DEFAULT 'upvote' ); CREATE TABLE IF NOT EXISTS events ( diff --git a/src/sql/update-tables.sql b/src/sql/update-tables.sql new file mode 100644 index 0000000..52ce2f6 --- /dev/null +++ b/src/sql/update-tables.sql @@ -0,0 +1,3 @@ +ALTER TABLE IF EXISTS votes + ADD COLUMN IF NOT EXISTS vote_type varchar(8) DEFAULT 'upvote', + ALTER COLUMN vote_type SET DEFAULT 'upvote';