nestforge-generator 0.1.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 (326) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +371 -0
  3. package/README.pt-BR.md +370 -0
  4. package/dist/features/auth-strategy.js +19 -0
  5. package/dist/features/auth-strategy.js.map +1 -0
  6. package/dist/features/database.js +59 -0
  7. package/dist/features/database.js.map +1 -0
  8. package/dist/features/dependencies.js +57 -0
  9. package/dist/features/dependencies.js.map +1 -0
  10. package/dist/features/language.js +172 -0
  11. package/dist/features/language.js.map +1 -0
  12. package/dist/features/markers.js +116 -0
  13. package/dist/features/markers.js.map +1 -0
  14. package/dist/generator.js +120 -0
  15. package/dist/generator.js.map +1 -0
  16. package/dist/index.js +59 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/prompts.js +137 -0
  19. package/dist/prompts.js.map +1 -0
  20. package/package.json +76 -0
  21. package/templates/drizzle/.env.example +45 -0
  22. package/templates/drizzle/.env.test +38 -0
  23. package/templates/drizzle/.github/workflows/ci.yml +119 -0
  24. package/templates/drizzle/ARCHITECTURE.md +227 -0
  25. package/templates/drizzle/ARCHITECTURE.pt-BR.md +225 -0
  26. package/templates/drizzle/CODE_OF_CONDUCT.md +29 -0
  27. package/templates/drizzle/CODE_OF_CONDUCT.pt-BR.md +27 -0
  28. package/templates/drizzle/CONTRIBUTING.md +57 -0
  29. package/templates/drizzle/CONTRIBUTING.pt-BR.md +54 -0
  30. package/templates/drizzle/Dockerfile +43 -0
  31. package/templates/drizzle/LICENSE +21 -0
  32. package/templates/drizzle/README.md +257 -0
  33. package/templates/drizzle/README.pt-BR.md +257 -0
  34. package/templates/drizzle/ROADMAP.md +143 -0
  35. package/templates/drizzle/ROADMAP.pt-BR.md +143 -0
  36. package/templates/drizzle/TESTING.md +136 -0
  37. package/templates/drizzle/TESTING.pt-BR.md +136 -0
  38. package/templates/drizzle/docker-compose.yml +82 -0
  39. package/templates/drizzle/docs/adding-a-module.md +585 -0
  40. package/templates/drizzle/docs/features-markers.md +457 -0
  41. package/templates/drizzle/drizzle.config.ts +49 -0
  42. package/templates/drizzle/nest-cli.json +8 -0
  43. package/templates/drizzle/package.json +96 -0
  44. package/templates/drizzle/src/app.module.ts +72 -0
  45. package/templates/drizzle/src/auth/auth.controller.ts +283 -0
  46. package/templates/drizzle/src/auth/auth.module.ts +81 -0
  47. package/templates/drizzle/src/auth/auth.service.spec.ts +111 -0
  48. package/templates/drizzle/src/auth/auth.service.ts +631 -0
  49. package/templates/drizzle/src/auth/drizzle-session.store.ts +263 -0
  50. package/templates/drizzle/src/auth/dto/forgot-password.dto.ts +9 -0
  51. package/templates/drizzle/src/auth/dto/login.dto.ts +10 -0
  52. package/templates/drizzle/src/auth/dto/refresh-token.dto.ts +9 -0
  53. package/templates/drizzle/src/auth/dto/register.dto.ts +11 -0
  54. package/templates/drizzle/src/auth/dto/reset-password.dto.ts +10 -0
  55. package/templates/drizzle/src/auth/guards/github-auth.guard.ts +10 -0
  56. package/templates/drizzle/src/auth/guards/google-auth.guard.ts +10 -0
  57. package/templates/drizzle/src/auth/guards/jwt-auth.guard.ts +25 -0
  58. package/templates/drizzle/src/auth/guards/session-auth.guard.spec.ts +77 -0
  59. package/templates/drizzle/src/auth/guards/session-auth.guard.ts +37 -0
  60. package/templates/drizzle/src/auth/session.service.spec.ts +74 -0
  61. package/templates/drizzle/src/auth/session.service.ts +96 -0
  62. package/templates/drizzle/src/auth/strategies/github.strategy.ts +41 -0
  63. package/templates/drizzle/src/auth/strategies/google.strategy.ts +43 -0
  64. package/templates/drizzle/src/auth/strategies/jwt.strategy.ts +25 -0
  65. package/templates/drizzle/src/auth/token.service.ts +175 -0
  66. package/templates/drizzle/src/common/constants/permissions.ts +8 -0
  67. package/templates/drizzle/src/common/constants/role-permissions.ts +9 -0
  68. package/templates/drizzle/src/common/constants/role.enum.ts +5 -0
  69. package/templates/drizzle/src/common/decorators/current-user.decorator.ts +8 -0
  70. package/templates/drizzle/src/common/decorators/permissions.decorator.ts +7 -0
  71. package/templates/drizzle/src/common/decorators/public.decorator.ts +4 -0
  72. package/templates/drizzle/src/common/decorators/roles.decorator.ts +6 -0
  73. package/templates/drizzle/src/common/filters/http-exception.filter.ts +34 -0
  74. package/templates/drizzle/src/common/guards/permissions.guard.spec.ts +54 -0
  75. package/templates/drizzle/src/common/guards/permissions.guard.ts +28 -0
  76. package/templates/drizzle/src/common/guards/roles.guard.spec.ts +37 -0
  77. package/templates/drizzle/src/common/guards/roles.guard.ts +24 -0
  78. package/templates/drizzle/src/common/interceptors/logging.interceptor.ts +23 -0
  79. package/templates/drizzle/src/common/middleware/csrf.middleware.spec.ts +97 -0
  80. package/templates/drizzle/src/common/middleware/csrf.middleware.ts +46 -0
  81. package/templates/drizzle/src/common/utils/avatar-storage.util.ts +32 -0
  82. package/templates/drizzle/src/common/utils/hash.util.ts +5 -0
  83. package/templates/drizzle/src/config/env.validation.ts +50 -0
  84. package/templates/drizzle/src/database/database-lifecycle.service.ts +30 -0
  85. package/templates/drizzle/src/database/database.constants.ts +2 -0
  86. package/templates/drizzle/src/database/database.decorators.ts +5 -0
  87. package/templates/drizzle/src/database/database.module.ts +96 -0
  88. package/templates/drizzle/src/database/database.types.ts +28 -0
  89. package/templates/drizzle/src/database/schema/index.ts +11 -0
  90. package/templates/drizzle/src/database/schema/mysql.schema.ts +215 -0
  91. package/templates/drizzle/src/database/schema/postgres.schema.ts +218 -0
  92. package/templates/drizzle/src/database/schema/sqlite.schema.ts +190 -0
  93. package/templates/drizzle/src/database/seed.ts +152 -0
  94. package/templates/drizzle/src/health/health.controller.ts +49 -0
  95. package/templates/drizzle/src/health/health.module.ts +19 -0
  96. package/templates/drizzle/src/health/indicators/drizzle-health.indicator.spec.ts +71 -0
  97. package/templates/drizzle/src/health/indicators/drizzle-health.indicator.ts +52 -0
  98. package/templates/drizzle/src/health/indicators/redis-health.indicator.spec.ts +54 -0
  99. package/templates/drizzle/src/health/indicators/redis-health.indicator.ts +33 -0
  100. package/templates/drizzle/src/mail/mail.module.ts +12 -0
  101. package/templates/drizzle/src/mail/mail.processor.ts +48 -0
  102. package/templates/drizzle/src/mail/mail.service.ts +24 -0
  103. package/templates/drizzle/src/mail/templates/email-templates.ts +28 -0
  104. package/templates/drizzle/src/main.ts +145 -0
  105. package/templates/drizzle/src/metrics/metrics.controller.ts +22 -0
  106. package/templates/drizzle/src/metrics/metrics.interceptor.ts +30 -0
  107. package/templates/drizzle/src/metrics/metrics.module.ts +12 -0
  108. package/templates/drizzle/src/metrics/metrics.service.ts +34 -0
  109. package/templates/drizzle/src/types/express-session.d.ts +13 -0
  110. package/templates/drizzle/src/users/dto/create-user.dto.ts +12 -0
  111. package/templates/drizzle/src/users/dto/find-users-query.dto.ts +12 -0
  112. package/templates/drizzle/src/users/dto/update-user.dto.ts +6 -0
  113. package/templates/drizzle/src/users/entities/user.entity.ts +23 -0
  114. package/templates/drizzle/src/users/users.controller.ts +165 -0
  115. package/templates/drizzle/src/users/users.module.ts +10 -0
  116. package/templates/drizzle/src/users/users.service.spec.ts +301 -0
  117. package/templates/drizzle/src/users/users.service.ts +213 -0
  118. package/templates/drizzle/test/auth.e2e-spec.ts +114 -0
  119. package/templates/drizzle/test/session-auth.e2e-spec.ts +163 -0
  120. package/templates/drizzle/test/users.e2e-spec.ts +212 -0
  121. package/templates/drizzle/test/utils/clean-database.ts +104 -0
  122. package/templates/drizzle/test/utils/e2e-setup.ts +77 -0
  123. package/templates/drizzle/tsconfig.build.json +4 -0
  124. package/templates/drizzle/tsconfig.json +25 -0
  125. package/templates/drizzle/vitest.config.ts +20 -0
  126. package/templates/drizzle/vitest.e2e.config.ts +19 -0
  127. package/templates/prisma/.env.example +44 -0
  128. package/templates/prisma/.env.test +37 -0
  129. package/templates/prisma/.github/workflows/ci.yml +109 -0
  130. package/templates/prisma/ARCHITECTURE.md +70 -0
  131. package/templates/prisma/ARCHITECTURE.pt-BR.md +63 -0
  132. package/templates/prisma/CODE_OF_CONDUCT.md +29 -0
  133. package/templates/prisma/CODE_OF_CONDUCT.pt-BR.md +27 -0
  134. package/templates/prisma/CONTRIBUTING.md +57 -0
  135. package/templates/prisma/CONTRIBUTING.pt-BR.md +54 -0
  136. package/templates/prisma/Dockerfile +33 -0
  137. package/templates/prisma/LICENSE +21 -0
  138. package/templates/prisma/README.md +267 -0
  139. package/templates/prisma/README.pt-BR.md +267 -0
  140. package/templates/prisma/ROADMAP.md +75 -0
  141. package/templates/prisma/ROADMAP.pt-BR.md +65 -0
  142. package/templates/prisma/TESTING.md +123 -0
  143. package/templates/prisma/TESTING.pt-BR.md +123 -0
  144. package/templates/prisma/docker-compose.yml +78 -0
  145. package/templates/prisma/docs/adding-a-module.md +230 -0
  146. package/templates/prisma/docs/features-markers.md +50 -0
  147. package/templates/prisma/nest-cli.json +8 -0
  148. package/templates/prisma/package.json +95 -0
  149. package/templates/prisma/prisma/schema.prisma +105 -0
  150. package/templates/prisma/prisma/seed.ts +46 -0
  151. package/templates/prisma/src/app.module.ts +72 -0
  152. package/templates/prisma/src/auth/auth.controller.ts +283 -0
  153. package/templates/prisma/src/auth/auth.module.ts +59 -0
  154. package/templates/prisma/src/auth/auth.service.spec.ts +53 -0
  155. package/templates/prisma/src/auth/auth.service.ts +224 -0
  156. package/templates/prisma/src/auth/dto/forgot-password.dto.ts +9 -0
  157. package/templates/prisma/src/auth/dto/login.dto.ts +10 -0
  158. package/templates/prisma/src/auth/dto/refresh-token.dto.ts +9 -0
  159. package/templates/prisma/src/auth/dto/register.dto.ts +11 -0
  160. package/templates/prisma/src/auth/dto/reset-password.dto.ts +10 -0
  161. package/templates/prisma/src/auth/guards/github-auth.guard.ts +10 -0
  162. package/templates/prisma/src/auth/guards/google-auth.guard.ts +10 -0
  163. package/templates/prisma/src/auth/guards/jwt-auth.guard.ts +25 -0
  164. package/templates/prisma/src/auth/guards/session-auth.guard.spec.ts +77 -0
  165. package/templates/prisma/src/auth/guards/session-auth.guard.ts +37 -0
  166. package/templates/prisma/src/auth/session.service.spec.ts +74 -0
  167. package/templates/prisma/src/auth/session.service.ts +96 -0
  168. package/templates/prisma/src/auth/strategies/github.strategy.ts +41 -0
  169. package/templates/prisma/src/auth/strategies/google.strategy.ts +43 -0
  170. package/templates/prisma/src/auth/strategies/jwt.strategy.ts +25 -0
  171. package/templates/prisma/src/auth/token.service.ts +84 -0
  172. package/templates/prisma/src/common/constants/permissions.ts +8 -0
  173. package/templates/prisma/src/common/constants/role-permissions.ts +9 -0
  174. package/templates/prisma/src/common/decorators/current-user.decorator.ts +8 -0
  175. package/templates/prisma/src/common/decorators/permissions.decorator.ts +7 -0
  176. package/templates/prisma/src/common/decorators/public.decorator.ts +4 -0
  177. package/templates/prisma/src/common/decorators/roles.decorator.ts +6 -0
  178. package/templates/prisma/src/common/filters/http-exception.filter.ts +34 -0
  179. package/templates/prisma/src/common/guards/permissions.guard.spec.ts +54 -0
  180. package/templates/prisma/src/common/guards/permissions.guard.ts +28 -0
  181. package/templates/prisma/src/common/guards/roles.guard.spec.ts +37 -0
  182. package/templates/prisma/src/common/guards/roles.guard.ts +24 -0
  183. package/templates/prisma/src/common/interceptors/logging.interceptor.ts +23 -0
  184. package/templates/prisma/src/common/middleware/csrf.middleware.spec.ts +97 -0
  185. package/templates/prisma/src/common/middleware/csrf.middleware.ts +46 -0
  186. package/templates/prisma/src/common/utils/avatar-storage.util.ts +32 -0
  187. package/templates/prisma/src/common/utils/hash.util.ts +5 -0
  188. package/templates/prisma/src/config/env.validation.ts +49 -0
  189. package/templates/prisma/src/database/prisma.module.ts +9 -0
  190. package/templates/prisma/src/database/prisma.service.ts +13 -0
  191. package/templates/prisma/src/health/health.controller.ts +49 -0
  192. package/templates/prisma/src/health/health.module.ts +19 -0
  193. package/templates/prisma/src/health/indicators/prisma-health.indicator.spec.ts +22 -0
  194. package/templates/prisma/src/health/indicators/prisma-health.indicator.ts +19 -0
  195. package/templates/prisma/src/health/indicators/redis-health.indicator.spec.ts +54 -0
  196. package/templates/prisma/src/health/indicators/redis-health.indicator.ts +33 -0
  197. package/templates/prisma/src/mail/mail.module.ts +12 -0
  198. package/templates/prisma/src/mail/mail.processor.ts +48 -0
  199. package/templates/prisma/src/mail/mail.service.ts +24 -0
  200. package/templates/prisma/src/mail/templates/email-templates.ts +28 -0
  201. package/templates/prisma/src/main.ts +92 -0
  202. package/templates/prisma/src/metrics/metrics.controller.ts +22 -0
  203. package/templates/prisma/src/metrics/metrics.interceptor.ts +30 -0
  204. package/templates/prisma/src/metrics/metrics.module.ts +12 -0
  205. package/templates/prisma/src/metrics/metrics.service.ts +34 -0
  206. package/templates/prisma/src/types/express-session.d.ts +13 -0
  207. package/templates/prisma/src/users/dto/create-user.dto.ts +12 -0
  208. package/templates/prisma/src/users/dto/find-users-query.dto.ts +12 -0
  209. package/templates/prisma/src/users/dto/update-user.dto.ts +6 -0
  210. package/templates/prisma/src/users/entities/user.entity.ts +20 -0
  211. package/templates/prisma/src/users/users.controller.ts +165 -0
  212. package/templates/prisma/src/users/users.module.ts +10 -0
  213. package/templates/prisma/src/users/users.service.spec.ts +104 -0
  214. package/templates/prisma/src/users/users.service.ts +114 -0
  215. package/templates/prisma/test/auth.e2e-spec.ts +75 -0
  216. package/templates/prisma/test/session-auth.e2e-spec.ts +163 -0
  217. package/templates/prisma/test/users.e2e-spec.ts +106 -0
  218. package/templates/prisma/test/utils/clean-database.ts +14 -0
  219. package/templates/prisma/test/utils/e2e-setup.ts +58 -0
  220. package/templates/prisma/tsconfig.build.json +4 -0
  221. package/templates/prisma/tsconfig.json +25 -0
  222. package/templates/prisma/vitest.config.ts +20 -0
  223. package/templates/prisma/vitest.e2e.config.ts +19 -0
  224. package/templates/typeorm/.env.example +45 -0
  225. package/templates/typeorm/.env.test +38 -0
  226. package/templates/typeorm/.github/workflows/ci.yml +116 -0
  227. package/templates/typeorm/ARCHITECTURE.md +158 -0
  228. package/templates/typeorm/ARCHITECTURE.pt-BR.md +156 -0
  229. package/templates/typeorm/CODE_OF_CONDUCT.md +29 -0
  230. package/templates/typeorm/CODE_OF_CONDUCT.pt-BR.md +27 -0
  231. package/templates/typeorm/CONTRIBUTING.md +57 -0
  232. package/templates/typeorm/CONTRIBUTING.pt-BR.md +54 -0
  233. package/templates/typeorm/Dockerfile +43 -0
  234. package/templates/typeorm/LICENSE +21 -0
  235. package/templates/typeorm/README.md +266 -0
  236. package/templates/typeorm/README.pt-BR.md +266 -0
  237. package/templates/typeorm/ROADMAP.md +79 -0
  238. package/templates/typeorm/ROADMAP.pt-BR.md +67 -0
  239. package/templates/typeorm/TESTING.md +114 -0
  240. package/templates/typeorm/TESTING.pt-BR.md +114 -0
  241. package/templates/typeorm/docker-compose.yml +78 -0
  242. package/templates/typeorm/docs/adding-a-module.md +369 -0
  243. package/templates/typeorm/docs/features-markers.md +50 -0
  244. package/templates/typeorm/nest-cli.json +8 -0
  245. package/templates/typeorm/package.json +98 -0
  246. package/templates/typeorm/src/app.module.ts +72 -0
  247. package/templates/typeorm/src/auth/auth.controller.ts +283 -0
  248. package/templates/typeorm/src/auth/auth.module.ts +86 -0
  249. package/templates/typeorm/src/auth/auth.service.spec.ts +111 -0
  250. package/templates/typeorm/src/auth/auth.service.ts +346 -0
  251. package/templates/typeorm/src/auth/dto/forgot-password.dto.ts +9 -0
  252. package/templates/typeorm/src/auth/dto/login.dto.ts +10 -0
  253. package/templates/typeorm/src/auth/dto/refresh-token.dto.ts +9 -0
  254. package/templates/typeorm/src/auth/dto/register.dto.ts +11 -0
  255. package/templates/typeorm/src/auth/dto/reset-password.dto.ts +10 -0
  256. package/templates/typeorm/src/auth/entities/email-verification-token.entity.ts +69 -0
  257. package/templates/typeorm/src/auth/entities/oauth-account.entity.ts +50 -0
  258. package/templates/typeorm/src/auth/entities/password-reset-token.entity.ts +69 -0
  259. package/templates/typeorm/src/auth/entities/refresh-token.entity.ts +69 -0
  260. package/templates/typeorm/src/auth/entities/session.entity.ts +50 -0
  261. package/templates/typeorm/src/auth/guards/github-auth.guard.ts +10 -0
  262. package/templates/typeorm/src/auth/guards/google-auth.guard.ts +10 -0
  263. package/templates/typeorm/src/auth/guards/jwt-auth.guard.ts +25 -0
  264. package/templates/typeorm/src/auth/guards/session-auth.guard.spec.ts +77 -0
  265. package/templates/typeorm/src/auth/guards/session-auth.guard.ts +37 -0
  266. package/templates/typeorm/src/auth/session.service.spec.ts +74 -0
  267. package/templates/typeorm/src/auth/session.service.ts +96 -0
  268. package/templates/typeorm/src/auth/strategies/github.strategy.ts +41 -0
  269. package/templates/typeorm/src/auth/strategies/google.strategy.ts +43 -0
  270. package/templates/typeorm/src/auth/strategies/jwt.strategy.ts +25 -0
  271. package/templates/typeorm/src/auth/token.service.ts +115 -0
  272. package/templates/typeorm/src/common/constants/permissions.ts +8 -0
  273. package/templates/typeorm/src/common/constants/role-permissions.ts +9 -0
  274. package/templates/typeorm/src/common/constants/role.enum.ts +5 -0
  275. package/templates/typeorm/src/common/decorators/current-user.decorator.ts +8 -0
  276. package/templates/typeorm/src/common/decorators/permissions.decorator.ts +7 -0
  277. package/templates/typeorm/src/common/decorators/public.decorator.ts +4 -0
  278. package/templates/typeorm/src/common/decorators/roles.decorator.ts +6 -0
  279. package/templates/typeorm/src/common/filters/http-exception.filter.ts +34 -0
  280. package/templates/typeorm/src/common/guards/permissions.guard.spec.ts +54 -0
  281. package/templates/typeorm/src/common/guards/permissions.guard.ts +28 -0
  282. package/templates/typeorm/src/common/guards/roles.guard.spec.ts +37 -0
  283. package/templates/typeorm/src/common/guards/roles.guard.ts +24 -0
  284. package/templates/typeorm/src/common/interceptors/logging.interceptor.ts +23 -0
  285. package/templates/typeorm/src/common/middleware/csrf.middleware.spec.ts +97 -0
  286. package/templates/typeorm/src/common/middleware/csrf.middleware.ts +46 -0
  287. package/templates/typeorm/src/common/utils/avatar-storage.util.ts +32 -0
  288. package/templates/typeorm/src/common/utils/hash.util.ts +5 -0
  289. package/templates/typeorm/src/config/env.validation.ts +50 -0
  290. package/templates/typeorm/src/database/data-source.ts +20 -0
  291. package/templates/typeorm/src/database/database.module.ts +39 -0
  292. package/templates/typeorm/src/database/seed.ts +81 -0
  293. package/templates/typeorm/src/database/typeorm-options.ts +49 -0
  294. package/templates/typeorm/src/health/health.controller.ts +49 -0
  295. package/templates/typeorm/src/health/health.module.ts +19 -0
  296. package/templates/typeorm/src/health/indicators/redis-health.indicator.spec.ts +54 -0
  297. package/templates/typeorm/src/health/indicators/redis-health.indicator.ts +33 -0
  298. package/templates/typeorm/src/health/indicators/typeorm-health.indicator.spec.ts +49 -0
  299. package/templates/typeorm/src/health/indicators/typeorm-health.indicator.ts +37 -0
  300. package/templates/typeorm/src/mail/mail.module.ts +12 -0
  301. package/templates/typeorm/src/mail/mail.processor.ts +48 -0
  302. package/templates/typeorm/src/mail/mail.service.ts +24 -0
  303. package/templates/typeorm/src/mail/templates/email-templates.ts +28 -0
  304. package/templates/typeorm/src/main.ts +107 -0
  305. package/templates/typeorm/src/metrics/metrics.controller.ts +22 -0
  306. package/templates/typeorm/src/metrics/metrics.interceptor.ts +30 -0
  307. package/templates/typeorm/src/metrics/metrics.module.ts +12 -0
  308. package/templates/typeorm/src/metrics/metrics.service.ts +34 -0
  309. package/templates/typeorm/src/types/express-session.d.ts +13 -0
  310. package/templates/typeorm/src/users/dto/create-user.dto.ts +12 -0
  311. package/templates/typeorm/src/users/dto/find-users-query.dto.ts +12 -0
  312. package/templates/typeorm/src/users/dto/update-user.dto.ts +6 -0
  313. package/templates/typeorm/src/users/entities/user.entity.ts +113 -0
  314. package/templates/typeorm/src/users/users.controller.ts +165 -0
  315. package/templates/typeorm/src/users/users.module.ts +13 -0
  316. package/templates/typeorm/src/users/users.service.spec.ts +183 -0
  317. package/templates/typeorm/src/users/users.service.ts +124 -0
  318. package/templates/typeorm/test/auth.e2e-spec.ts +111 -0
  319. package/templates/typeorm/test/session-auth.e2e-spec.ts +159 -0
  320. package/templates/typeorm/test/users.e2e-spec.ts +183 -0
  321. package/templates/typeorm/test/utils/clean-database.ts +65 -0
  322. package/templates/typeorm/test/utils/e2e-setup.ts +77 -0
  323. package/templates/typeorm/tsconfig.build.json +4 -0
  324. package/templates/typeorm/tsconfig.json +25 -0
  325. package/templates/typeorm/vitest.config.ts +20 -0
  326. package/templates/typeorm/vitest.e2e.config.ts +19 -0
@@ -0,0 +1,585 @@
1
+ # Adicionando um módulo
2
+
3
+ Este guia mostra como adicionar um módulo `posts` ao projeto usando NestJS, Drizzle ORM e Zod.
4
+
5
+ ## 1. Adicione a tabela ao schema
6
+
7
+ Abra o schema correspondente ao banco escolhido:
8
+
9
+ * PostgreSQL: `src/database/schema/postgres.schema.ts`
10
+ * MySQL: `src/database/schema/mysql.schema.ts`
11
+ * SQLite: `src/database/schema/sqlite.schema.ts`
12
+
13
+ Adicione a tabela `posts` usando a implementação correspondente ao banco.
14
+
15
+ ### PostgreSQL
16
+
17
+ Adicione `index` aos imports, caso ainda não esteja presente, e inclua:
18
+
19
+ ```ts
20
+ export const posts = pgTable(
21
+ 'posts',
22
+ {
23
+ id: uuid('id').defaultRandom().primaryKey(),
24
+ title: varchar('title', {
25
+ length: 200,
26
+ }).notNull(),
27
+ content: text('content').notNull(),
28
+ authorId: uuid('author_id')
29
+ .notNull()
30
+ .references(() => users.id, {
31
+ onDelete: 'cascade',
32
+ }),
33
+ createdAt: timestamp('created_at', {
34
+ withTimezone: true,
35
+ mode: 'date',
36
+ })
37
+ .defaultNow()
38
+ .notNull(),
39
+ updatedAt: timestamp('updated_at', {
40
+ withTimezone: true,
41
+ mode: 'date',
42
+ })
43
+ .defaultNow()
44
+ .$onUpdate(() => new Date())
45
+ .notNull(),
46
+ },
47
+ (table) => [
48
+ index('posts_author_id_idx').on(
49
+ table.authorId,
50
+ ),
51
+ ],
52
+ );
53
+ ```
54
+
55
+ ### MySQL
56
+
57
+ Adicione:
58
+
59
+ ```ts
60
+ export const posts = mysqlTable(
61
+ 'posts',
62
+ {
63
+ id: varchar('id', {
64
+ length: 36,
65
+ }).primaryKey(),
66
+ title: varchar('title', {
67
+ length: 200,
68
+ }).notNull(),
69
+ content: text('content').notNull(),
70
+ authorId: varchar('author_id', {
71
+ length: 36,
72
+ })
73
+ .notNull()
74
+ .references(() => users.id, {
75
+ onDelete: 'cascade',
76
+ }),
77
+ createdAt: datetime('created_at', {
78
+ mode: 'date',
79
+ })
80
+ .default(sql`CURRENT_TIMESTAMP`)
81
+ .notNull(),
82
+ updatedAt: datetime('updated_at', {
83
+ mode: 'date',
84
+ })
85
+ .default(sql`CURRENT_TIMESTAMP`)
86
+ .$onUpdate(() => new Date())
87
+ .notNull(),
88
+ },
89
+ (table) => [
90
+ index('posts_author_id_idx').on(
91
+ table.authorId,
92
+ ),
93
+ ],
94
+ );
95
+ ```
96
+
97
+ ### SQLite
98
+
99
+ Adicione:
100
+
101
+ ```ts
102
+ export const posts = sqliteTable(
103
+ 'posts',
104
+ {
105
+ id: text('id').primaryKey(),
106
+ title: text('title').notNull(),
107
+ content: text('content').notNull(),
108
+ authorId: text('author_id')
109
+ .notNull()
110
+ .references(() => users.id, {
111
+ onDelete: 'cascade',
112
+ }),
113
+ createdAt: integer('created_at', {
114
+ mode: 'timestamp_ms',
115
+ })
116
+ .default(sql`(unixepoch() * 1000)`)
117
+ .notNull(),
118
+ updatedAt: integer('updated_at', {
119
+ mode: 'timestamp_ms',
120
+ })
121
+ .default(sql`(unixepoch() * 1000)`)
122
+ .$onUpdate(() => new Date())
123
+ .notNull(),
124
+ },
125
+ (table) => [
126
+ index('posts_author_id_idx').on(
127
+ table.authorId,
128
+ ),
129
+ ],
130
+ );
131
+ ```
132
+
133
+ No final do schema, adicione os tipos inferidos:
134
+
135
+ ```ts
136
+ export type Post = typeof posts.$inferSelect;
137
+ export type NewPost = typeof posts.$inferInsert;
138
+ ```
139
+
140
+ O arquivo `src/database/schema/index.ts` já exporta o schema selecionado pela CLI. Portanto, não é necessário criar outro arquivo de exportação.
141
+
142
+ ## 2. Crie os DTOs
143
+
144
+ Crie `src/posts/dto/create-post.dto.ts`:
145
+
146
+ ```ts
147
+ import { createZodDto } from 'nestjs-zod';
148
+ import { z } from 'zod';
149
+
150
+ export const createPostSchema = z.object({
151
+ title: z.string().min(3).max(200),
152
+ content: z.string().min(1),
153
+ });
154
+
155
+ export class CreatePostDto extends createZodDto(
156
+ createPostSchema,
157
+ ) {}
158
+ ```
159
+
160
+ Crie `src/posts/dto/update-post.dto.ts`:
161
+
162
+ ```ts
163
+ import { createZodDto } from 'nestjs-zod';
164
+ import { createPostSchema } from './create-post.dto';
165
+
166
+ export const updatePostSchema =
167
+ createPostSchema.partial();
168
+
169
+ export class UpdatePostDto extends createZodDto(
170
+ updatePostSchema,
171
+ ) {}
172
+ ```
173
+
174
+ ## 3. Crie o service
175
+
176
+ Crie `src/posts/posts.service.ts`:
177
+
178
+ ```ts
179
+ import { randomUUID } from 'node:crypto';
180
+ import {
181
+ Injectable,
182
+ NotFoundException,
183
+ } from '@nestjs/common';
184
+ import {
185
+ desc,
186
+ eq,
187
+ } from 'drizzle-orm';
188
+ import { InjectDatabase } from '../database/database.decorators';
189
+ import type { DrizzleDatabase } from '../database/database.types';
190
+ import {
191
+ posts,
192
+ users,
193
+ type NewPost,
194
+ } from '../database/schema';
195
+ import { CreatePostDto } from './dto/create-post.dto';
196
+ import { UpdatePostDto } from './dto/update-post.dto';
197
+
198
+ @Injectable()
199
+ export class PostsService {
200
+ constructor(
201
+ @InjectDatabase()
202
+ private readonly database: DrizzleDatabase,
203
+ ) {}
204
+
205
+ async create(
206
+ authorId: string,
207
+ dto: CreatePostDto,
208
+ ) {
209
+ const id = randomUUID();
210
+
211
+ const data: NewPost = {
212
+ id,
213
+ title: dto.title,
214
+ content: dto.content,
215
+ authorId,
216
+ };
217
+
218
+ await this.database
219
+ .insert(posts)
220
+ .values(data);
221
+
222
+ return this.findOne(id);
223
+ }
224
+
225
+ async findAll() {
226
+ return this.database
227
+ .select({
228
+ id: posts.id,
229
+ title: posts.title,
230
+ content: posts.content,
231
+ authorId: posts.authorId,
232
+ createdAt: posts.createdAt,
233
+ updatedAt: posts.updatedAt,
234
+ author: {
235
+ id: users.id,
236
+ name: users.name,
237
+ email: users.email,
238
+ },
239
+ })
240
+ .from(posts)
241
+ .innerJoin(
242
+ users,
243
+ eq(posts.authorId, users.id),
244
+ )
245
+ .orderBy(desc(posts.createdAt));
246
+ }
247
+
248
+ async findOne(id: string) {
249
+ const [post] = await this.database
250
+ .select({
251
+ id: posts.id,
252
+ title: posts.title,
253
+ content: posts.content,
254
+ authorId: posts.authorId,
255
+ createdAt: posts.createdAt,
256
+ updatedAt: posts.updatedAt,
257
+ author: {
258
+ id: users.id,
259
+ name: users.name,
260
+ email: users.email,
261
+ },
262
+ })
263
+ .from(posts)
264
+ .innerJoin(
265
+ users,
266
+ eq(posts.authorId, users.id),
267
+ )
268
+ .where(eq(posts.id, id))
269
+ .limit(1);
270
+
271
+ if (!post) {
272
+ throw new NotFoundException(
273
+ 'Post não encontrado',
274
+ );
275
+ }
276
+
277
+ return post;
278
+ }
279
+
280
+ async update(
281
+ id: string,
282
+ dto: UpdatePostDto,
283
+ ) {
284
+ await this.findOne(id);
285
+
286
+ await this.database
287
+ .update(posts)
288
+ .set({
289
+ ...dto,
290
+ updatedAt: new Date(),
291
+ })
292
+ .where(eq(posts.id, id));
293
+
294
+ return this.findOne(id);
295
+ }
296
+
297
+ async remove(id: string) {
298
+ await this.findOne(id);
299
+
300
+ await this.database
301
+ .delete(posts)
302
+ .where(eq(posts.id, id));
303
+
304
+ return {
305
+ message: 'Post removido com sucesso',
306
+ };
307
+ }
308
+ }
309
+ ```
310
+
311
+ O ID é criado no service para manter o mesmo comportamento entre PostgreSQL, MySQL e SQLite.
312
+
313
+ ## 4. Crie o controller
314
+
315
+ Crie `src/posts/posts.controller.ts`:
316
+
317
+ ```ts
318
+ import {
319
+ Body,
320
+ Controller,
321
+ Delete,
322
+ Get,
323
+ Param,
324
+ Patch,
325
+ Post,
326
+ Req,
327
+ } from '@nestjs/common';
328
+ import type { Request } from 'express';
329
+ import { CreatePostDto } from './dto/create-post.dto';
330
+ import { UpdatePostDto } from './dto/update-post.dto';
331
+ import { PostsService } from './posts.service';
332
+
333
+ @Controller('posts')
334
+ export class PostsController {
335
+ constructor(
336
+ private readonly postsService: PostsService,
337
+ ) {}
338
+
339
+ @Post()
340
+ create(
341
+ @Req() request: Request,
342
+ @Body() dto: CreatePostDto,
343
+ ) {
344
+ return this.postsService.create(
345
+ request.user.id,
346
+ dto,
347
+ );
348
+ }
349
+
350
+ @Get()
351
+ findAll() {
352
+ return this.postsService.findAll();
353
+ }
354
+
355
+ @Get(':id')
356
+ findOne(@Param('id') id: string) {
357
+ return this.postsService.findOne(id);
358
+ }
359
+
360
+ @Patch(':id')
361
+ update(
362
+ @Param('id') id: string,
363
+ @Body() dto: UpdatePostDto,
364
+ ) {
365
+ return this.postsService.update(id, dto);
366
+ }
367
+
368
+ @Delete(':id')
369
+ remove(@Param('id') id: string) {
370
+ return this.postsService.remove(id);
371
+ }
372
+ }
373
+ ```
374
+
375
+ Se o projeto não usa autenticação, remova `@Req()` e receba o `authorId` de outra forma adequada ao domínio.
376
+
377
+ ## 5. Crie o módulo
378
+
379
+ Crie `src/posts/posts.module.ts`:
380
+
381
+ ```ts
382
+ import { Module } from '@nestjs/common';
383
+ import { PostsController } from './posts.controller';
384
+ import { PostsService } from './posts.service';
385
+
386
+ @Module({
387
+ controllers: [PostsController],
388
+ providers: [PostsService],
389
+ exports: [PostsService],
390
+ })
391
+ export class PostsModule {}
392
+ ```
393
+
394
+ O `DatabaseModule` é global. Não é necessário registrar tabelas ou repositórios dentro de cada módulo.
395
+
396
+ Depois, importe `PostsModule` em `src/app.module.ts`:
397
+
398
+ ```ts
399
+ import { PostsModule } from './posts/posts.module';
400
+
401
+ @Module({
402
+ imports: [
403
+ PostsModule,
404
+ ],
405
+ })
406
+ export class AppModule {}
407
+ ```
408
+
409
+ Preserve os outros módulos que já estiverem registrados no `AppModule`.
410
+
411
+ ## 6. Gere e aplique a migration
412
+
413
+ Depois de alterar o schema, gere uma migration:
414
+
415
+ ```bash
416
+ npm run drizzle:generate
417
+ ```
418
+
419
+ Confira os arquivos criados em `drizzle/` antes de aplicar a migration.
420
+
421
+ Aplique as migrations:
422
+
423
+ ```bash
424
+ npm run drizzle:migrate
425
+ ```
426
+
427
+ Durante o desenvolvimento, também é possível sincronizar diretamente o schema:
428
+
429
+ ```bash
430
+ npm run drizzle:push
431
+ ```
432
+
433
+ Prefira migrations em ambientes compartilhados e em produção.
434
+
435
+ ## 7. Adicione testes unitários
436
+
437
+ Nos testes unitários, injete um mock de `DrizzleDatabase` diretamente no service:
438
+
439
+ ```ts
440
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
441
+ import type { DrizzleDatabase } from '../database/database.types';
442
+ import { PostsService } from './posts.service';
443
+
444
+ describe('PostsService', () => {
445
+ let database: Record<string, ReturnType<typeof vi.fn>>;
446
+ let service: PostsService;
447
+
448
+ beforeEach(() => {
449
+ database = {
450
+ select: vi.fn(),
451
+ insert: vi.fn(),
452
+ update: vi.fn(),
453
+ delete: vi.fn(),
454
+ };
455
+
456
+ service = new PostsService(
457
+ database as unknown as DrizzleDatabase,
458
+ );
459
+ });
460
+
461
+ it('deve ser definido', () => {
462
+ expect(service).toBeDefined();
463
+ });
464
+ });
465
+ ```
466
+
467
+ Cada consulta Drizzle usa uma cadeia de métodos. O mock deve reproduzir apenas a cadeia utilizada pelo método testado.
468
+
469
+ Exemplo para uma consulta que termina em `limit()`:
470
+
471
+ ```ts
472
+ const limit = vi.fn().mockResolvedValue([
473
+ {
474
+ id: 'post-1',
475
+ title: 'Meu post',
476
+ },
477
+ ]);
478
+
479
+ const where = vi.fn().mockReturnValue({
480
+ limit,
481
+ });
482
+
483
+ const innerJoin = vi.fn().mockReturnValue({
484
+ where,
485
+ });
486
+
487
+ const from = vi.fn().mockReturnValue({
488
+ innerJoin,
489
+ });
490
+
491
+ database.select.mockReturnValue({
492
+ from,
493
+ });
494
+ ```
495
+
496
+ Use `src/users/users.service.spec.ts` como referência para mocks mais completos.
497
+
498
+ ## 8. Atualize a limpeza E2E
499
+
500
+ Em `test/utils/clean-database.ts`, importe `posts`:
501
+
502
+ ```ts
503
+ import {
504
+ oauthAccounts,
505
+ posts,
506
+ users,
507
+ } from '../../src/database/schema';
508
+ ```
509
+
510
+ Exclua os posts antes dos usuários por causa da chave estrangeira `posts.author_id`.
511
+
512
+ Para PostgreSQL e MySQL:
513
+
514
+ ```ts
515
+ await transaction.delete(posts);
516
+ await transaction.delete(users);
517
+ ```
518
+
519
+ Para SQLite:
520
+
521
+ ```ts
522
+ transaction.delete(posts).run();
523
+ transaction.delete(users).run();
524
+ ```
525
+
526
+ No SQLite com `better-sqlite3`, o callback da transação deve ser síncrono e as consultas devem ser executadas com `.run()`.
527
+
528
+ ## 9. Use transações quando necessário
529
+
530
+ PostgreSQL e MySQL aceitam callbacks assíncronos:
531
+
532
+ ```ts
533
+ await this.database.transaction(
534
+ async (transaction) => {
535
+ await transaction
536
+ .insert(posts)
537
+ .values(post);
538
+
539
+ await transaction
540
+ .update(users)
541
+ .set({
542
+ updatedAt: new Date(),
543
+ })
544
+ .where(eq(users.id, authorId));
545
+ },
546
+ );
547
+ ```
548
+
549
+ Com SQLite e `better-sqlite3`, use um callback síncrono:
550
+
551
+ ```ts
552
+ this.database.transaction(
553
+ (transaction) => {
554
+ transaction
555
+ .insert(posts)
556
+ .values(post)
557
+ .run();
558
+
559
+ transaction
560
+ .update(users)
561
+ .set({
562
+ updatedAt: new Date(),
563
+ })
564
+ .where(eq(users.id, authorId))
565
+ .run();
566
+ },
567
+ );
568
+ ```
569
+
570
+ Se o código precisar oferecer os três bancos simultaneamente, mantenha implementações separadas usando os marcadores de banco do NestForge.
571
+
572
+ ## Checklist
573
+
574
+ * [ ] Tabela adicionada ao schema do banco selecionado
575
+ * [ ] Tipos `Post` e `NewPost` exportados
576
+ * [ ] DTOs Zod criados
577
+ * [ ] Service usa `InjectDatabase` e `DrizzleDatabase`
578
+ * [ ] Controller criado
579
+ * [ ] Módulo importado no `AppModule`
580
+ * [ ] Migration gerada e revisada
581
+ * [ ] Migration aplicada
582
+ * [ ] Testes unitários adicionados
583
+ * [ ] Limpeza dos testes E2E atualizada
584
+ * [ ] Transações adaptadas ao driver utilizado
585
+ * [ ] Swagger e permissões adicionados, quando aplicáveis