nodejs-quickstart-structure 1.18.1 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +2 -1
  3. package/lib/modules/caching-setup.js +76 -73
  4. package/lib/modules/kafka-setup.js +249 -191
  5. package/lib/modules/project-setup.js +1 -0
  6. package/package.json +13 -2
  7. package/templates/clean-architecture/js/src/errors/BadRequestError.js +11 -10
  8. package/templates/clean-architecture/js/src/errors/BadRequestError.spec.js.ejs +22 -21
  9. package/templates/clean-architecture/js/src/errors/NotFoundError.js +11 -10
  10. package/templates/clean-architecture/js/src/errors/NotFoundError.spec.js.ejs +22 -21
  11. package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.js.ejs +69 -39
  12. package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.spec.js.ejs +142 -81
  13. package/templates/clean-architecture/js/src/interfaces/controllers/userController.js.ejs +156 -75
  14. package/templates/clean-architecture/js/src/interfaces/controllers/userController.spec.js.ejs +234 -138
  15. package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.js.ejs +27 -21
  16. package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.spec.js.ejs +66 -49
  17. package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/user.types.js.ejs +19 -17
  18. package/templates/clean-architecture/js/src/interfaces/routes/api.js +12 -10
  19. package/templates/clean-architecture/js/src/usecases/DeleteUser.js +11 -0
  20. package/templates/clean-architecture/js/src/usecases/DeleteUser.spec.js.ejs +47 -0
  21. package/templates/clean-architecture/js/src/usecases/UpdateUser.js +11 -0
  22. package/templates/clean-architecture/js/src/usecases/UpdateUser.spec.js.ejs +48 -0
  23. package/templates/clean-architecture/js/src/utils/errorMessages.js +14 -0
  24. package/templates/clean-architecture/ts/src/errors/BadRequestError.spec.ts.ejs +22 -21
  25. package/templates/clean-architecture/ts/src/errors/BadRequestError.ts +9 -8
  26. package/templates/clean-architecture/ts/src/errors/NotFoundError.spec.ts.ejs +22 -21
  27. package/templates/clean-architecture/ts/src/errors/NotFoundError.ts +9 -8
  28. package/templates/clean-architecture/ts/src/infrastructure/repositories/UserRepository.spec.ts.ejs +175 -85
  29. package/templates/clean-architecture/ts/src/infrastructure/repositories/userRepository.ts.ejs +74 -0
  30. package/templates/clean-architecture/ts/src/interfaces/controllers/userController.spec.ts.ejs +331 -185
  31. package/templates/clean-architecture/ts/src/interfaces/controllers/userController.ts.ejs +173 -84
  32. package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -51
  33. package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.ts.ejs +29 -21
  34. package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/user.types.ts.ejs +17 -15
  35. package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.ts +13 -11
  36. package/templates/clean-architecture/ts/src/usecases/deleteUser.spec.ts.ejs +47 -0
  37. package/templates/clean-architecture/ts/src/usecases/deleteUser.ts +9 -0
  38. package/templates/clean-architecture/ts/src/usecases/updateUser.spec.ts.ejs +48 -0
  39. package/templates/clean-architecture/ts/src/usecases/updateUser.ts +9 -0
  40. package/templates/clean-architecture/ts/src/utils/errorMessages.ts +12 -0
  41. package/templates/common/.gitattributes +46 -0
  42. package/templates/common/README.md.ejs +294 -270
  43. package/templates/common/caching/clean/js/DeleteUser.js.ejs +27 -0
  44. package/templates/common/caching/clean/js/UpdateUser.js.ejs +27 -0
  45. package/templates/common/caching/clean/ts/deleteUser.ts.ejs +24 -0
  46. package/templates/common/caching/clean/ts/updateUser.ts.ejs +25 -0
  47. package/templates/common/caching/ts/memoryCache.ts.ejs +73 -64
  48. package/templates/common/caching/ts/redisClient.ts.ejs +89 -80
  49. package/templates/common/database/js/models/User.js.ejs +79 -53
  50. package/templates/common/database/js/models/User.js.mongoose.ejs +23 -19
  51. package/templates/common/database/js/models/User.spec.js.ejs +94 -84
  52. package/templates/common/database/ts/models/User.spec.ts.ejs +100 -84
  53. package/templates/common/database/ts/models/User.ts.ejs +87 -61
  54. package/templates/common/database/ts/models/User.ts.mongoose.ejs +30 -25
  55. package/templates/common/health/js/healthRoute.js.ejs +50 -47
  56. package/templates/common/health/ts/healthRoute.ts.ejs +49 -46
  57. package/templates/common/jest.e2e.config.js.ejs +8 -8
  58. package/templates/common/kafka/js/messaging/baseConsumer.js.ejs +30 -30
  59. package/templates/common/kafka/js/messaging/userEventSchema.js.ejs +12 -11
  60. package/templates/common/kafka/js/messaging/welcomeEmailConsumer.js.ejs +44 -31
  61. package/templates/common/kafka/js/messaging/welcomeEmailConsumer.spec.js.ejs +86 -49
  62. package/templates/common/kafka/js/services/kafkaService.js.ejs +93 -93
  63. package/templates/common/kafka/js/utils/kafkaEvents.js.ejs +7 -0
  64. package/templates/common/kafka/ts/messaging/userEventSchema.spec.ts.ejs +51 -51
  65. package/templates/common/kafka/ts/messaging/userEventSchema.ts.ejs +12 -11
  66. package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.spec.ts.ejs +86 -49
  67. package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.ts.ejs +38 -25
  68. package/templates/common/kafka/ts/services/kafkaService.ts.ejs +95 -95
  69. package/templates/common/kafka/ts/utils/kafkaEvents.ts.ejs +5 -0
  70. package/templates/common/shutdown/js/gracefulShutdown.js.ejs +65 -61
  71. package/templates/common/shutdown/js/gracefulShutdown.spec.js.ejs +149 -160
  72. package/templates/common/shutdown/ts/gracefulShutdown.spec.ts.ejs +179 -158
  73. package/templates/common/shutdown/ts/gracefulShutdown.ts.ejs +59 -55
  74. package/templates/common/src/tests/e2e/e2e.users.test.js.ejs +120 -49
  75. package/templates/common/src/tests/e2e/e2e.users.test.ts.ejs +120 -49
  76. package/templates/common/swagger.yml.ejs +118 -66
  77. package/templates/db/mysql/V1__Initial_Setup.sql.ejs +10 -9
  78. package/templates/db/postgres/V1__Initial_Setup.sql.ejs +10 -9
  79. package/templates/mvc/js/src/controllers/userController.js.ejs +246 -105
  80. package/templates/mvc/js/src/controllers/userController.spec.js.ejs +481 -209
  81. package/templates/mvc/js/src/errors/BadRequestError.js +11 -10
  82. package/templates/mvc/js/src/errors/BadRequestError.spec.js.ejs +22 -21
  83. package/templates/mvc/js/src/errors/NotFoundError.js +11 -10
  84. package/templates/mvc/js/src/errors/NotFoundError.spec.js.ejs +22 -21
  85. package/templates/mvc/js/src/graphql/resolvers/user.resolvers.js.ejs +25 -19
  86. package/templates/mvc/js/src/graphql/resolvers/user.resolvers.spec.js.ejs +64 -47
  87. package/templates/mvc/js/src/graphql/typeDefs/user.types.js.ejs +19 -17
  88. package/templates/mvc/js/src/routes/api.js +10 -8
  89. package/templates/mvc/js/src/routes/api.spec.js.ejs +41 -36
  90. package/templates/mvc/js/src/utils/errorMessages.js +14 -0
  91. package/templates/mvc/ts/src/controllers/userController.spec.ts.ejs +481 -203
  92. package/templates/mvc/ts/src/controllers/userController.ts.ejs +248 -107
  93. package/templates/mvc/ts/src/errors/BadRequestError.spec.ts.ejs +22 -21
  94. package/templates/mvc/ts/src/errors/BadRequestError.ts +9 -8
  95. package/templates/mvc/ts/src/errors/NotFoundError.spec.ts.ejs +27 -21
  96. package/templates/mvc/ts/src/errors/NotFoundError.ts +9 -8
  97. package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -51
  98. package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.ts.ejs +29 -21
  99. package/templates/mvc/ts/src/graphql/typeDefs/user.types.ts.ejs +17 -15
  100. package/templates/mvc/ts/src/index.ts.ejs +156 -153
  101. package/templates/mvc/ts/src/routes/api.spec.ts.ejs +59 -40
  102. package/templates/mvc/ts/src/routes/api.ts +12 -10
  103. package/templates/mvc/ts/src/utils/errorMessages.ts +12 -0
  104. package/templates/clean-architecture/ts/src/infrastructure/repositories/UserRepository.ts.ejs +0 -37
@@ -0,0 +1,46 @@
1
+ # Enforce LF endings for all text files
2
+ * text=auto eol=lf
3
+
4
+ # Explicitly handle common text file types
5
+ *.js text eol=lf
6
+ *.ts text eol=lf
7
+ *.tsx text eol=lf
8
+ *.jsx text eol=lf
9
+ *.mjs text eol=lf
10
+ *.cjs text eol=lf
11
+ *.json text eol=lf
12
+ *.md text eol=lf
13
+ *.sh text eol=lf
14
+ *.yml text eol=lf
15
+ *.yaml text eol=lf
16
+ *.html text eol=lf
17
+ *.css text eol=lf
18
+ *.scss text eol=lf
19
+ *.less text eol=lf
20
+ *.graphql text eol=lf
21
+ *.sql text eol=lf
22
+ *.pug text eol=lf
23
+
24
+ # Configuration files
25
+ .env* text eol=lf
26
+ .editorconfig text eol=lf
27
+ .prettierrc text eol=lf
28
+ .eslintrc* text eol=lf
29
+ .gitignore text eol=lf
30
+ .dockerignore text eol=lf
31
+ .gitattributes text eol=lf
32
+
33
+ # Assets
34
+ *.png binary
35
+ *.jpg binary
36
+ *.jpeg binary
37
+ *.gif binary
38
+ *.ico binary
39
+ *.webp binary
40
+ *.avif binary
41
+ *.pdf binary
42
+ *.woff binary
43
+ *.woff2 binary
44
+ *.ttf binary
45
+ *.eot binary
46
+ *.svg text eol=lf
@@ -1,271 +1,295 @@
1
- # <%= projectName %>
2
-
3
- ![Node.js](https://img.shields.io/badge/Node.js-18%2B-green.svg)
4
- ![License](https://img.shields.io/badge/License-ISC-blue.svg)
5
- <% if (language === 'TypeScript') { %>![TypeScript](https://img.shields.io/badge/Language-TypeScript-blue.svg)<% } else { %>![JavaScript](https://img.shields.io/badge/Language-JavaScript-yellow.svg)<% } %>
6
- <% if (includeSecurity) { %>
7
- [![Snyk Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/github/yourusername/<%= projectName %>?style=flat-square)](https://snyk.io/)
8
- [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=<%= projectName %>&metric=alert_status)](https://sonarcloud.io/)
9
- <% } %>
10
-
11
- A production-ready Node.js microservice generated with **<%= architecture %>** and **<%= database %>**.
12
- This project follows a strict **7-Step Production-Ready Process** to ensure quality and scalability from day one.
13
-
14
- ---
15
-
16
- ## 🚀 7-Step Production-Ready Process
17
-
18
- 1. **Initialize Git**: `git init` (Required for Husky hooks and security gates).
19
- 2. **Install Dependencies**: `npm install`.
20
- 3. **Configure Environment**: Copy `.env.example` to `.env`.
21
- 4. **Start Infrastructure**: `docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>`.
22
- 5. **Run Development**: `npm run dev`.
23
- 6. **Verify Standards**: `npm run lint` and `npm test` (Enforce 80% coverage).
24
- 7. **Build & Deploy**: `npm run build` followed by `npm run deploy` (via PM2).
25
-
26
- ---
27
-
28
- ## 🚀 Key Features
29
-
30
- - **Architecture**: <%= architecture %> (<% if (architecture === 'Clean Architecture') { %>Domain, UseCases, Infrastructure<% } else { %>MVC Pattern<% } %>).
31
- - **Database**: <%= database %> <% if (database !== 'None') { %>(via <%= database === 'MongoDB' ? 'Mongoose' : 'Sequelize' %>)<% } %>.
32
- - **Security**: Helmet, CORS, Rate Limiting, HPP, Snyk SCA.
33
- - **Quality**: 80%+ Test Coverage, Eslint, Prettier, Husky.
34
- - **DevOps**: Multi-stage Docker, CI/CD ready (GitHub/GitLab/Jenkins).
35
- <% if (includeSecurity) { %>- **Enterprise Hardening**: SonarCloud SAST, Security Policies.<% } %>
36
-
37
- ## 📂 Project Structure
38
-
39
- The project follows **<%= architecture %>** principles.
40
- <% if (architecture === 'Clean Architecture') { -%>
41
- - **Domain**: Pure business logic (Entities/Interfaces).
42
- - **Use Case**: Application-specific business rules.
43
- - **Infrastructure**: External concerns (DB, Messaging, Caching).
44
- <% } else { -%>
45
- - **Model**: Database schemas and data logic.
46
- - **View**: Template engines or API responders.
47
- - **Controller**: Orchestrates flow between Model and View.
48
- <% } -%>
49
-
50
- ---
51
-
52
- ## 🛠️ Detailed Getting Started
53
-
54
- Follow the **🚀 7-Step Production-Ready Process** summary at the top, or follow these detailed instructions:
55
-
56
- ### 1. Prerequisites
57
- - Node.js (v18+)
58
- - Docker & Docker Compose
59
-
60
- ### 2. Environment Setup
61
- Copy the example environment file and adjust the values as needed:
62
- ```bash
63
- cp .env.example .env
64
- ```
65
-
66
- ### 3. Infrastructure & App Launch
67
- ```bash
68
- # Initialize Git for security hooks
69
- git init
70
-
71
- # Install dependencies
72
- npm install
73
-
74
- # Start required services
75
- docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>
76
-
77
- # Run the app in development mode
78
- npm run dev
79
- ```
80
-
81
- ### 4. Quality & Standards
82
- ```bash
83
- # Lint & Format
84
- npm run lint
85
- npm run format
86
-
87
- # Run Unit/Integration Tests
88
- npm test
89
- npm run test:coverage
90
- ```
91
-
92
- <% if (communication === 'Kafka') { -%>
93
- Microservices communication handled via **Kafka**.
94
- <% } else if (communication === 'GraphQL') { -%>
95
- API is exposed via **GraphQL**.
96
- The Apollo Sandbox UI for API exploration and documentation is available natively, fully embedded for offline development:
97
- - **URL**: `http://localhost:3000/graphql` (Dynamic based on PORT)
98
- If you are opening `http://localhost:3000/graphql` in your browser, you can directly run the following in the Apollo Sandbox UI:
99
-
100
- **Query to get all users:**
101
- ```graphql
102
- query GetAllUsers {
103
- getAllUsers {
104
- id
105
- name
106
- email
107
- }
108
- }
109
- ```
110
-
111
- **Mutation to create a user:**
112
- ```graphql
113
- mutation CreateUser {
114
- createUser(name: "John Doe", email: "john@example.com") {
115
- id
116
- name
117
- email
118
- }
119
- }
120
- ```
121
- <% } else { -%>
122
- API is exposed via **REST**.
123
- A Swagger UI for API documentation is available at:
124
- - **URL**: `http://localhost:3000/api-docs` (Dynamic based on PORT)
125
- <%_ } -%>
126
-
127
- <% if (communication === 'Kafka') { -%>
128
- ## 📡 Testing Kafka Asynchronous Flow
129
- This project demonstrates a production-ready Kafka flow:
130
- 1. **Producer**: When a user is created via the API, a `USER_CREATED` event is sent to `user-topic`.
131
- 2. **Consumer**: `WelcomeEmailConsumer` listens to `user-topic` and simulates sending an email.
132
-
133
- ### How to verify:
134
- 1. Ensure infrastructure is running: `docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>`
135
- 2. Start the app: `npm run dev`
136
- 3. Trigger an event by creating a user (via Postman or curl):
137
- ```bash
138
- curl -X POST http://localhost:3000/api/users \
139
- -H "Content-Type: application/json" \
140
- -d '{"name": "Kafka Tester", "email": "kafka@example.com"}'
141
- ```
142
- 4. Observe the logs:
143
- ```text
144
- [Kafka] Producer: Sent USER_CREATED event for 'kafka@example.com'
145
- [Kafka] Consumer: Received USER_CREATED.
146
- [Kafka] Consumer: 📧 Sending welcome email to 'kafka@example.com'... Done!
147
- ```
148
-
149
- ### 🛠️ Kafka Troubleshooting
150
- If the connection or events are failing:
151
- 1. **Check Docker**: Ensure Kafka container is running (`docker ps`).
152
- 2. **Verify Broker**: `KAFKA_BROKER` in `.env` must match your host/port (standard: 9093).
153
- 3. **Advertised Listeners**: If using Windows/WSL, check `docker-compose.yml` advertisers are correct.
154
- 4. **Logs**: Check `docker compose logs -f kafka` for start-up errors.
155
- <% } -%>
156
-
157
- <% if (caching === 'Redis') { -%>
158
- ## Caching
159
- This project uses **Redis** for caching.
160
- - **Client**: `ioredis`
161
- - **Connection**: Configured via `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD` in `.env`.
162
- <% } else if (caching === 'Memory Cache') { -%>
163
- ## Caching
164
- This project uses **Memory Cache** for in-memory caching.
165
- - **Client**: `node-cache`
166
- <% } -%>
167
-
168
- ## 📝 Logging
169
- This project uses **Winston** for structured logging.
170
- - **Development**: Logs are printed to the console.
171
- - **Production**: Logs are saved to files:
172
- - `error.log`: Only error level logs.
173
- - `combined.log`: All logs.
174
-
175
- ## 🐳 Docker Deployment
176
- This project uses a **Multi-Stage Dockerfile** for optimized production images.
177
-
178
- <% if (database !== 'None' || caching === 'Redis' || communication === 'Kafka') { -%>
179
- ### 1. Running Locally (Development)
180
- To run the Node.js application locally while using Docker for the infrastructure (Database, Redis, Kafka, etc.):
181
-
182
- ```bash
183
- # Start infrastructure
184
- docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>
185
-
186
- # Start the application
187
- npm run dev
188
- ```
189
-
190
- ### 2. Running the App Container with Compose Infrastructure
191
- If you want to run the application itself inside a Docker container while connecting to the infrastructure managed by your `docker-compose.yml`:
192
-
193
- ```bash
194
- # First, ensure your infrastructure is running
195
- docker-compose up -d
196
-
197
- # Build Production Image
198
- docker build -t <%= projectName %> .
199
-
200
- # Run Container (attached to the compose network)
201
- docker run -p 3000:3000 --network <%= projectName %>_default \
202
- <% if (database !== 'None') { -%>
203
- -e DB_HOST=db \
204
- <% } -%>
205
- <% if (caching === 'Redis') { -%>
206
- -e REDIS_HOST=redis \
207
- <% } -%>
208
- <% if (communication === 'Kafka') { -%>
209
- -e KAFKA_BROKER=kafka:29092 \
210
- <% } -%>
211
- <%= projectName %>
212
- ```
213
- <% } else { -%>
214
- ```bash
215
- # Build Production Image
216
- docker build -t <%= projectName %> .
217
-
218
- # Run Container
219
- docker run -p 3000:3000 <%= projectName %>
220
- ```
221
- <% } -%>
222
- ## 🚀 PM2 Deployment (VPS/EC2)
223
- This project is pre-configured for direct deployment to a VPS/EC2 instance using **PM2** (via `ecosystem.config.js`).
224
- 1. Install dependencies
225
- ```bash
226
- npm install
227
- ```
228
- 2. **Start Infrastructure (DB, Redis, Kafka, etc.) in the background**
229
- *(This specifically starts the background services without running the application inside Docker, allowing PM2 to handle it).*
230
- ```bash
231
- docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>
232
- ```
233
- 3. **Wait 5-10s** for the database to fully initialize.
234
- 4. **Deploy the App using PM2 in Cluster Mode**
235
- ```bash
236
- <% if (language === 'TypeScript') { %>npm run build
237
- <% } %>npm run deploy
238
- ```
239
- 5. **Check logs**
240
- ```bash
241
- npx pm2 logs
242
- ```
243
- 6. Stop and remove the PM2 application
244
- ```bash
245
- npx pm2 delete <%= projectName %>
246
- ```
247
- 7. Stop and remove the Docker infrastructure
248
- ```bash
249
- docker-compose down
250
- ```
251
-
252
- ## 🔒 Security Features
253
- - **Helmet**: Sets secure HTTP headers.
254
- - **CORS**: Configured for cross-origin requests.
255
- - **Rate Limiting**: Protects against DDoS / Brute-force.
256
- - **HPP**: Prevents HTTP Parameter Pollution attacks.
257
- <% if (includeSecurity) { %>
258
- ### 🛡️ Enterprise Hardening (Big Tech Standard)
259
- - **Snyk SCA**: Run `npm run snyk:test` for dependency scanning.
260
- - **SonarCloud**: Automated SAST on every Push/PR.
261
- - **Digital Guardians**: Recommended Gitleaks integration for secret protection.
262
- - **Security Policy**: Standard `SECURITY.md` for vulnerability reporting.
263
- <% } %>
264
- ## 🤖 AI-Native Development
265
-
266
- This project is "AI-Ready" out of the box. We have pre-configured industry-leading AI context files to bridge the gap between "Generated Code" and "AI-Assisted Development."
267
-
268
- - **Magic Defaults**: We've automatically tailored your AI context to focus on **<%= projectName %>** and its specific architectural stack (<%= architecture %>, <%= database %>, etc.).
269
- - **Use Cursor?** We've configured **`.cursorrules`** at the root. It enforces project standards (80% coverage, MVC/Clean) directly within the editor.
270
- - *Pro-tip*: You can customize the `Project Goal` placeholder in `.cursorrules` to help the AI understand your specific business logic!
1
+ # <%= projectName %>
2
+
3
+ ![Node.js](https://img.shields.io/badge/Node.js-18%2B-green.svg)
4
+ ![License](https://img.shields.io/badge/License-ISC-blue.svg)
5
+ <% if (language === 'TypeScript') { %>![TypeScript](https://img.shields.io/badge/Language-TypeScript-blue.svg)<% } else { %>![JavaScript](https://img.shields.io/badge/Language-JavaScript-yellow.svg)<% } %>
6
+ <% if (includeSecurity) { %>
7
+ [![Snyk Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/github/yourusername/<%= projectName %>?style=flat-square)](https://snyk.io/)
8
+ [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=<%= projectName %>&metric=alert_status)](https://sonarcloud.io/)
9
+ <% } %>
10
+
11
+ A production-ready Node.js microservice generated with **<%= architecture %>** and **<%= database %>**.
12
+ This project follows a strict **7-Step Production-Ready Process** to ensure quality and scalability from day one.
13
+
14
+ ---
15
+
16
+ ## 🚀 7-Step Production-Ready Process
17
+
18
+ 1. **Initialize Git**: `git init` (Required for Husky hooks and security gates).
19
+ 2. **Install Dependencies**: `npm install`.
20
+ 3. **Configure Environment**: Copy `.env.example` to `.env`.
21
+ 4. **Start Infrastructure**: `docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>`.
22
+ 5. **Run Development**: `npm run dev`.
23
+ 6. **Verify Standards**: `npm run lint` and `npm test` (Enforce 80% coverage).
24
+ 7. **Build & Deploy**: `npm run build` followed by `npm run deploy` (via PM2).
25
+
26
+ ---
27
+
28
+ ## 🚀 Key Features
29
+
30
+ - **Architecture**: <%= architecture %> (<% if (architecture === 'Clean Architecture') { %>Domain, UseCases, Infrastructure<% } else { %>MVC Pattern<% } %>).
31
+ - **Database**: <%= database %> <% if (database !== 'None') { %>(via <%= database === 'MongoDB' ? 'Mongoose' : 'Sequelize' %>)<% } %>.
32
+ - **Security**: Helmet, CORS, Rate Limiting, HPP, Snyk SCA.
33
+ - **Quality**: 80%+ Test Coverage, Eslint, Prettier, Husky.
34
+ - **DevOps**: Multi-stage Docker, CI/CD ready (GitHub/GitLab/Jenkins).
35
+ <% if (includeSecurity) { %>- **Enterprise Hardening**: SonarCloud SAST, Security Policies.<% } %>
36
+
37
+ ## 📂 Project Structure
38
+
39
+ The project follows **<%= architecture %>** principles.
40
+ <% if (architecture === 'Clean Architecture') { -%>
41
+ - **Domain**: Pure business logic (Entities/Interfaces).
42
+ - **Use Case**: Application-specific business rules.
43
+ - **Infrastructure**: External concerns (DB, Messaging, Caching).
44
+ <% } else { -%>
45
+ - **Model**: Database schemas and data logic.
46
+ - **View**: Template engines or API responders.
47
+ - **Controller**: Orchestrates flow between Model and View.
48
+ <% } -%>
49
+
50
+ ---
51
+
52
+ ## 🛠️ Detailed Getting Started
53
+
54
+ Follow the **🚀 7-Step Production-Ready Process** summary at the top, or follow these detailed instructions:
55
+
56
+ ### 1. Prerequisites
57
+ - Node.js (v18+)
58
+ - Docker & Docker Compose
59
+
60
+ ### 2. Environment Setup
61
+ Copy the example environment file and adjust the values as needed:
62
+ ```bash
63
+ cp .env.example .env
64
+ ```
65
+
66
+ ### 3. Infrastructure & App Launch
67
+ ```bash
68
+ # Initialize Git for security hooks
69
+ git init
70
+
71
+ # Install dependencies
72
+ npm install
73
+
74
+ # Start required services
75
+ docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>
76
+
77
+ # Run the app in development mode
78
+ npm run dev
79
+ ```
80
+
81
+ ### 4. Quality & Standards
82
+ ```bash
83
+ # Lint & Format
84
+ npm run lint
85
+ npm run format
86
+
87
+ # Run Unit/Integration Tests
88
+ npm test
89
+ npm run test:coverage
90
+ ```
91
+
92
+ <% if (communication === 'Kafka') { -%>
93
+ Microservices communication handled via **Kafka**.
94
+ <% } else if (communication === 'GraphQL') { -%>
95
+ API is exposed via **GraphQL**.
96
+ The Apollo Sandbox UI for API exploration and documentation is available natively, fully embedded for offline development:
97
+ - **URL**: `http://localhost:3000/graphql` (Dynamic based on PORT)
98
+ If you are opening `http://localhost:3000/graphql` in your browser, you can directly run the following in the Apollo Sandbox UI:
99
+
100
+ **Query to get all users:**
101
+ ```graphql
102
+ query GetAllUsers {
103
+ getAllUsers {
104
+ id
105
+ name
106
+ email
107
+ }
108
+ }
109
+ ```
110
+
111
+ **Mutation to create a user:**
112
+ ```graphql
113
+ mutation CreateUser {
114
+ createUser(name: "John Doe", email: "john@example.com") {
115
+ id
116
+ name
117
+ email
118
+ }
119
+ }
120
+ ```
121
+
122
+ **Mutation to update a user:**
123
+ ```graphql
124
+ mutation UpdateUser {
125
+ updateUser(id: "1", name: "John Updated") {
126
+ id
127
+ name
128
+ email
129
+ }
130
+ }
131
+ ```
132
+
133
+ **Mutation to delete a user:**
134
+ ```graphql
135
+ mutation DeleteUser {
136
+ deleteUser(id: "1")
137
+ }
138
+ ```
139
+ <% } else { -%>
140
+ API is exposed via **REST**.
141
+ A Swagger UI for API documentation is available at:
142
+ - **URL**: `http://localhost:3000/api-docs` (Dynamic based on PORT)
143
+
144
+ ### 🛣️ User Endpoints:
145
+ - `GET /api/users`: List all users.
146
+ - `POST /api/users`: Create a new user.
147
+ - `PATCH /api/users/:id`: Partially update a user.
148
+ - `DELETE /api/users/:id`: Delete a user (Soft Delete).
149
+ <%_ } -%>
150
+
151
+ <% if (communication === 'Kafka') { -%>
152
+ ## 📡 Testing Kafka Asynchronous Flow
153
+ This project demonstrates a production-ready Kafka flow:
154
+ 1. **Producer**: When a user is created via the API, a `USER_CREATED` event is sent to `user-topic`.
155
+ 2. **Consumer**: `WelcomeEmailConsumer` listens to `user-topic` and simulates sending an email.
156
+
157
+ ### How to verify:
158
+ 1. Ensure infrastructure is running: `docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>`
159
+ 2. Start the app: `npm run dev`
160
+ 3. Trigger an event by creating a user (via Postman or curl):
161
+ ```bash
162
+ curl -X POST http://localhost:3000/api/users \
163
+ -H "Content-Type: application/json" \
164
+ -d '{"name": "Kafka Tester", "email": "kafka@example.com"}'
165
+ ```
166
+ 4. Observe the logs:
167
+ ```text
168
+ [Kafka] Producer: Sent USER_CREATED event for 'kafka@example.com'
169
+ [Kafka] Consumer: Received USER_CREATED.
170
+ [Kafka] Consumer: 📧 Sending welcome email to 'kafka@example.com'... Done!
171
+ ```
172
+
173
+ ### 🛠️ Kafka Troubleshooting
174
+ If the connection or events are failing:
175
+ 1. **Check Docker**: Ensure Kafka container is running (`docker ps`).
176
+ 2. **Verify Broker**: `KAFKA_BROKER` in `.env` must match your host/port (standard: 9093).
177
+ 3. **Advertised Listeners**: If using Windows/WSL, check `docker-compose.yml` advertisers are correct.
178
+ 4. **Logs**: Check `docker compose logs -f kafka` for start-up errors.
179
+ <% } -%>
180
+
181
+ <% if (caching === 'Redis') { -%>
182
+ ## ⚡ Caching
183
+ This project uses **Redis** for caching.
184
+ - **Client**: `ioredis`
185
+ - **Connection**: Configured via `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD` in `.env`.
186
+ <% } else if (caching === 'Memory Cache') { -%>
187
+ ## Caching
188
+ This project uses **Memory Cache** for in-memory caching.
189
+ - **Client**: `node-cache`
190
+ <% } -%>
191
+
192
+ ## 📝 Logging
193
+ This project uses **Winston** for structured logging.
194
+ - **Development**: Logs are printed to the console.
195
+ - **Production**: Logs are saved to files:
196
+ - `error.log`: Only error level logs.
197
+ - `combined.log`: All logs.
198
+
199
+ ## 🐳 Docker Deployment
200
+ This project uses a **Multi-Stage Dockerfile** for optimized production images.
201
+
202
+ <% if (database !== 'None' || caching === 'Redis' || communication === 'Kafka') { -%>
203
+ ### 1. Running Locally (Development)
204
+ To run the Node.js application locally while using Docker for the infrastructure (Database, Redis, Kafka, etc.):
205
+
206
+ ```bash
207
+ # Start infrastructure
208
+ docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>
209
+
210
+ # Start the application
211
+ npm run dev
212
+ ```
213
+
214
+ ### 2. Running the App Container with Compose Infrastructure
215
+ If you want to run the application itself inside a Docker container while connecting to the infrastructure managed by your `docker-compose.yml`:
216
+
217
+ ```bash
218
+ # First, ensure your infrastructure is running
219
+ docker-compose up -d
220
+
221
+ # Build Production Image
222
+ docker build -t <%= projectName %> .
223
+
224
+ # Run Container (attached to the compose network)
225
+ docker run -p 3000:3000 --network <%= projectName %>_default \
226
+ <% if (database !== 'None') { -%>
227
+ -e DB_HOST=db \
228
+ <% } -%>
229
+ <% if (caching === 'Redis') { -%>
230
+ -e REDIS_HOST=redis \
231
+ <% } -%>
232
+ <% if (communication === 'Kafka') { -%>
233
+ -e KAFKA_BROKER=kafka:29092 \
234
+ <% } -%>
235
+ <%= projectName %>
236
+ ```
237
+ <% } else { -%>
238
+ ```bash
239
+ # Build Production Image
240
+ docker build -t <%= projectName %> .
241
+
242
+ # Run Container
243
+ docker run -p 3000:3000 <%= projectName %>
244
+ ```
245
+ <% } -%>
246
+ ## 🚀 PM2 Deployment (VPS/EC2)
247
+ This project is pre-configured for direct deployment to a VPS/EC2 instance using **PM2** (via `ecosystem.config.js`).
248
+ 1. Install dependencies
249
+ ```bash
250
+ npm install
251
+ ```
252
+ 2. **Start Infrastructure (DB, Redis, Kafka, etc.) in the background**
253
+ *(This specifically starts the background services without running the application inside Docker, allowing PM2 to handle it).*
254
+ ```bash
255
+ docker-compose up -d<% if (database !== 'None') { %> db<% } %><% if (caching === 'Redis') { %> redis<% } %><% if (communication === 'Kafka') { %> kafka<% } %>
256
+ ```
257
+ 3. **Wait 5-10s** for the database to fully initialize.
258
+ 4. **Deploy the App using PM2 in Cluster Mode**
259
+ ```bash
260
+ <% if (language === 'TypeScript') { %>npm run build
261
+ <% } %>npm run deploy
262
+ ```
263
+ 5. **Check logs**
264
+ ```bash
265
+ npx pm2 logs
266
+ ```
267
+ 6. Stop and remove the PM2 application
268
+ ```bash
269
+ npx pm2 delete <%= projectName %>
270
+ ```
271
+ 7. Stop and remove the Docker infrastructure
272
+ ```bash
273
+ docker-compose down
274
+ ```
275
+
276
+ ## 🔒 Security Features
277
+ - **Helmet**: Sets secure HTTP headers.
278
+ - **CORS**: Configured for cross-origin requests.
279
+ - **Rate Limiting**: Protects against DDoS / Brute-force.
280
+ - **HPP**: Prevents HTTP Parameter Pollution attacks.
281
+ <% if (includeSecurity) { %>
282
+ ### 🛡️ Enterprise Hardening (Big Tech Standard)
283
+ - **Snyk SCA**: Run `npm run snyk:test` for dependency scanning.
284
+ - **SonarCloud**: Automated SAST on every Push/PR.
285
+ - **Digital Guardians**: Recommended Gitleaks integration for secret protection.
286
+ - **Security Policy**: Standard `SECURITY.md` for vulnerability reporting.
287
+ <% } %>
288
+ ## 🤖 AI-Native Development
289
+
290
+ This project is "AI-Ready" out of the box. We have pre-configured industry-leading AI context files to bridge the gap between "Generated Code" and "AI-Assisted Development."
291
+
292
+ - **Magic Defaults**: We've automatically tailored your AI context to focus on **<%= projectName %>** and its specific architectural stack (<%= architecture %>, <%= database %>, etc.).
293
+ - **Use Cursor?** We've configured **`.cursorrules`** at the root. It enforces project standards (80% coverage, MVC/Clean) directly within the editor.
294
+ - *Pro-tip*: You can customize the `Project Goal` placeholder in `.cursorrules` to help the AI understand your specific business logic!
271
295
  - **Use ChatGPT/Gemini/Claude?** Check the **`prompts/`** directory. It contains highly-specialized Agent Skill templates. You can copy-paste these into any LLM to give it a "Senior Developer" understanding of your codebase immediately.
@@ -0,0 +1,27 @@
1
+ <%_ if (caching === 'Redis') { -%>
2
+ const cacheService = require('../infrastructure/caching/redisClient');
3
+ <%_ } else if (caching === 'Memory Cache') { -%>
4
+ const cacheService = require('../infrastructure/caching/memoryCache');
5
+ <%_ } -%>
6
+ const logger = require('../infrastructure/log/logger');
7
+
8
+ class DeleteUser {
9
+ constructor(userRepository) {
10
+ this.userRepository = userRepository;
11
+ }
12
+
13
+ async execute(id) {
14
+ const result = await this.userRepository.delete(id);
15
+
16
+ try {
17
+ await cacheService.del('users:all');
18
+ logger.info('Invalidated users:all cache');
19
+ } catch (error) {
20
+ logger.error('Cache error (del):', error);
21
+ }
22
+
23
+ return result;
24
+ }
25
+ }
26
+
27
+ module.exports = DeleteUser;