Skip to content

Latest commit

 

History

History
103 lines (74 loc) · 1.86 KB

README.md

File metadata and controls

103 lines (74 loc) · 1.86 KB

Node.js API with PostgreSQL and TypeScript DEMO

Starting project

Install project dependencies

yarn install

Create .env file

Rename the file .env.example to .env This step is important to run before running the prestart command

Run project prestart

yarn prestart

This will check for the .env file located at the project root level and load below environment variables

POSTGRES_DB: undefined;
POSTGRES_HOST: undefined;
POSTGRES_PASSWORD: undefined;
POSTGRES_USER: undefined;

Database Setup

Create Postgresql database with Docker command

docker run -d \
--name node-postgres-demo \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_DB=node-postgres-demo \
-p 5432:5432 \
postgres

Create Users Table from terminal

Get in docker container terminal

docker exec -it {container_identifier} bash

The {container_identifier} for Docker is the Container ID for the Docker instance You can get it using the below command

docker ps

Login to Postgres

psql -h localhost -p 5432 -U postgres

Go to node-postgres-demo

\c node-postgres-demo

Create users table

CREATE TABLE users (
 id SERIAL PRIMARY KEY,
 username VARCHAR(80),
 email VARCHAR(255),
 created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
 );

Create entry in Users table

$curl -H 'Content-Type: application/json' \
-d '{"username": "mateo", "email": "[email protected]"}' \
-X POST \
http://localhost:5000/api/v1/users

We can send JSON data using POST with curl using the --json option.

$curl --json '{"username": "mateo", "email": "[email protected]"}' http://localhost:5000/api/v1/users

Get all users

$curl -H 'Content-Type: application/json' \
http://localhost:5000/api/v1/users