|
1 | | -# Nestjs Prisma Template |
| 1 | +# NestJS Prisma API Template |
2 | 2 |
|
3 | | -This is an opinionated template for Nestjs prisma restful projects. |
| 3 | +A modern RESTful API template built with NestJS and Prisma, featuring comprehensive monitoring, logging, and deployment solutions. |
4 | 4 |
|
5 | | -## Features |
| 5 | +## ✨ Features |
6 | 6 |
|
7 | | -- [x] Platform: [Express](https://expressjs.com/) |
8 | | -- [x] Database: |
9 | | - - [x] ORM: [Prisma](https://prisma.io) |
10 | | - - [ ] Redis: [Redis](https://github.com/songkeys/nestjs-redis) |
11 | | -- Cache Manager: [cache-manager](https://github.com/nestjs/cache-manager) |
12 | | -- [x] Logger: [Pino](https://github.com/pinojs/pino) |
| 7 | +### 🚀 Core Technology Stack |
13 | 8 |
|
14 | | - - [nestjs-pino](https://github.com/iamolegga/nestjs-pino) |
15 | | - - [rotating-file-stream](https://github.com/iccicci/rotating-file-stream) |
16 | | - - [pino-pretty](https://github.com/pinojs/pino-pretty) in development |
17 | | - - [pino-loki](https://github.com/Julien-R44/pino-loki) |
| 9 | +- **Framework**: [NestJS](https://nestjs.com/) + [Express](https://expressjs.com/) |
| 10 | +- **Database**: [Prisma](https://prisma.io) ORM |
| 11 | +- **Logging**: [Pino](https://github.com/pinojs/pino) + [nestjs-pino](https://github.com/iamolegga/nestjs-pino) |
| 12 | +- **Caching**: [cache-manager](https://github.com/nestjs/cache-manager) |
18 | 13 |
|
19 | | -- [x] health |
20 | | -- [x] Authentication: [Passport](https://github.com/nestjs/passport) |
21 | | - - JWT: [passport-jwt](https://www.passportjs.org/packages/passport-jwt/) |
22 | | - - Google: [passport-google-oauth20](https://www.passportjs.org/packages/passport-google-oauth20/) |
23 | | -- [x] Swagger: [@nestjs/swagger](https://github.com/nestjs/swagger) |
24 | | -- [x] Tracing: [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel) |
25 | | - - [x] [grafana](https://grafana.com/docs/grafana/latest/) |
26 | | - - [x] [loki](https://grafana.com/docs/loki/latest/) |
27 | | - - [x] [prometheus](https://prometheus.io/) |
28 | | - - [x] [promtail](https://grafana.com/docs/loki/latest/send-data/promtail/) |
29 | | - - [x] [tempo](https://grafana.com/docs/tempo/latest) |
30 | | - - [x] [alertmanager](https://grafana.com/docs/grafana/latest/alerting/set-up/migrating-alerts/legacy-alerting/grafana-cloud-alerting/alertmanager/) |
31 | | -- [x] k8s |
| 14 | +### 🔐 Authentication & Authorization |
32 | 15 |
|
33 | | -## Guide |
| 16 | +- **Auth Strategy**: [Passport](https://github.com/nestjs/passport) |
| 17 | + - JWT Authentication: [passport-jwt](https://www.passportjs.org/packages/passport-jwt/) |
| 18 | + - Google OAuth: [passport-google-oauth20](https://www.passportjs.org/packages/passport-google-oauth20/) |
34 | 19 |
|
35 | | -### Development |
| 20 | +### 📊 Monitoring & Observability |
36 | 21 |
|
37 | | -Init environment variables: |
| 22 | +- **API Documentation**: [Swagger](https://github.com/nestjs/swagger) |
| 23 | +- **Distributed Tracing**: [OpenTelemetry](https://github.com/pragmaticivan/nestjs-otel) |
| 24 | +- **Metrics**: [Prometheus](https://prometheus.io/) |
| 25 | +- **Log Aggregation**: [Grafana Loki](https://grafana.com/docs/loki/latest/) |
| 26 | +- **Alerting**: [Alertmanager](https://grafana.com/docs/grafana/latest/alerting/set-up/migrating-alerts/legacy-alerting/grafana-cloud-alerting/alertmanager/) |
| 27 | + |
| 28 | +### 🐳 Deployment & Operations |
| 29 | + |
| 30 | +- **Containerization**: Docker + Docker Compose |
| 31 | +- **Orchestration**: Kubernetes configurations |
| 32 | +- **Health Checks**: Built-in health check endpoints |
| 33 | + |
| 34 | +## 🚀 Quick Start |
| 35 | + |
| 36 | +### Environment Setup |
| 37 | + |
| 38 | +1. **Clone the project** |
38 | 39 |
|
39 | 40 | ```bash |
40 | | -cp .env.example .env |
| 41 | +git clone <your-repo-url> |
| 42 | +cd nestjs-prisma-api |
41 | 43 | ``` |
42 | 44 |
|
43 | | -Install dependencies: |
| 45 | +2. **Install dependencies** |
44 | 46 |
|
45 | 47 | ```bash |
46 | 48 | pnpm install |
47 | 49 | ``` |
48 | 50 |
|
49 | | -Generate Prisma client: |
| 51 | +3. **Environment configuration** |
50 | 52 |
|
51 | 53 | ```bash |
52 | | -pnpm run prisma:migrate |
| 54 | +cp .env.example .env |
| 55 | +# Edit .env file to configure database connection and other settings |
53 | 56 | ``` |
54 | 57 |
|
55 | | -Migrate database: |
| 58 | +4. **Database initialization** |
56 | 59 |
|
57 | 60 | ```bash |
| 61 | +# Generate Prisma client |
| 62 | +pnpm run prisma:generate |
| 63 | + |
| 64 | +# Run database migrations |
58 | 65 | pnpm run prisma:migrate:dev |
59 | 66 | ``` |
60 | 67 |
|
61 | | -Run the app: |
| 68 | +5. **Start development server** |
62 | 69 |
|
63 | 70 | ```bash |
64 | 71 | pnpm run start:dev |
65 | 72 | ``` |
66 | 73 |
|
67 | | -### Deployment with docker |
| 74 | +The application will start at `http://localhost:3000`, with Swagger documentation available at `http://localhost:3000/api`. |
| 75 | + |
| 76 | +## ⚙️ Configuration Guide |
| 77 | + |
| 78 | +### Grafana Cloud Configuration |
| 79 | + |
| 80 | +#### 1. Configure Loki Log Collection |
| 81 | + |
| 82 | +Visit [Grafana Cloud Logs]`https://grafana.com/orgs/{your-org-id}/hosted-logs/{your-dashboard-id}#sending-logs` to get configuration information. |
| 83 | + |
| 84 | +Configure in your `.env` file: |
| 85 | + |
| 86 | +```bash |
| 87 | +LOG_LOKI_HOST="your-loki-url" |
| 88 | +LOG_LOKI_USERNAME="your-username" |
| 89 | +LOG_LOKI_PASSWORD="Basic <Your Grafana.com API Token>" |
| 90 | +``` |
| 91 | + |
| 92 | +#### 2. Configure OpenTelemetry Tracing |
| 93 | + |
| 94 | +Visit [Grafana Cloud OTLP]`https://grafana.com/orgs/{your-org-id}/stacks/{your-stack-id}/otlp-info` to get configuration information. |
| 95 | + |
| 96 | +Configure in your `.env` file: |
| 97 | + |
| 98 | +```bash |
| 99 | +OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf" |
| 100 | +OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp-gateway-prod-ap-southeast-1.grafana.net/otlp" |
| 101 | +OTEL_EXPORTER_OTLP_HEADERS="Authorization=Basic {your-api-key}" |
| 102 | +``` |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +### Grafana Docker Environment |
| 107 | + |
| 108 | +Use [dockotlp](https://github.com/iamnivekx/dockotlp) to quickly set up a local monitoring environment: |
| 109 | + |
| 110 | +```bash |
| 111 | +git clone https://github.com/iamnivekx/dockotlp.git |
| 112 | +cd dockotlp |
| 113 | +docker compose up -d |
| 114 | +``` |
| 115 | + |
| 116 | +## 🐳 Deployment |
| 117 | + |
| 118 | +### Docker Deployment |
68 | 119 |
|
69 | | -Docker: |
| 120 | +1. **Build image** |
70 | 121 |
|
71 | 122 | ```bash |
72 | 123 | docker-compose build |
73 | 124 | ``` |
74 | 125 |
|
75 | | -### start docker-compose |
| 126 | +2. **Start services** |
76 | 127 |
|
77 | 128 | ```bash |
78 | 129 | docker-compose up -d |
79 | 130 | ``` |
80 | 131 |
|
81 | | -In Node.js Environment: |
| 132 | +### Production Deployment |
| 133 | + |
| 134 | +1. **Install dependencies and build** |
82 | 135 |
|
83 | 136 | ```bash |
84 | 137 | pnpm install |
85 | 138 | pnpm run build |
86 | | -pnpm run start:prod |
87 | 139 | ``` |
88 | 140 |
|
89 | | -### Migrate database |
90 | | - |
91 | | -Development: |
| 141 | +2. **Start production service** |
92 | 142 |
|
93 | 143 | ```bash |
94 | | -pnpm run prisma:migrate:dev |
| 144 | +pnpm run start:prod |
95 | 145 | ``` |
96 | 146 |
|
97 | | -Production: |
| 147 | +### Database Migrations |
98 | 148 |
|
99 | | -```bash |
100 | | -pnpm run prisma:migrate:deploy |
101 | | -``` |
| 149 | +- **Development**: `pnpm run prisma:migrate:dev` |
| 150 | +- **Production**: `pnpm run prisma:migrate:deploy` |
| 151 | + |
| 152 | +## 📸 Feature Preview |
| 153 | + |
| 154 | +### API Documentation (Swagger) |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +### OpenTelemetry Distributed Tracing |
| 159 | + |
| 160 | + |
102 | 161 |
|
103 | | -## Overview |
| 162 | +### Prometheus Metrics Monitoring |
104 | 163 |
|
105 | | -swagger |
| 164 | + |
106 | 165 |
|
107 | | - |
| 166 | +### Alert Management |
108 | 167 |
|
109 | | -`opentelemetry` |
| 168 | + |
110 | 169 |
|
111 | | - |
| 170 | +### Kubernetes Deployment |
112 | 171 |
|
113 | | -`prometheus` |
| 172 | + |
| 173 | + |
| 174 | +### Ingress Configuration |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +## 📁 Project Structure |
| 179 | + |
| 180 | +``` |
| 181 | +src/ |
| 182 | +├── api/ # API modules |
| 183 | +│ ├── auth/ # Authentication module |
| 184 | +│ ├── health/ # Health checks |
| 185 | +│ └── users/ # User management |
| 186 | +├── common/ # Common modules |
| 187 | +│ ├── constants/ # Constant definitions |
| 188 | +│ ├── decorators/ # Custom decorators |
| 189 | +│ └── guards/ # Guards |
| 190 | +├── config/ # Configuration module |
| 191 | +├── entities/ # Data entities |
| 192 | +├── prisma/ # Prisma configuration |
| 193 | +└── services/ # Business services |
| 194 | +``` |
114 | 195 |
|
115 | | - |
| 196 | +## 🤝 Contributing |
116 | 197 |
|
117 | | -`alerting` |
118 | | - |
| 198 | +Issues and Pull Requests are welcome! |
119 | 199 |
|
120 | | -`k8s` |
121 | | - |
| 200 | +## 📄 License |
122 | 201 |
|
123 | | -`ingress` |
124 | | - |
| 202 | +This project is licensed under the MIT License. |
125 | 203 |
|
126 | | -## Acknowledgements |
| 204 | +## 🙏 Acknowledgments |
127 | 205 |
|
128 | | -- [nestjs-prisma](https://github.com/notiz-dev/nestjs-prisma) |
129 | | -- [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel) |
130 | | -- [nestjs-otel-prom-grafana-tempo](https://github.com/pragmaticivan/nestjs-otel-prom-grafana-tempo) |
| 206 | +- [grafana](https://grafana.com) - Grafana |
| 207 | +- [dockotlp](https://github.com/iamnivekx/dockotlp) - Docker OpenTelemetry Prometheus Loki Grafana environment |
| 208 | +- [nestjs-prisma](https://github.com/notiz-dev/nestjs-prisma) - NestJS Prisma integration |
| 209 | +- [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel) - OpenTelemetry integration |
| 210 | +- [nestjs-otel-prom-grafana-tempo](https://github.com/pragmaticivan/nestjs-otel-prom-grafana-tempo) - Monitoring stack integration examples |
0 commit comments