Skip to content

Commit

Permalink
#5 Start to work on TypeORM integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatansalas committed Jul 1, 2017
1 parent c63142a commit 9b3b83b
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 15 deletions.
7 changes: 6 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ HOST = localhost
PORT = 8080
NODE_ENV = development
LOG_LEVEL = debug
PROJECT_DIR = /usr/app
PROJECT_DIR = /usr/app
POSTGRES_PASSWORD=test
POSTGRES_USER=test
POSTGRES_DB=test
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
16 changes: 15 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
version: '3'
services:
db:
container_name: web-api-db
image: postgres:9.6.3-alpine
environment:
- POSTGRES_PASSWORD=test
- POSTGRES_USER=test
- POSTGRES_DB=test
web:
container_name: web-api
build: .
Expand All @@ -12,5 +19,12 @@ services:
- PROJECT_DIR=/usr/app/
- NODE_ENV=development
- LOG_LEVEL=debug
- POSTGRES_PASSWORD=test
- POSTGRES_USER=test
- POSTGRES_DB=test
- POSTGRES_HOST=0.0.0.0
- POSTGRES_PORT=5432
ports:
- "localhost:8080:8080"
- "localhost:8080:8080"
links:
- db
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"hapijs-status-monitor": "^0.5.0",
"inert": "^4.2.0",
"joi": "^10.6.0",
"pg": "^6.4.0",
"reflect-metadata": "^0.1.10",
"typeorm": "0.0.11",
"uuid": "^3.1.0",
"vision": "^4.1.1",
"winston": "^2.3.1",
Expand Down
16 changes: 11 additions & 5 deletions src/api/users/controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Hapi from 'hapi';
import * as Boom from 'boom';
import * as uuid from 'uuid';
import Database from "../../database";

import User from '../../model/user';
import {User} from '../../model/user';
import Utils from '../../helper/utils';
import Logger from '../../helper/logger';
import Repository from '../../repository';
Expand All @@ -14,14 +14,16 @@ class UserController {
try {
Logger.info(`POST - ${Utils.getUrl(request)}`);

const connection = await Database.init();
const userRepository = connection.getRepository(User);

const user = new User();

user.id = uuid();
user.age = request.payload.age;
user.name = request.payload.name;
user.lastName = request.payload.last_name;

this.repository.save(user.id, user);
await userRepository.create(user);

return response({
statusCode: 200,
Expand Down Expand Up @@ -65,7 +67,11 @@ class UserController {
Logger.info(`GET - ${Utils.getUrl(request)}`);

const id = encodeURIComponent(request.params.id);
const user = this.repository.getById(id);

const connection = await Database.init();
const userRepository = connection.getRepository(User);

const user = await userRepository.findOneById(id);

if (!user) {
return response(Boom.notFound('User not found'));
Expand Down
28 changes: 28 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Connection, createConnection} from "typeorm";

import Logger from './helper/logger';

export default class Database {
public static async init(): Promise<Connection> {
try {
Logger.info(`Creating db connection`);

return await createConnection({
driver: {
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
},
entities: [
__dirname + "/model/*.ts",
],
autoSchemaSync: true,
});
} catch (error) {
Logger.info(`Something went wrong when creating connection -> ${error}`);
}
}
}
16 changes: 14 additions & 2 deletions src/model/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
export default class User {
public id: string;
import {Entity} from "typeorm/decorator/entity/Entity";
import {Column, PrimaryGeneratedColumn} from "typeorm";

@Entity()
export class User {

@PrimaryGeneratedColumn()
public id: number;

@Column()
public age: string;

@Column()
public name: string;

@Column()
public lastName: string;
}
4 changes: 1 addition & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'reflect-metadata';
import * as Hapi from 'hapi';

import Logger from './helper/logger';
Expand All @@ -9,9 +10,6 @@ class Server {
try {
const server = new Hapi.Server();

Logger.info('HOST -> ' + process.env.HOST);
Logger.info('PORT -> ' + process.env.PORT);

server.connection({
host: process.env.HOST,
port: process.env.PORT,
Expand Down
Loading

0 comments on commit 9b3b83b

Please sign in to comment.