Skip to content

Commit

Permalink
Merge pull request #33 from swaibat/ch-Postgress-setup-166116575
Browse files Browse the repository at this point in the history
#166116575 Setup postgresql and create User table
  • Loading branch information
Rumbiiha swaibu committed May 21, 2019
2 parents 7319ad2 + 6c41fea commit f076048
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
node_modules
package-lock.json
.nyc_output
Expand Down
55 changes: 55 additions & 0 deletions api/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pg from 'pg';
import dotenv from 'dotenv'
dotenv.config()

const config = {
user: process.env.user, // this is the db user credential
database:process.env.database,
password: process.env.password,
port: process.env.port,
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000,
};

const pool = new pg.Pool(config);

pool.on('connect', () => {
});

const createTables = () => {
const Users = `CREATE TABLE IF NOT EXISTS
users (
id SERIAL PRIMARY KEY,
firstName VARCHAR (128) NOT NULL,
lastName VARCHAR(128) NOT NULL,
address TEXT NOT NULL,
email VARCHAR (355) UNIQUE NOT NULL,
password VARCHAR(128) NOT NULL,
status VARCHAR(128) NOT NULL,
createdOn TIMESTAMP DEFAULT Now(),
modifiedOn TIMESTAMP NOT NULL,
isAdmin BOOLEAN NOT NULL
)`;

pool.query(Users)
.then((res) => {
return res;
})
.catch((err) => {
return err;
});
};

pool.on('remove', () => {
process.exit(0);
});


// export pool and createTables to be accessible from an where within the application

export {
createTables,
pool,
}

require('make-runnable');
3 changes: 1 addition & 2 deletions api/controllers/loansController.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,4 @@ export class Loan {
loan.status = 'rejected';
res.status(200).send(loan);
}
}

}
6 changes: 4 additions & 2 deletions api/midleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Joi from '@hapi/joi';
import { users } from '../models/users';
import jwt from 'jsonwebtoken';
const appSecreteKey = 'hksuua7as77hjvb348b3j2hbrbsc9923k';
import dotenv from 'dotenv'
dotenv.config()


// validate token
export function ensureToken(req, res, next) {
Expand All @@ -11,7 +13,7 @@ export function ensureToken(req, res, next) {
token = token.slice(7, token.length);
}
if (token) {
jwt.verify(token, appSecreteKey, (err, decoded) => {
jwt.verify(token, process.env.appSecreteKey, (err, decoded) => {
if (err) {
return res.json({
error: 403,
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ app.use((error, req, res, next) => {
});


app.listen(process.env.PORT || 5000, () => console.log('listening on port 5000'));
app.listen(process.env.PORT || 3000, () => console.log('listening on port 5000'));
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
"test": "test"
},
"dependencies": {
"@babel/node": "^7.2.2",
"@babel/core": "^7.4.4",
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.4.4",
"@babel/register": "^7.4.4",
"@hapi/joi": "^15.0.1",
"dotenv": "^8.0.0",
"express": "^4.16.4",
"jsonwebtoken": "^8.5.1",
"jwt-decode": "^2.2.0",
"timeago.js": "^4.0.0-beta.2",
"swagger-ui-express": "^4.0.3"
"make-runnable": "^1.3.6",
"pg": "^7.11.0",
"swagger-ui-express": "^4.0.3",
"timeago.js": "^4.0.0-beta.2"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
Expand All @@ -42,7 +45,8 @@
"start": "babel-node index.js",
"dev": "nodemon --exec babel-node index.js ",
"test": "nyc mocha",
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls "
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls ",
"create": "babel-node ./api/config/db createTables"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit f076048

Please sign in to comment.