https://github.com/mhaidarhanif/median-api
Median API
https://github.com/mhaidarhanif/median-api
api backend median nestjs prisma swagger
Last synced: about 2 months ago
JSON representation
Median API
- Host: GitHub
- URL: https://github.com/mhaidarhanif/median-api
- Owner: mhaidarhanif
- License: mit
- Created: 2023-06-04T15:59:55.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-05T00:47:55.000Z (about 3 years ago)
- Last Synced: 2025-01-14T00:45:13.236Z (over 1 year ago)
- Topics: api, backend, median, nestjs, prisma, swagger
- Language: TypeScript
- Homepage: https://my-backend-infra.mhaidarhanif.com
- Size: 503 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
NestJS is a progressive Node.js framework for building efficient and scalable server-side applications.
# My Backend Infra
Simple REST API made with [NestJS](https://github.com/nestjs/nest) and Containerized with Docker.
- Live:
- Images:
-
-
- Repo:
- Status:
## API Documentation
Base API URLs:
-
-
Endpoints:
| HTTP | Endpoint | Description |
|:-------- |:------------ |:----------- |
| `GET` | `/` | Show welcome |
| `POST` | `/auth/register` | Register new user |
| `POST` | `/auth/login` | Login to user |
| `GET` | `/articles` | Get all articles |
| `GET` | `/articles/drafts` | Get all draft articles |
| `GET` | `/articles/:id` | Get one article by id |
| `POST` | `/articles` | Create new article |
| `PUT` | `/articles/:id` | Update one article by id |
| `PATCH` | `/articles/:id` | Update one article by id |
| `DELETE` | `/articles` | Delete all articles |
| `DELETE` | `/articles/:id` | Delete one article by id |
## Architecture Diagram

## Prepare Database
Make sure the database is ready before doing any dependency installation.
Edit `.env` file for the app:
```sh
DATABASE_URL="postgres://myuser:mypassword@localhost:5432/db"
JWT_SECRET="abdefghijklmnopqrstuvwxyzabcdefghi"
UNSPLASH_ACCESS_KEY="abdefghijklmnopqrstuvwxyzabcdefghi"
```
- `DATABASE_URL`, can be retreived from your choice:
- Local database instance
- Local database container, explained below to run it
- `JWT_SECRET`, recommended to generate with `scripts/random.sh`
- `UNSPLASH_ACCESS_KEY`, get it from Unsplash Developer app portal
Run Docker on your machine and run Docker Compose that specifically only run the database instance in the background:
```sh
$ docker compose -f docker-compose.dev.yaml up -d
```
## Install Dependencies
Install local dependencies:
```sh
$ pnpm i
```
Install global dependencies:
```sh
$ pnpm i -g @nestjs/cli
$ pnpm i -g prettier
$ pnpm i -g eslint
```
Check available scripts/commands:
```sh
$ pnpm run
```
## Run for Development
Push schema to the database or generate Prisma schema while in development:
```sh
$ pnpm prisma:push # prisma db push
$ pnpm prisma:generate # prisma generate
```
Run the NestJS server:
```sh
# with watch
$ pnpm dev
# without watch
$ pnpm start
```
Then open on your browser.
## API Documentation with Swagger
After running the server on local, open on your browser. Or if already deployed, check the `/docs` route.
## Database Operation on Development
Check the Prisma commands accordingly:
```sh
$ pnpm prisma:format
$ pnpm prisma:validate
$ pnpm prisma:generate
$ pnpm prisma:push
$ pnpm prisma:studio
$ pnpm prisma:seed
```
## Build for Production
```sh
$ pnpm build
$ pnpm start:prod
```
## Build for Production with Container
Build the app image only:
```sh
$ docker compose build
```
Run Docker Compose for both app container and database container:
```
$ docker compose up
$ curl -i localhost:4000/api
```
To stop compose that was run without `-d`:
```sh
$ docker compose down
```
## Push the Image to Docker Hub
Login, tag, and push the image:
```
$ docker login
$ docker tag my-backend-infra mhaidarh/my-backend-infra
# or
$ docker tag my-backend-infra \
asia-southeast2-docker.pkg.dev/project-name/docker/my-backend-infra
$ docker push mhaidarh/my-backend-infra
# or
$ docker push \
asia-southeast2-docker.pkg.dev/project-name/docker/my-backend-infra
```
## Pull and Run the Container from Docker Hub
Check if it can be pulled and run:
```sh
$ docker pull mhaidarh/my-backend-infra
# or
$ docker pull \
asia-southeast2-docker.pkg.dev/project-name/docker/my-backend-infra
$ docker run -p 4000:4000 -d --name my-backend-infra-container mhaidarh/my-backend-infra
# or
$ docker run -p 4000:4000 -d --name my-backend-infra-container asia-southeast2-docker.pkg.dev/project-name/docker/my-backend-infra
```
Can also run via Docker Compose.
```sh
$ docker compose up
```
## Check Docker image size
```sh
docker inspect -f "{{ .Size }}" mhaidarh/my-backend-infra | numfmt --to=si
```
## Deployment on Railway or Render
1. Create a new project
2. Create a PostgreSQL instance
3. Connect GitHub repo to the project, add the environment variables
- `DATABASE_URL`, get automatically from Railway `${{Postgres.DATABASE_URL}}`
- `JWT_SECRET`, recommended to generate with `scripts/random.sh`
- `UNSPLASH_ACCESS_KEY`, get it from Unsplash Developer app portal
## Test for Assurance
```sh
# unit tests
$ pnpm test
# e2e tests
$ pnpm test:e2e
# test coverage
$ pnpm test:cov
```
## License
[MIT](LICENSE).
## References
- [NestJS + Redis + Postgres Local Development With Docker Compose](https://tomray.dev/nestjs-docker-compose-postgres)
- [AlexSKuznetsov/prisma-express](https://github.com/AlexSKuznetsov/prisma-express)