DEV Community

ihaback
ihaback

Posted on

Booking Typescript API with Express, typeORM and Postgres

Simple booking api attempt with Typescript, Express, TypeORM and Postgres.
Testing with Jest & Supertest.

The code

Project repo

Clone project

git clone git@github.com:ihaback/booking-api.git
Enter fullscreen mode Exit fullscreen mode

Project setup

npm install
Enter fullscreen mode Exit fullscreen mode

Prerequisites

  • Node
  • Docker

Project setup

npm install
Enter fullscreen mode Exit fullscreen mode

Rename .env.example to env

Change NODE_ENV to prod if you want to test building prod version locally.

NODE_ENV=dev
DB_TYPE=postgres
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_INSTANCE=postgres
DB_SYNCHRONIZE=true
JWT_SECRET=secret
Enter fullscreen mode Exit fullscreen mode

Run docker compose for Postgres DB

docker-compose up
Enter fullscreen mode Exit fullscreen mode

Start dev server and seed database with initial data

npm run dev
Enter fullscreen mode Exit fullscreen mode

Run tests against Postgres DB with jest & supertest

npm run test
Enter fullscreen mode Exit fullscreen mode

Lint code to detect issues

npm run lint
Enter fullscreen mode Exit fullscreen mode

Build code for production

Make sure your NODE_ENV is set to prod.

npm run build
Enter fullscreen mode Exit fullscreen mode

Login to receive jwt token for subsequent request

POST http://localhost:3000/api/auth/login
Enter fullscreen mode Exit fullscreen mode
{
  "username": "admin",
  "password": "admin"
}
Enter fullscreen mode Exit fullscreen mode

Use token from login respone in the auth header for subsequent request

generated-token
Enter fullscreen mode Exit fullscreen mode

Create booking

POST http://localhost:3000/api/bookings
Enter fullscreen mode Exit fullscreen mode
{
  "startDate": "2022-03-24 18:46:55.223294",
  "endDate": "2022-03-24 18:46:55.223294",
  "cost": 100,
  "destinationId": 1 // id for destination
}
Enter fullscreen mode Exit fullscreen mode

Get all bookings

GET http://localhost:3000/api/bookings
Enter fullscreen mode Exit fullscreen mode

Get single booking

GET http://localhost:3000/api/bookings/:id
Enter fullscreen mode Exit fullscreen mode

Update booking

PUT http://localhost:3000/api/bookings/:id
Enter fullscreen mode Exit fullscreen mode
{
  "startDate": "2022-03-24 18:46:55.223294",
  "endDate": "2022-03-24 18:46:55.223294",
  "cost": 10000
}
Enter fullscreen mode Exit fullscreen mode

Delete booking

DELETE http://localhost:3000/api/bookings/:id
Enter fullscreen mode Exit fullscreen mode

Create destination

POST http://localhost:3000/api/destinations
Enter fullscreen mode Exit fullscreen mode
{
  "name": "New York",
  "description": "description",
  "state": "New York",
  "city": "New York",
  "cost": 100,
  "maxGuests": 2,
  "available": true
}
Enter fullscreen mode Exit fullscreen mode

Get all destinations

GET http://localhost:3000/api/destinations
Enter fullscreen mode Exit fullscreen mode

Get single destination

GET http://localhost:3000/api/destinations/:id
Enter fullscreen mode Exit fullscreen mode

Update destination

PUT http://localhost:3000/api/destinations/:id
Enter fullscreen mode Exit fullscreen mode
{
  "name": "Los Angeles",
  "state": "California",
  "city": "Los Angeles",
  "cost": 100,
  "maxGuests": 2
}
Enter fullscreen mode Exit fullscreen mode

Delete destination

DELETE http://localhost:3000/api/destinations/:id
Enter fullscreen mode Exit fullscreen mode

Top comments (0)