This project is an API that serves a polling application, where users can create polls, other users can reply to them, and the results can be accessed in real-time. The backend is built using Node.js, Fastify, Prisma, PostgreSQL, and Redis, with WebSocket integration for real-time updates.
- REST API
- Docker
- PostgreSQL
- Redis
- Fastify
- Prisma
- Cookies
- WebSockets
- Pub/Sub
Create a new Node.js project:
npm init -y
TypeScript is used for data typing in JavaScript:
npm install typescript @types/node -D
npx tsc --init
Install tsx
as a development dependency to run TypeScript files:
npm install tsx -D
Prisma is an ORM that simplifies data manipulation:
npm install prisma -D
npx prisma init
To manage your database schema, you can use (for visualization of the data):
npx prisma studio
To apply schema changes and run migrations:
npx prisma migrate dev
Clone this repository to your local machine:
git clone https://github.com/luisggf/polls-nlw-expert
cd <repository-directory>
The package.json file is already configured with all the necessary dependencies. To install them, simply run:
npm install
Create a .env
file in the root directory with the following contents (i kept my local .env and .yml configs in this repository but you can change it to your own). Example:
DATABASE_URL="postgresql://user:password@localhost:5432/polls?schema=public"
The docker-compose.yml
file defines the services for PostgreSQL and Redis:
version: "3.7"
services:
postgres:
image: bitnami/postgresql:latest
ports:
- "5432:5432"
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=polls
volumes:
- polls_pg_data:/bitnami/postgresql
redis:
image: bitnami/redis:latest
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
- "6379:6379"
volumes:
- "polls_redis_data:/bitnami/redis/data"
volumes:
polls_pg_data:
polls_redis_data:
- Ensure that the
POSTGRES_USER
andPOSTGRES_PASSWORD
in thedocker-compose.yml
file match the credentials specified in the.env
file. - Use the
DATABASE_URL
in the.env
file to connect to the PostgreSQL database.
Use Docker to start PostgreSQL and Redis containers:
docker compose up -d
After setting up the Docker containers, apply the latest database migrations:
npx prisma migrate dev
In case this command doesn't work make sure to check if there's any PostGres instance running locally in the background. This programs interfere directly with the Prisma migration for the Docker VM.
This project has a seed script to populate the database with initial data, run:
npm run seed
In case this command doesn't work make sure to check if the ts-node dependency is installed. In case you run into problems related to the ts-node, please run:
npm install -g ts-node
To start the server the following inline command can be used:
npm run dev
Now you're good to go, the server is sucessfully running and the Backend is ready to accept/communicate with client side application :) The Frontend application can be found here: https://github.com/luisggf/polls-front
OBS: To rerun this server other times you need to run, in sequence, this commands:
docker compose up
npm run dev
- Note that the Docker Desktop program must be running.