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';