Published on

Booking Typescript API with Express, typeORM and Postgres

Overview

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

Project setup

npm install

Prerequisites

  • Node
  • Docker

Project setup

npm install

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

Run docker compose for Postgres DB

docker-compose up

Start dev server and seed database with initial data

npm run dev

Run tests against Postgres DB with jest & supertest

npm run test

Lint code to detect issues

npm run lint

Build code for production

Make sure your NODE_ENV is set to prod.

npm run build

Login to receive jwt token for subsequent request

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

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

generated-token

Create booking

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

Get all bookings

GET http://localhost:3000/api/bookings

Get single booking

GET http://localhost:3000/api/bookings/:id

Update booking

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

Delete booking

DELETE http://localhost:3000/api/bookings/:id

Create destination

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

Get all destinations

GET http://localhost:3000/api/destinations

Get single destination

GET http://localhost:3000/api/destinations/:id

Update destination

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

Delete destination

DELETE http://localhost:3000/api/destinations/:id