# BeKind Backend

Backend template built with TypeScript, Node.js, Express.js and PostgreSQL. Features auto-routing,
Sequelize ORM, JWT authentication, Server-Sent Events (SSE), and OneSignal push notifications.

**Created by Nguyen Thi Nguyet Que**

## Quick Start

```bash
pnpm install
cp .env.example .env          # fill in DB, Redis, JWT values — see docs/setup.md
psql -U postgres -c "CREATE DATABASE bekind;"
pnpm migrate
pnpm dev
```

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

---

## Important Notes

⚠️ **Database Schema Changes**: NEVER modify database structure directly (SQL console, GUI tools,
etc.). Always use migration files in `sql/migrations/`. In production, restrict schema changes via
PostgreSQL permissions:

```sql
REVOKE CREATE ON SCHEMA public FROM app_user;
REVOKE ALTER ON ALL TABLES IN SCHEMA public FROM app_user;
```

---

## Stack

| Layer     | Technology                            |
| --------- | ------------------------------------- |
| Runtime   | Node.js 20 + TypeScript               |
| Framework | Express.js + express-automatic-routes |
| ORM       | Sequelize (PostgreSQL)                |
| Cache     | Redis                                 |
| Auth      | JWT + HttpOnly Cookies / Bearer       |
| Docs      | OpenAPI 3.0 (Zod + zod-to-openapi)    |

---

## Important Commands

### Development

| Command                       | Description                            |
| ----------------------------- | -------------------------------------- |
| `pnpm dev` / `pnpm start:dev` | Run development server with hot reload |
| `pnpm build`                  | Build for production                   |
| `pnpm start`                  | Run production server                  |

### Database

| Command        | Description                   |
| -------------- | ----------------------------- |
| `pnpm migrate` | Run SQL migrations            |
| `pnpm gen-db`  | Generate models from database |

### Quality

| Command                 | Description                                              |
| ----------------------- | -------------------------------------------------------- |
| `pnpm lint`             | ESLint check                                             |
| `pnpm type-check`       | TypeScript check                                         |
| `pnpm quality:check`    | Full check — lint + type-check + swagger + test coverage |
| `pnpm swagger:validate` | Generate then validate Swagger spec                      |

### Testing

| Command                 | Description                             |
| ----------------------- | --------------------------------------- |
| `pnpm test:unit`        | Unit tests only                         |
| `pnpm test:integration` | Integration tests only                  |
| `pnpm test:coverage`    | Full suite + coverage report            |
| `pnpm test:critical`    | Critical gate — auth + security (< 30s) |

---

## Docs

| Topic               | Link                                               |
| ------------------- | -------------------------------------------------- |
| Setup & Environment | [docs/setup.md](docs/setup.md)                     |
| New API Development | [docs/api-development.md](docs/api-development.md) |
| Error Handling      | [docs/error-handling.md](docs/error-handling.md)   |
| Configuration       | [docs/configuration.md](docs/configuration.md)     |
| Swagger / OpenAPI   | [docs/swagger.md](docs/swagger.md)                 |
| Architecture        | [docs/architecture.md](docs/architecture.md)       |
| Testing             | [docs/testing.md](docs/testing.md)                 |
| Conventions         | [docs/conventions.md](docs/conventions.md)         |
| Security            | [docs/security.md](docs/security.md)               |

<!-- Test -->
