Commit e3156c52 authored by Matheus Horstmann's avatar Matheus Horstmann 🐴 Committed by Lucas Fernandes de Oliveira

Issue #1: Create Database Handler and Config file

Signed-off-by: Matheus Horstmann's avatarMatheus Horstmann <mch15@inf.ufpr.br>
parent 448986fe
image: node:10-stretch
services:
- postgres:10
variables:
POSTGRES_DB: 'form-creator'
POSTGRES_HOST: 'postgres'
PGPASSWORD: '123mudar'
POSTGRES_PASSWORD: '123mudar'
POSTGRES_USER: 'runner'
POSTGRES_PASSWORD: '123mudar'
POSTGRES_PORT: 5432
cache:
paths:
- node_modules
......@@ -9,9 +19,21 @@ stages:
- build
- deploy
run_test:
stage: test
before_script:
- apt-get update -q -y
- apt-get install wget gnupg -y
- echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" > /etc/apt/sources.list.d/pgdg.list
- wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
- apt-get update -q -y
- apt-get install -y postgresql-client-10
- git clone --recurse-submodules https://gitlab.c3sl.ufpr.br/simmctic/form-creator/form-creator-database.git form-creator-database
- cd form-creator-database
- psql-manager/manager.sh create workspace
- psql-manager/manager.sh fixture workspace
- cd ..
- rm -rf form-creator-database
script:
- yarn install --frozen-lockfile --silent --non-interactive
- ln -s config.env.example config/test.env
......
......@@ -5,6 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 0.0.5 - 19-03-2019
### Changed
- Remove tslint-stylish from package.json, package is deprecated (Horstmann)
- Update yarn.lock to avoid vulnerabilities (Horstmann)
- Update CI file to handle database (Horstmann)
### Added
- Class config using singleton patern, to centralize all configuration in one module (Horstmann)
- DbHandler to be a layer between API and database #1 (Horstmann)
## 0.0.4 - 12-02-2019
### Added
- Class Form #3 (Horstmann)
......
{
"name": "form-creator-api",
"version": "0.0.3",
"version": "0.0.5",
"description": "RESTful API used to manage and answer forms.",
"main": "index.js",
"scripts": {
"start": "scripts/start.sh config/config.env"
, "test": "scripts/test.sh config/test.env"
, "lint": "tslint -s node_modules/tslint-stylish -t stylish src/**/*.ts test/**/*.ts"
, "show-coverage": "xdg-open coverage/lcov-report/index.html"
, "doc-code": "typedoc --mode 'file' --module 'commonjs' --target 'ES6' --ignoreCompilerErrors --exclude '**/*.spec.ts' --out 'doc/code' 'src'"
"start": "scripts/start.sh config/config.env",
"test": "scripts/test.sh config/test.env",
"lint": "tslint -s node_modules/tslint-stylish -t stylish src/**/*.ts test/**/*.ts",
"show-coverage": "xdg-open coverage/lcov-report/index.html",
"doc-code": "typedoc --mode 'file' --module 'commonjs' --target 'ES6' --ignoreCompilerErrors --exclude '**/*.spec.ts' --out 'doc/code' 'src'"
},
"repository": {
"type": "git",
......@@ -19,8 +19,10 @@
"dependencies": {
"@types/async": "^2.0.50",
"@types/express": "^4.16.0",
"@types/pg": "^7.4.13",
"async": "^2.6.1",
"express": "^4.16.4",
"pg": "^7.8.1",
"ts-node": "^7.0.1",
"typescript": "^3.2.2"
},
......@@ -32,8 +34,7 @@
"istanbul": "1.1.0-alpha.1",
"mocha": "^5.2.0",
"supertest": "^3.3.0",
"tslint": "^5.12.1",
"tslint-stylish": "^2.1.0",
"tslint": "^5.13.1",
"typedoc": "^0.14.1"
}
}
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { expect } from "chai";
import { configs } from "./config";
describe("Config Handler", () => {
it("should test configs", () => {
expect(configs.poolconfig["user"]).to.be.equal(process.env["POSTGRES_USER"]);
expect(configs.poolconfig["host"]).to.be.equal(process.env["POSTGRES_HOST"]);
expect(configs.poolconfig["database"]).to.be.equal(process.env["POSTGRES_DB"]);
expect(configs.poolconfig["password"]).to.be.equal(process.env["POSTGRES_PASSWORD"]);
expect(configs.poolconfig["port"]).to.be.equal(parseInt(process.env["POSTGRES_PORT"], 10));
});
});
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { PoolConfig } from "pg";
class Config {
private static instance: Config;
private user: string;
private host: string;
private database: string;
private password: string;
private port: number;
public readonly poolconfig: PoolConfig;
private constructor(){
this.user = process.env["POSTGRES_USER"];
this.host = process.env["POSTGRES_HOST"];
this.database = process.env["POSTGRES_DB"];
this.password = process.env["POSTGRES_PASSWORD"];
this.port = parseInt(process.env["POSTGRES_PORT"], 10);
this.poolconfig = {
user: this.user,
database: this.database,
password: this.password,
host: this.host,
port: this.port,
max: 10,
idleTimeoutMillis: 3000
};
}
public static get Configurations(){
return this.instance || (this.instance = new this());
}
}
export const configs = Config.Configurations;
This diff is collapsed.
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Pool, PoolConfig, QueryResult } from "pg";
/**
* Class of the SGBD from the Form Creator Api perspective. Used to
* perform all the operations into the database that the Form Creator Api
* requires. This operations include read and write data.
*/
export class DbHandler {
/** Information used to connect with a PostgreSQL database. */
private pool: Pool;
/**
* Creates a new adapter with the database connection configuration.
* @param config - The information required to create a connection with
* the database.
*/
constructor(config: PoolConfig) {
this.pool = new Pool(config);
}
/**
* Asynchronously executes a query and get its result.
* @param query - Query (SQL format) to be executed.
* @param cb - Callback function which contains the data read.
* @param cb.error - Error information when the method fails.
* @param cb.result - Query result.
*/
public executeQuery(query: string, cb: (err: Error, result?: QueryResult) => void): void{
this.pool.connect((err, client, done) => {
if (err) {
cb(err);
return;
}
client.query(query, [], (error, result) => {
// call 'done()' to release client back to pool
done();
cb(error, (result) ? result : null);
});
});
}
/**
* Asynchronously ends a transaction
*/
public commit(cb: (err: Error, result?: QueryResult) => void){
this.executeQuery("COMMIT;", cb);
}
/**
* Asynchronously starts a transaction
*/
public begin(cb: (err: Error, result?: QueryResult) => void){
this.executeQuery("BEGIN;", cb);
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment