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.
- package/LICENSE +21 -0
- package/README.md +371 -0
- package/README.pt-BR.md +370 -0
- package/dist/features/auth-strategy.js +19 -0
- package/dist/features/auth-strategy.js.map +1 -0
- package/dist/features/database.js +59 -0
- package/dist/features/database.js.map +1 -0
- package/dist/features/dependencies.js +57 -0
- package/dist/features/dependencies.js.map +1 -0
- package/dist/features/language.js +172 -0
- package/dist/features/language.js.map +1 -0
- package/dist/features/markers.js +116 -0
- package/dist/features/markers.js.map +1 -0
- package/dist/generator.js +120 -0
- package/dist/generator.js.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/prompts.js +137 -0
- package/dist/prompts.js.map +1 -0
- package/package.json +76 -0
- package/templates/drizzle/.env.example +45 -0
- package/templates/drizzle/.env.test +38 -0
- package/templates/drizzle/.github/workflows/ci.yml +119 -0
- package/templates/drizzle/ARCHITECTURE.md +227 -0
- package/templates/drizzle/ARCHITECTURE.pt-BR.md +225 -0
- package/templates/drizzle/CODE_OF_CONDUCT.md +29 -0
- package/templates/drizzle/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/drizzle/CONTRIBUTING.md +57 -0
- package/templates/drizzle/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/drizzle/Dockerfile +43 -0
- package/templates/drizzle/LICENSE +21 -0
- package/templates/drizzle/README.md +257 -0
- package/templates/drizzle/README.pt-BR.md +257 -0
- package/templates/drizzle/ROADMAP.md +143 -0
- package/templates/drizzle/ROADMAP.pt-BR.md +143 -0
- package/templates/drizzle/TESTING.md +136 -0
- package/templates/drizzle/TESTING.pt-BR.md +136 -0
- package/templates/drizzle/docker-compose.yml +82 -0
- package/templates/drizzle/docs/adding-a-module.md +585 -0
- package/templates/drizzle/docs/features-markers.md +457 -0
- package/templates/drizzle/drizzle.config.ts +49 -0
- package/templates/drizzle/nest-cli.json +8 -0
- package/templates/drizzle/package.json +96 -0
- package/templates/drizzle/src/app.module.ts +72 -0
- package/templates/drizzle/src/auth/auth.controller.ts +283 -0
- package/templates/drizzle/src/auth/auth.module.ts +81 -0
- package/templates/drizzle/src/auth/auth.service.spec.ts +111 -0
- package/templates/drizzle/src/auth/auth.service.ts +631 -0
- package/templates/drizzle/src/auth/drizzle-session.store.ts +263 -0
- package/templates/drizzle/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/drizzle/src/auth/dto/login.dto.ts +10 -0
- package/templates/drizzle/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/drizzle/src/auth/dto/register.dto.ts +11 -0
- package/templates/drizzle/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/drizzle/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/drizzle/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/drizzle/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/drizzle/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/drizzle/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/drizzle/src/auth/session.service.spec.ts +74 -0
- package/templates/drizzle/src/auth/session.service.ts +96 -0
- package/templates/drizzle/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/drizzle/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/drizzle/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/drizzle/src/auth/token.service.ts +175 -0
- package/templates/drizzle/src/common/constants/permissions.ts +8 -0
- package/templates/drizzle/src/common/constants/role-permissions.ts +9 -0
- package/templates/drizzle/src/common/constants/role.enum.ts +5 -0
- package/templates/drizzle/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/drizzle/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/drizzle/src/common/decorators/public.decorator.ts +4 -0
- package/templates/drizzle/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/drizzle/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/drizzle/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/drizzle/src/common/guards/permissions.guard.ts +28 -0
- package/templates/drizzle/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/drizzle/src/common/guards/roles.guard.ts +24 -0
- package/templates/drizzle/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/drizzle/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/drizzle/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/drizzle/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/drizzle/src/common/utils/hash.util.ts +5 -0
- package/templates/drizzle/src/config/env.validation.ts +50 -0
- package/templates/drizzle/src/database/database-lifecycle.service.ts +30 -0
- package/templates/drizzle/src/database/database.constants.ts +2 -0
- package/templates/drizzle/src/database/database.decorators.ts +5 -0
- package/templates/drizzle/src/database/database.module.ts +96 -0
- package/templates/drizzle/src/database/database.types.ts +28 -0
- package/templates/drizzle/src/database/schema/index.ts +11 -0
- package/templates/drizzle/src/database/schema/mysql.schema.ts +215 -0
- package/templates/drizzle/src/database/schema/postgres.schema.ts +218 -0
- package/templates/drizzle/src/database/schema/sqlite.schema.ts +190 -0
- package/templates/drizzle/src/database/seed.ts +152 -0
- package/templates/drizzle/src/health/health.controller.ts +49 -0
- package/templates/drizzle/src/health/health.module.ts +19 -0
- package/templates/drizzle/src/health/indicators/drizzle-health.indicator.spec.ts +71 -0
- package/templates/drizzle/src/health/indicators/drizzle-health.indicator.ts +52 -0
- package/templates/drizzle/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/drizzle/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/drizzle/src/mail/mail.module.ts +12 -0
- package/templates/drizzle/src/mail/mail.processor.ts +48 -0
- package/templates/drizzle/src/mail/mail.service.ts +24 -0
- package/templates/drizzle/src/mail/templates/email-templates.ts +28 -0
- package/templates/drizzle/src/main.ts +145 -0
- package/templates/drizzle/src/metrics/metrics.controller.ts +22 -0
- package/templates/drizzle/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/drizzle/src/metrics/metrics.module.ts +12 -0
- package/templates/drizzle/src/metrics/metrics.service.ts +34 -0
- package/templates/drizzle/src/types/express-session.d.ts +13 -0
- package/templates/drizzle/src/users/dto/create-user.dto.ts +12 -0
- package/templates/drizzle/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/drizzle/src/users/dto/update-user.dto.ts +6 -0
- package/templates/drizzle/src/users/entities/user.entity.ts +23 -0
- package/templates/drizzle/src/users/users.controller.ts +165 -0
- package/templates/drizzle/src/users/users.module.ts +10 -0
- package/templates/drizzle/src/users/users.service.spec.ts +301 -0
- package/templates/drizzle/src/users/users.service.ts +213 -0
- package/templates/drizzle/test/auth.e2e-spec.ts +114 -0
- package/templates/drizzle/test/session-auth.e2e-spec.ts +163 -0
- package/templates/drizzle/test/users.e2e-spec.ts +212 -0
- package/templates/drizzle/test/utils/clean-database.ts +104 -0
- package/templates/drizzle/test/utils/e2e-setup.ts +77 -0
- package/templates/drizzle/tsconfig.build.json +4 -0
- package/templates/drizzle/tsconfig.json +25 -0
- package/templates/drizzle/vitest.config.ts +20 -0
- package/templates/drizzle/vitest.e2e.config.ts +19 -0
- package/templates/prisma/.env.example +44 -0
- package/templates/prisma/.env.test +37 -0
- package/templates/prisma/.github/workflows/ci.yml +109 -0
- package/templates/prisma/ARCHITECTURE.md +70 -0
- package/templates/prisma/ARCHITECTURE.pt-BR.md +63 -0
- package/templates/prisma/CODE_OF_CONDUCT.md +29 -0
- package/templates/prisma/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/prisma/CONTRIBUTING.md +57 -0
- package/templates/prisma/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/prisma/Dockerfile +33 -0
- package/templates/prisma/LICENSE +21 -0
- package/templates/prisma/README.md +267 -0
- package/templates/prisma/README.pt-BR.md +267 -0
- package/templates/prisma/ROADMAP.md +75 -0
- package/templates/prisma/ROADMAP.pt-BR.md +65 -0
- package/templates/prisma/TESTING.md +123 -0
- package/templates/prisma/TESTING.pt-BR.md +123 -0
- package/templates/prisma/docker-compose.yml +78 -0
- package/templates/prisma/docs/adding-a-module.md +230 -0
- package/templates/prisma/docs/features-markers.md +50 -0
- package/templates/prisma/nest-cli.json +8 -0
- package/templates/prisma/package.json +95 -0
- package/templates/prisma/prisma/schema.prisma +105 -0
- package/templates/prisma/prisma/seed.ts +46 -0
- package/templates/prisma/src/app.module.ts +72 -0
- package/templates/prisma/src/auth/auth.controller.ts +283 -0
- package/templates/prisma/src/auth/auth.module.ts +59 -0
- package/templates/prisma/src/auth/auth.service.spec.ts +53 -0
- package/templates/prisma/src/auth/auth.service.ts +224 -0
- package/templates/prisma/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/prisma/src/auth/dto/login.dto.ts +10 -0
- package/templates/prisma/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/prisma/src/auth/dto/register.dto.ts +11 -0
- package/templates/prisma/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/prisma/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/prisma/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/prisma/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/prisma/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/prisma/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/prisma/src/auth/session.service.spec.ts +74 -0
- package/templates/prisma/src/auth/session.service.ts +96 -0
- package/templates/prisma/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/prisma/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/prisma/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/prisma/src/auth/token.service.ts +84 -0
- package/templates/prisma/src/common/constants/permissions.ts +8 -0
- package/templates/prisma/src/common/constants/role-permissions.ts +9 -0
- package/templates/prisma/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/prisma/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/prisma/src/common/decorators/public.decorator.ts +4 -0
- package/templates/prisma/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/prisma/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/prisma/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/prisma/src/common/guards/permissions.guard.ts +28 -0
- package/templates/prisma/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/prisma/src/common/guards/roles.guard.ts +24 -0
- package/templates/prisma/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/prisma/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/prisma/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/prisma/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/prisma/src/common/utils/hash.util.ts +5 -0
- package/templates/prisma/src/config/env.validation.ts +49 -0
- package/templates/prisma/src/database/prisma.module.ts +9 -0
- package/templates/prisma/src/database/prisma.service.ts +13 -0
- package/templates/prisma/src/health/health.controller.ts +49 -0
- package/templates/prisma/src/health/health.module.ts +19 -0
- package/templates/prisma/src/health/indicators/prisma-health.indicator.spec.ts +22 -0
- package/templates/prisma/src/health/indicators/prisma-health.indicator.ts +19 -0
- package/templates/prisma/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/prisma/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/prisma/src/mail/mail.module.ts +12 -0
- package/templates/prisma/src/mail/mail.processor.ts +48 -0
- package/templates/prisma/src/mail/mail.service.ts +24 -0
- package/templates/prisma/src/mail/templates/email-templates.ts +28 -0
- package/templates/prisma/src/main.ts +92 -0
- package/templates/prisma/src/metrics/metrics.controller.ts +22 -0
- package/templates/prisma/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/prisma/src/metrics/metrics.module.ts +12 -0
- package/templates/prisma/src/metrics/metrics.service.ts +34 -0
- package/templates/prisma/src/types/express-session.d.ts +13 -0
- package/templates/prisma/src/users/dto/create-user.dto.ts +12 -0
- package/templates/prisma/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/prisma/src/users/dto/update-user.dto.ts +6 -0
- package/templates/prisma/src/users/entities/user.entity.ts +20 -0
- package/templates/prisma/src/users/users.controller.ts +165 -0
- package/templates/prisma/src/users/users.module.ts +10 -0
- package/templates/prisma/src/users/users.service.spec.ts +104 -0
- package/templates/prisma/src/users/users.service.ts +114 -0
- package/templates/prisma/test/auth.e2e-spec.ts +75 -0
- package/templates/prisma/test/session-auth.e2e-spec.ts +163 -0
- package/templates/prisma/test/users.e2e-spec.ts +106 -0
- package/templates/prisma/test/utils/clean-database.ts +14 -0
- package/templates/prisma/test/utils/e2e-setup.ts +58 -0
- package/templates/prisma/tsconfig.build.json +4 -0
- package/templates/prisma/tsconfig.json +25 -0
- package/templates/prisma/vitest.config.ts +20 -0
- package/templates/prisma/vitest.e2e.config.ts +19 -0
- package/templates/typeorm/.env.example +45 -0
- package/templates/typeorm/.env.test +38 -0
- package/templates/typeorm/.github/workflows/ci.yml +116 -0
- package/templates/typeorm/ARCHITECTURE.md +158 -0
- package/templates/typeorm/ARCHITECTURE.pt-BR.md +156 -0
- package/templates/typeorm/CODE_OF_CONDUCT.md +29 -0
- package/templates/typeorm/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/typeorm/CONTRIBUTING.md +57 -0
- package/templates/typeorm/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/typeorm/Dockerfile +43 -0
- package/templates/typeorm/LICENSE +21 -0
- package/templates/typeorm/README.md +266 -0
- package/templates/typeorm/README.pt-BR.md +266 -0
- package/templates/typeorm/ROADMAP.md +79 -0
- package/templates/typeorm/ROADMAP.pt-BR.md +67 -0
- package/templates/typeorm/TESTING.md +114 -0
- package/templates/typeorm/TESTING.pt-BR.md +114 -0
- package/templates/typeorm/docker-compose.yml +78 -0
- package/templates/typeorm/docs/adding-a-module.md +369 -0
- package/templates/typeorm/docs/features-markers.md +50 -0
- package/templates/typeorm/nest-cli.json +8 -0
- package/templates/typeorm/package.json +98 -0
- package/templates/typeorm/src/app.module.ts +72 -0
- package/templates/typeorm/src/auth/auth.controller.ts +283 -0
- package/templates/typeorm/src/auth/auth.module.ts +86 -0
- package/templates/typeorm/src/auth/auth.service.spec.ts +111 -0
- package/templates/typeorm/src/auth/auth.service.ts +346 -0
- package/templates/typeorm/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/typeorm/src/auth/dto/login.dto.ts +10 -0
- package/templates/typeorm/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/typeorm/src/auth/dto/register.dto.ts +11 -0
- package/templates/typeorm/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/typeorm/src/auth/entities/email-verification-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/oauth-account.entity.ts +50 -0
- package/templates/typeorm/src/auth/entities/password-reset-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/refresh-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/session.entity.ts +50 -0
- package/templates/typeorm/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/typeorm/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/typeorm/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/typeorm/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/typeorm/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/typeorm/src/auth/session.service.spec.ts +74 -0
- package/templates/typeorm/src/auth/session.service.ts +96 -0
- package/templates/typeorm/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/typeorm/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/typeorm/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/typeorm/src/auth/token.service.ts +115 -0
- package/templates/typeorm/src/common/constants/permissions.ts +8 -0
- package/templates/typeorm/src/common/constants/role-permissions.ts +9 -0
- package/templates/typeorm/src/common/constants/role.enum.ts +5 -0
- package/templates/typeorm/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/typeorm/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/typeorm/src/common/decorators/public.decorator.ts +4 -0
- package/templates/typeorm/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/typeorm/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/typeorm/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/typeorm/src/common/guards/permissions.guard.ts +28 -0
- package/templates/typeorm/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/typeorm/src/common/guards/roles.guard.ts +24 -0
- package/templates/typeorm/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/typeorm/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/typeorm/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/typeorm/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/typeorm/src/common/utils/hash.util.ts +5 -0
- package/templates/typeorm/src/config/env.validation.ts +50 -0
- package/templates/typeorm/src/database/data-source.ts +20 -0
- package/templates/typeorm/src/database/database.module.ts +39 -0
- package/templates/typeorm/src/database/seed.ts +81 -0
- package/templates/typeorm/src/database/typeorm-options.ts +49 -0
- package/templates/typeorm/src/health/health.controller.ts +49 -0
- package/templates/typeorm/src/health/health.module.ts +19 -0
- package/templates/typeorm/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/typeorm/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/typeorm/src/health/indicators/typeorm-health.indicator.spec.ts +49 -0
- package/templates/typeorm/src/health/indicators/typeorm-health.indicator.ts +37 -0
- package/templates/typeorm/src/mail/mail.module.ts +12 -0
- package/templates/typeorm/src/mail/mail.processor.ts +48 -0
- package/templates/typeorm/src/mail/mail.service.ts +24 -0
- package/templates/typeorm/src/mail/templates/email-templates.ts +28 -0
- package/templates/typeorm/src/main.ts +107 -0
- package/templates/typeorm/src/metrics/metrics.controller.ts +22 -0
- package/templates/typeorm/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/typeorm/src/metrics/metrics.module.ts +12 -0
- package/templates/typeorm/src/metrics/metrics.service.ts +34 -0
- package/templates/typeorm/src/types/express-session.d.ts +13 -0
- package/templates/typeorm/src/users/dto/create-user.dto.ts +12 -0
- package/templates/typeorm/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/typeorm/src/users/dto/update-user.dto.ts +6 -0
- package/templates/typeorm/src/users/entities/user.entity.ts +113 -0
- package/templates/typeorm/src/users/users.controller.ts +165 -0
- package/templates/typeorm/src/users/users.module.ts +13 -0
- package/templates/typeorm/src/users/users.service.spec.ts +183 -0
- package/templates/typeorm/src/users/users.service.ts +124 -0
- package/templates/typeorm/test/auth.e2e-spec.ts +111 -0
- package/templates/typeorm/test/session-auth.e2e-spec.ts +159 -0
- package/templates/typeorm/test/users.e2e-spec.ts +183 -0
- package/templates/typeorm/test/utils/clean-database.ts +65 -0
- package/templates/typeorm/test/utils/e2e-setup.ts +77 -0
- package/templates/typeorm/tsconfig.build.json +4 -0
- package/templates/typeorm/tsconfig.json +25 -0
- package/templates/typeorm/vitest.config.ts +20 -0
- 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
|