Fixed Database Integration

- fixed wrong table creation path
- added table update path
pull/1/head
Trivernis 5 years ago
parent 6765026742
commit ff77c14bec

@ -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

@ -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<void>;
/**
* Loads data from the database if data has not been loaded
*/
protected loadDataIfNotExists() {
if (this.dataLoaded) {
this.loadData();

@ -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<VoteType> {
public async userVote(userId: number): Promise<dataaccess.VoteType> {
const result = await queryHelper.first({
text: "SELECT vote_type FROM votes WHERE user_id = $1 AND item_id = $2",
values: [userId, this.id],

@ -1,3 +1,4 @@
import {DataObject} from "./DataObject";
import {queryHelper} from "./index";
import {Post} from "./Post";

@ -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();
}

@ -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 (

@ -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';
Loading…
Cancel
Save