# Setup Guide

Author: Nguyen Thi Nguyet Que Created on: May 2, 2026

## Prerequisites

- Node.js 20+
- PostgreSQL 14+
- Redis 6+
- pnpm

---

## 1. Install dependencies

```bash
pnpm install
```

---

## 2. Configure environment

```bash
cp .env.example .env
```

Minimum required values to fill in before running the app:

```env
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=bekind

REDIS_HOST=localhost
REDIS_PORT=6379

JWT_SECRET=minimum_32_characters_secret_here
JWT_REFRESH_SECRET=minimum_32_characters_secret_here
TOKEN_ENCRYPTION_KEY=exactly_32_characters_key_here!!
```

See `.env.example` for the full list (email, storage, notifications, virus scan, etc.).

### All environment variables

| Variable               | Description                             | Required          |
| ---------------------- | --------------------------------------- | ----------------- |
| `DB_HOST`              | PostgreSQL host                         | Yes               |
| `DB_PORT`              | PostgreSQL port                         | Yes               |
| `DB_NAME`              | Database name                           | Yes               |
| `DB_USER`              | Database user                           | Yes               |
| `DB_PASSWORD`          | Database password                       | Yes               |
| `REDIS_HOST`           | Redis host (rate limiting, sessions)    | Yes               |
| `REDIS_PORT`           | Redis port                              | Yes               |
| `JWT_SECRET`           | JWT secret, min 32 chars                | Yes               |
| `JWT_REFRESH_SECRET`   | JWT refresh secret, min 32 chars        | Yes               |
| `TOKEN_ENCRYPTION_KEY` | Token encryption key, exactly 32 chars  | Yes               |
| `PORT`                 | Server port                             | No (default 3001) |
| `NODE_ENV`             | Environment                             | No (development)  |
| `BACKEND_URL`          | Public URL (used in Swagger server URL) | No                |
| `STORAGE_PROVIDER`     | `local` or `minio`                      | No (local)        |

---

## 3. Create database

PostgreSQL must be running before this step.

```bash
psql -U postgres -c "CREATE DATABASE bekind;"
```

---

## 4. Run migrations

```bash
pnpm migrate
```

To run a specific migration only:

```bash
node sql/scripts/run-003.js
```

---

## 5. Generate models

Only required when the database schema changes.

```bash
pnpm gen-db
```

| Command             | Description                           |
| ------------------- | ------------------------------------- |
| `pnpm gen-db`       | Generate models from database         |
| `pnpm gen-db:force` | Generate and overwrite existing files |
| `pnpm gen-db:clean` | Delete old models and regenerate      |

> Generated models in `src/models/` are auto-generated — do not edit them by hand.

---

## 6. Run development server

```bash
pnpm dev
```

Server: `http://localhost:3001` — Swagger UI: `http://localhost:3001/swagger/index`

---

## Docker

```bash
# Development
docker-compose up --build

# Production
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
```
