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/coffee-shop-backend
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 (you can modify the values according to your own setup). Example:
DATABASE_URL="postgresql://user:password@localhost:5432/coffee_shop?schema=public"
The docker-compose.yml
file defines the services for PostgreSQL:
version: "3.7"
services:
postgres:
image: bitnami/postgresql:latest
ports:
- "5432:5432"
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=coffee_shop
volumes:
- coffee_shop_pg_data:/bitnami/postgresql
volumes:
coffee_shop_pg_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 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, check for any local PostgreSQL instance that might be interfering with the Prisma migration for the Docker VM.
To start the server, use the following command:
npm run dev
Now you're ready to go! The server is successfully running, and the backend is prepared to accept and communicate with the frontend application.
The frontend application can be found here: https://github.com/luisggf/coffee-shop-frontend
OBS: To rerun this server at any time, use the following sequence of commands:
docker compose up
npm run dev
- Note that the Docker Desktop program must be running.