@mikemajesty/microservice-crud 0.0.1

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 (44) hide show
  1. package/README.md +92 -0
  2. package/bin/microservice-crud +3 -0
  3. package/package.json +32 -0
  4. package/src/cli.js +314 -0
  5. package/src/scafold/mongo/.gitkeep +0 -0
  6. package/src/scafold/postgres/.gitkeep +0 -0
  7. package/src/templates/mongo/core/entity/entity.js +49 -0
  8. package/src/templates/mongo/core/repository/repository.js +18 -0
  9. package/src/templates/mongo/core/use-cases/__tests__/create.spec.js +63 -0
  10. package/src/templates/mongo/core/use-cases/__tests__/delete.spec.js +74 -0
  11. package/src/templates/mongo/core/use-cases/__tests__/getByID.spec.js +70 -0
  12. package/src/templates/mongo/core/use-cases/__tests__/list.spec.js +80 -0
  13. package/src/templates/mongo/core/use-cases/__tests__/update.spec.js +78 -0
  14. package/src/templates/mongo/core/use-cases/create.js +28 -0
  15. package/src/templates/mongo/core/use-cases/delete.js +37 -0
  16. package/src/templates/mongo/core/use-cases/getByID.js +32 -0
  17. package/src/templates/mongo/core/use-cases/list.js +26 -0
  18. package/src/templates/mongo/core/use-cases/update.js +42 -0
  19. package/src/templates/mongo/modules/adapter.js +42 -0
  20. package/src/templates/mongo/modules/controller.js +95 -0
  21. package/src/templates/mongo/modules/module.js +100 -0
  22. package/src/templates/mongo/modules/repository.js +36 -0
  23. package/src/templates/mongo/modules/schema.js +45 -0
  24. package/src/templates/mongo/modules/swagger.js +91 -0
  25. package/src/templates/mongo/modules/types.js +49 -0
  26. package/src/templates/postgres/core/entity/entity.js +49 -0
  27. package/src/templates/postgres/core/repository/repository.js +18 -0
  28. package/src/templates/postgres/core/use-cases/__tests__/create.spec.js +58 -0
  29. package/src/templates/postgres/core/use-cases/__tests__/delete.spec.js +73 -0
  30. package/src/templates/postgres/core/use-cases/__tests__/getByID.spec.js +69 -0
  31. package/src/templates/postgres/core/use-cases/__tests__/list.spec.js +79 -0
  32. package/src/templates/postgres/core/use-cases/__tests__/update.spec.js +70 -0
  33. package/src/templates/postgres/core/use-cases/create.js +28 -0
  34. package/src/templates/postgres/core/use-cases/delete.js +37 -0
  35. package/src/templates/postgres/core/use-cases/getByID.js +32 -0
  36. package/src/templates/postgres/core/use-cases/list.js +23 -0
  37. package/src/templates/postgres/core/use-cases/update.js +39 -0
  38. package/src/templates/postgres/modules/adapter.js +42 -0
  39. package/src/templates/postgres/modules/controller.js +95 -0
  40. package/src/templates/postgres/modules/module.js +80 -0
  41. package/src/templates/postgres/modules/repository.js +47 -0
  42. package/src/templates/postgres/modules/schema.js +32 -0
  43. package/src/templates/postgres/modules/swagger.js +90 -0
  44. package/src/templates/postgres/modules/types.js +52 -0
@@ -0,0 +1,32 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getCoreUsecaseGetByID = (name) => `import { ${capitalizeFirstLetter(name)}GetByIDInput, ${capitalizeFirstLetter(name)}GetByIDOutput } from '@/modules/${name}/types';
7
+ import { ${capitalizeFirstLetter(name)}GetByIdSchema } from '@/modules/${name}/types';
8
+ import { ValidateSchema } from '@/utils/decorators/validate-schema.decorator';
9
+ import { ApiNotFoundException } from '@/utils/exception';
10
+
11
+ import { ${capitalizeFirstLetter(name)}Entity } from '../entity/${name}';
12
+ import { I${capitalizeFirstLetter(name)}Repository } from '../repository/${name}';
13
+
14
+ export class ${capitalizeFirstLetter(name)}GetByIdUsecase {
15
+ constructor(private readonly ${name}Repository: I${capitalizeFirstLetter(name)}Repository) {}
16
+
17
+ @ValidateSchema(${capitalizeFirstLetter(name)}GetByIdSchema)
18
+ async execute({ id }: ${capitalizeFirstLetter(name)}GetByIDInput): Promise<${capitalizeFirstLetter(name)}GetByIDOutput> {
19
+ const ${name} = await this.${name}Repository.findById(id);
20
+
21
+ if (!${name}) {
22
+ throw new ApiNotFoundException('${name}NotFound');
23
+ }
24
+
25
+ return new ${capitalizeFirstLetter(name)}Entity(${name});
26
+ }
27
+ }
28
+ `
29
+
30
+ module.exports = {
31
+ getCoreUsecaseGetByID
32
+ }
@@ -0,0 +1,23 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getCoreUsecaseList = (name) => `import { ${capitalizeFirstLetter(name)}ListInput, ${capitalizeFirstLetter(name)}ListOutput, ${capitalizeFirstLetter(name)}ListSchema } from '@/modules/${name}/types';
7
+ import { ValidateSchema } from '@/utils/decorators/validate-schema.decorator';
8
+
9
+ import { I${capitalizeFirstLetter(name)}Repository } from '../repository/${name}';
10
+
11
+ export class ${capitalizeFirstLetter(name)}ListUsecase {
12
+ constructor(private readonly ${name}Repository: I${capitalizeFirstLetter(name)}Repository) {}
13
+
14
+ @ValidateSchema(${capitalizeFirstLetter(name)}ListSchema)
15
+ async execute(input: ${capitalizeFirstLetter(name)}ListInput): Promise<${capitalizeFirstLetter(name)}ListOutput> {
16
+ return await this.${name}Repository.paginate(input);
17
+ }
18
+ }
19
+ `
20
+
21
+ module.exports = {
22
+ getCoreUsecaseList
23
+ }
@@ -0,0 +1,39 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getCoreUsecaseUpdate = (name) => `import { I${capitalizeFirstLetter(name)}Repository } from '@/core/${name}/repository/${name}';
7
+ import { ${capitalizeFirstLetter(name)}UpdateInput, ${capitalizeFirstLetter(name)}UpdateOutput, ${capitalizeFirstLetter(name)}UpdateSchema } from '@/modules/${name}/types';
8
+ import { ValidateSchema } from '@/utils/decorators/validate-schema.decorator';
9
+ import { ApiNotFoundException } from '@/utils/exception';
10
+
11
+ import { ${capitalizeFirstLetter(name)}Entity } from './../entity/${name}';
12
+
13
+ export class ${capitalizeFirstLetter(name)}UpdateUsecase {
14
+ constructor(private readonly ${name}Repository: I${capitalizeFirstLetter(name)}Repository) {}
15
+
16
+ @ValidateSchema(${capitalizeFirstLetter(name)}UpdateSchema)
17
+ async execute(input: ${capitalizeFirstLetter(name)}UpdateInput): Promise<${capitalizeFirstLetter(name)}UpdateOutput> {
18
+ const ${name} = await this.${name}Repository.findById(input.id);
19
+
20
+ if (!${name}) {
21
+ throw new ApiNotFoundException('${name}NotFound');
22
+ }
23
+
24
+ const ${name}Finded = new ${capitalizeFirstLetter(name)}Entity(${name});
25
+
26
+ const entity = new ${capitalizeFirstLetter(name)}Entity({ ...${name}Finded, ...input });
27
+
28
+ await this.${name}Repository.updateOne({ id: entity.id }, entity);
29
+
30
+ const updated = await this.${name}Repository.findById(entity.id);
31
+
32
+ return new ${capitalizeFirstLetter(name)}Entity(updated);
33
+ }
34
+ }
35
+ `
36
+
37
+ module.exports = {
38
+ getCoreUsecaseUpdate
39
+ }
@@ -0,0 +1,42 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getModuleAdapter = (name) => `import {
7
+ ${capitalizeFirstLetter(name)}CreateInput,
8
+ ${capitalizeFirstLetter(name)}CreateOutput,
9
+ ${capitalizeFirstLetter(name)}DeleteInput,
10
+ ${capitalizeFirstLetter(name)}DeleteOutput,
11
+ ${capitalizeFirstLetter(name)}GetByIDInput,
12
+ ${capitalizeFirstLetter(name)}GetByIDOutput,
13
+ ${capitalizeFirstLetter(name)}ListInput,
14
+ ${capitalizeFirstLetter(name)}ListOutput,
15
+ ${capitalizeFirstLetter(name)}UpdateInput,
16
+ ${capitalizeFirstLetter(name)}UpdateOutput
17
+ } from './types';
18
+
19
+ export abstract class I${capitalizeFirstLetter(name)}CreateAdapter {
20
+ abstract execute(input: ${capitalizeFirstLetter(name)}CreateInput): Promise<${capitalizeFirstLetter(name)}CreateOutput>;
21
+ }
22
+
23
+ export abstract class I${capitalizeFirstLetter(name)}UpdateAdapter {
24
+ abstract execute(input: ${capitalizeFirstLetter(name)}UpdateInput): Promise<${capitalizeFirstLetter(name)}UpdateOutput>;
25
+ }
26
+
27
+ export abstract class I${capitalizeFirstLetter(name)}GetByIDAdapter {
28
+ abstract execute(input: ${capitalizeFirstLetter(name)}GetByIDInput): Promise<${capitalizeFirstLetter(name)}GetByIDOutput>;
29
+ }
30
+
31
+ export abstract class I${capitalizeFirstLetter(name)}ListAdapter {
32
+ abstract execute(input: ${capitalizeFirstLetter(name)}ListInput): Promise<${capitalizeFirstLetter(name)}ListOutput>;
33
+ }
34
+
35
+ export abstract class I${capitalizeFirstLetter(name)}DeleteAdapter {
36
+ abstract execute(input: ${capitalizeFirstLetter(name)}DeleteInput): Promise<${capitalizeFirstLetter(name)}DeleteOutput>;
37
+ }
38
+ `
39
+
40
+ module.exports = {
41
+ getModuleAdapter
42
+ }
@@ -0,0 +1,95 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getModuleController = (name) => `import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
7
+ import { ApiBearerAuth, ApiBody, ApiParam, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
8
+
9
+ import { UserRole } from '@/core/user/entity/user';
10
+ import { Roles } from '@/utils/decorators/role.decorator';
11
+ import { SearchHttpSchema } from '@/utils/search';
12
+ import { SortHttpSchema } from '@/utils/sort';
13
+
14
+ import {
15
+ I${capitalizeFirstLetter(name)}CreateAdapter,
16
+ I${capitalizeFirstLetter(name)}DeleteAdapter,
17
+ I${capitalizeFirstLetter(name)}GetByIDAdapter,
18
+ I${capitalizeFirstLetter(name)}ListAdapter,
19
+ I${capitalizeFirstLetter(name)}UpdateAdapter
20
+ } from './adapter';
21
+ import { SwagggerRequest, SwagggerResponse } from './swagger';
22
+ import {
23
+ ${capitalizeFirstLetter(name)}CreateInput,
24
+ ${capitalizeFirstLetter(name)}CreateOutput,
25
+ ${capitalizeFirstLetter(name)}DeleteInput,
26
+ ${capitalizeFirstLetter(name)}DeleteOutput,
27
+ ${capitalizeFirstLetter(name)}GetByIDInput,
28
+ ${capitalizeFirstLetter(name)}GetByIDOutput,
29
+ ${capitalizeFirstLetter(name)}ListInput,
30
+ ${capitalizeFirstLetter(name)}ListOutput,
31
+ ${capitalizeFirstLetter(name)}UpdateInput,
32
+ ${capitalizeFirstLetter(name)}UpdateOutput
33
+ } from './types';
34
+
35
+ @Controller()
36
+ @ApiTags('${name}')
37
+ @ApiBearerAuth()
38
+ @Roles(UserRole.BACKOFFICE)
39
+ export class ${capitalizeFirstLetter(name)}Controller {
40
+ constructor(
41
+ private readonly ${name}Create: I${capitalizeFirstLetter(name)}CreateAdapter,
42
+ private readonly ${name}Update: I${capitalizeFirstLetter(name)}UpdateAdapter,
43
+ private readonly ${name}GetByID: I${capitalizeFirstLetter(name)}GetByIDAdapter,
44
+ private readonly ${name}List: I${capitalizeFirstLetter(name)}ListAdapter,
45
+ private readonly ${name}Delete: I${capitalizeFirstLetter(name)}DeleteAdapter
46
+ ) {}
47
+
48
+ @Post('/${name}')
49
+ @ApiResponse(SwagggerResponse.create[200])
50
+ @ApiBody(SwagggerRequest.createBody)
51
+ async create(@Body() input: ${capitalizeFirstLetter(name)}CreateInput): ${capitalizeFirstLetter(name)}CreateOutput {
52
+ return await this.${name}Create.execute(input);
53
+ }
54
+
55
+ @Put('/${name}')
56
+ @ApiResponse(SwagggerResponse.update[200])
57
+ @ApiResponse(SwagggerResponse.update[404])
58
+ @ApiBody(SwagggerRequest.updateBody)
59
+ async update(@Body() input: ${capitalizeFirstLetter(name)}UpdateInput): ${capitalizeFirstLetter(name)}UpdateOutput {
60
+ return await this.${name}Update.execute(input);
61
+ }
62
+
63
+ @Get('/${name}/:id')
64
+ @ApiParam({ name: 'id', required: true })
65
+ @ApiResponse(SwagggerResponse.getByID[200])
66
+ @ApiResponse(SwagggerResponse.getByID[404])
67
+ async getById(@Param() input: ${capitalizeFirstLetter(name)}GetByIDInput): ${capitalizeFirstLetter(name)}GetByIDOutput {
68
+ return await this.${name}GetByID.execute(input);
69
+ }
70
+
71
+ @Get('/${name}')
72
+ @ApiQuery(SwagggerRequest.listQuery.pagination.limit)
73
+ @ApiQuery(SwagggerRequest.listQuery.pagination.page)
74
+ @ApiQuery(SwagggerRequest.listQuery.sort)
75
+ @ApiQuery(SwagggerRequest.listQuery.search)
76
+ @ApiResponse(SwagggerResponse.list[200])
77
+ async list(@Query() input: ${capitalizeFirstLetter(name)}ListInput): ${capitalizeFirstLetter(name)}ListOutput {
78
+ input.sort = SortHttpSchema.parse(input.sort);
79
+ input.search = SearchHttpSchema.parse(input.search);
80
+ return await this.${name}List.execute(input);
81
+ }
82
+
83
+ @Delete('/${name}/:id')
84
+ @ApiParam({ name: 'id', required: true })
85
+ @ApiResponse(SwagggerResponse.delete[200])
86
+ @ApiResponse(SwagggerResponse.delete[404])
87
+ async delete(@Param() input: ${capitalizeFirstLetter(name)}DeleteInput): ${capitalizeFirstLetter(name)}DeleteOutput {
88
+ return await this.${name}Delete.execute(input);
89
+ }
90
+ }
91
+ `
92
+
93
+ module.exports = {
94
+ getModuleController
95
+ }
@@ -0,0 +1,80 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getModule = (name) => `import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
7
+ import { getRepositoryToken, TypeOrmModule } from '@nestjs/typeorm';
8
+ import { Repository } from 'typeorm';
9
+
10
+ import { ${capitalizeFirstLetter(name)}Entity } from '@/core/${name}/entity/${name}';
11
+ import { I${capitalizeFirstLetter(name)}Repository } from '@/core/${name}/repository/${name}';
12
+ import { ${capitalizeFirstLetter(name)}CreateUsecase } from '@/core/${name}/use-cases/${name}-create';
13
+ import { ${capitalizeFirstLetter(name)}DeleteUsecase } from '@/core/${name}/use-cases/${name}-delete';
14
+ import { ${capitalizeFirstLetter(name)}GetByIdUsecase } from '@/core/${name}/use-cases/${name}-getByID';
15
+ import { ${capitalizeFirstLetter(name)}ListUsecase } from '@/core/${name}/use-cases/${name}-list';
16
+ import { ${capitalizeFirstLetter(name)}UpdateUsecase } from '@/core/${name}/use-cases/${name}-update';
17
+ import { LoggerModule } from '@/infra/logger';
18
+ import { TokenModule } from '@/libs/auth';
19
+ import { IsLoggedMiddleware } from '@/utils/middlewares/is-logged.middleware';
20
+
21
+ import {
22
+ I${capitalizeFirstLetter(name)}CreateAdapter,
23
+ I${capitalizeFirstLetter(name)}DeleteAdapter,
24
+ I${capitalizeFirstLetter(name)}GetByIDAdapter,
25
+ I${capitalizeFirstLetter(name)}ListAdapter,
26
+ I${capitalizeFirstLetter(name)}UpdateAdapter
27
+ } from './adapter';
28
+ import { ${capitalizeFirstLetter(name)}Controller } from './controller';
29
+ import { ${capitalizeFirstLetter(name)}Repository } from './repository';
30
+ import { ${capitalizeFirstLetter(name)}Schema } from './schema';
31
+
32
+ @Module({
33
+ imports: [TokenModule, LoggerModule, TypeOrmModule.forFeature([${capitalizeFirstLetter(name)}Schema])],
34
+ controllers: [${capitalizeFirstLetter(name)}Controller],
35
+ providers: [
36
+ {
37
+ provide: I${capitalizeFirstLetter(name)}Repository,
38
+ useFactory: (repository: Repository<${capitalizeFirstLetter(name)}Schema & ${capitalizeFirstLetter(name)}Entity>) => {
39
+ return new ${capitalizeFirstLetter(name)}Repository(repository);
40
+ },
41
+ inject: [getRepositoryToken(${capitalizeFirstLetter(name)}Schema)]
42
+ },
43
+ {
44
+ provide: I${capitalizeFirstLetter(name)}CreateAdapter,
45
+ useFactory: (repository: I${capitalizeFirstLetter(name)}Repository) => new ${capitalizeFirstLetter(name)}CreateUsecase(repository),
46
+ inject: [I${capitalizeFirstLetter(name)}Repository]
47
+ },
48
+ {
49
+ provide: I${capitalizeFirstLetter(name)}UpdateAdapter,
50
+ useFactory: (repository: I${capitalizeFirstLetter(name)}Repository) => new ${capitalizeFirstLetter(name)}UpdateUsecase(repository),
51
+ inject: [I${capitalizeFirstLetter(name)}Repository]
52
+ },
53
+ {
54
+ provide: I${capitalizeFirstLetter(name)}GetByIDAdapter,
55
+ useFactory: (repository: I${capitalizeFirstLetter(name)}Repository) => new ${capitalizeFirstLetter(name)}GetByIdUsecase(repository),
56
+ inject: [I${capitalizeFirstLetter(name)}Repository]
57
+ },
58
+ {
59
+ provide: I${capitalizeFirstLetter(name)}ListAdapter,
60
+ useFactory: (repository: I${capitalizeFirstLetter(name)}Repository) => new ${capitalizeFirstLetter(name)}ListUsecase(repository),
61
+ inject: [I${capitalizeFirstLetter(name)}Repository]
62
+ },
63
+ {
64
+ provide: I${capitalizeFirstLetter(name)}DeleteAdapter,
65
+ useFactory: (repository: I${capitalizeFirstLetter(name)}Repository) => new ${capitalizeFirstLetter(name)}DeleteUsecase(repository),
66
+ inject: [I${capitalizeFirstLetter(name)}Repository]
67
+ }
68
+ ],
69
+ exports: []
70
+ })
71
+ export class ${capitalizeFirstLetter(name)}Module implements NestModule {
72
+ configure(consumer: MiddlewareConsumer) {
73
+ consumer.apply(IsLoggedMiddleware).forRoutes(${capitalizeFirstLetter(name)}Controller);
74
+ }
75
+ }
76
+ `
77
+
78
+ module.exports = {
79
+ getModule
80
+ }
@@ -0,0 +1,47 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getModuleRepository = (name) => `import { Injectable } from '@nestjs/common';
7
+ import { Repository } from 'typeorm';
8
+
9
+ import { ${capitalizeFirstLetter(name)}Entity } from '@/core/${name}/entity/${name}';
10
+ import { I${capitalizeFirstLetter(name)}Repository } from '@/core/${name}/repository/${name}';
11
+ import { PostgresRepository } from '@/infra/repository/postgres/repository';
12
+ import { ValidateDatabaseSort } from '@/utils/decorators/validate-allowed-sort-order.decorator';
13
+ import { SearchTypeEnum, ValidatePostgresFilter } from '@/utils/decorators/validate-postgres-filter.decorator';
14
+ import { calucaleSkip } from '@/utils/pagination';
15
+
16
+ import { ${capitalizeFirstLetter(name)}Schema } from './schema';
17
+ import { ${capitalizeFirstLetter(name)}ListInput, ${capitalizeFirstLetter(name)}ListOutput } from './types';
18
+
19
+ @Injectable()
20
+ export class ${capitalizeFirstLetter(name)}Repository
21
+ extends PostgresRepository<${capitalizeFirstLetter(name)}Schema & ${capitalizeFirstLetter(name)}Entity>
22
+ implements Omit<I${capitalizeFirstLetter(name)}Repository, 'updateMany' | 'seed'>
23
+ {
24
+ constructor(readonly repository: Repository<${capitalizeFirstLetter(name)}Schema & ${capitalizeFirstLetter(name)}Entity>) {
25
+ super(repository);
26
+ }
27
+
28
+ @ValidatePostgresFilter([{ name: 'name', type: SearchTypeEnum.like }])
29
+ @ValidateDatabaseSort(['createdAt', 'name'])
30
+ async paginate(input: ${capitalizeFirstLetter(name)}ListInput): Promise<${capitalizeFirstLetter(name)}ListOutput> {
31
+ const skip = calucaleSkip(input);
32
+
33
+ const [docs, total] = await this.repository.findAndCount({
34
+ take: input.limit,
35
+ skip,
36
+ order: input.sort,
37
+ where: input.search
38
+ });
39
+
40
+ return { docs, total, page: input.page, limit: input.limit };
41
+ }
42
+ }
43
+ `
44
+
45
+ module.exports = {
46
+ getModuleRepository
47
+ }
@@ -0,0 +1,32 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getModuleSchema = (name) => `import { Max, Min } from 'class-validator';
7
+ import { BaseEntity, Column, CreateDateColumn, DeleteDateColumn, Entity, UpdateDateColumn } from 'typeorm';
8
+
9
+ @Entity({ name: '${name}_collection' })
10
+ export class ${capitalizeFirstLetter(name)}Schema extends BaseEntity {
11
+ @Column({ type: 'uuid', primary: true })
12
+ id: string;
13
+
14
+ @Column('text')
15
+ @Min(1)
16
+ @Max(200)
17
+ name: string;
18
+
19
+ @CreateDateColumn()
20
+ createdAt: Date;
21
+
22
+ @UpdateDateColumn()
23
+ updatedAt: Date;
24
+
25
+ @DeleteDateColumn({ nullable: true })
26
+ deletedAt: Date;
27
+ }
28
+ `
29
+
30
+ module.exports = {
31
+ getModuleSchema
32
+ }
@@ -0,0 +1,90 @@
1
+ const getModuleSwagger = (name) => `import { CreatedModel } from '@/infra/repository/types';
2
+ import { Swagger } from '@/utils/swagger';
3
+
4
+ const entity = {
5
+ name: '<name>'
6
+ };
7
+
8
+ const entityFull = { ...entity, updatedAt: '<updatedAt>', createdAt: '<createdAt>', deletedAt: '<deletedAt>' };
9
+
10
+ export const SwagggerResponse = {
11
+ create: {
12
+ 200: Swagger.defaultResponseJSON({
13
+ status: 200,
14
+ json: { created: true, id: '<uuid>' } as CreatedModel,
15
+ description: '${name} created.'
16
+ })
17
+ },
18
+ update: {
19
+ 200: Swagger.defaultResponseJSON({
20
+ status: 200,
21
+ json: entityFull,
22
+ description: '${name} updated.'
23
+ }),
24
+ 404: Swagger.defaultResponseError({
25
+ status: 404,
26
+ route: 'api/${name}',
27
+ message: '${name}NotFound',
28
+ description: '${name} not found.'
29
+ })
30
+ },
31
+ getByID: {
32
+ 200: Swagger.defaultResponseJSON({
33
+ status: 200,
34
+ json: entityFull,
35
+ description: '${name} found.'
36
+ }),
37
+ 404: Swagger.defaultResponseError({
38
+ status: 404,
39
+ route: 'api/${name}/:id',
40
+ message: '${name}NotFound',
41
+ description: '${name} not found.'
42
+ })
43
+ },
44
+ delete: {
45
+ 200: Swagger.defaultResponseJSON({
46
+ status: 200,
47
+ json: entityFull,
48
+ description: '${name} found.'
49
+ }),
50
+ 404: Swagger.defaultResponseError({
51
+ status: 404,
52
+ route: 'api/${name}/:id',
53
+ message: '${name}NotFound',
54
+ description: '${name} not found.'
55
+ })
56
+ },
57
+ list: {
58
+ 200: Swagger.defaultResponseJSON({
59
+ status: 200,
60
+ json: { docs: { docs: [entityFull], page: 1, limit: 1, total: 1 } },
61
+ description: '${name} created.'
62
+ })
63
+ }
64
+ };
65
+
66
+ export const SwagggerRequest = {
67
+ createBody: Swagger.defaultRequestJSON({ ...entity, id: undefined }),
68
+ updateBody: Swagger.defaultRequestJSON({ ...entity, id: '<id>' }),
69
+ listQuery: {
70
+ pagination: {
71
+ limit: Swagger.defaultApiQueryOptions({ example: 10, name: 'limit', required: false }),
72
+ page: Swagger.defaultApiQueryOptions({ example: 1, name: 'page', required: false })
73
+ },
74
+ sort: Swagger.defaultApiQueryOptions({
75
+ name: 'sort',
76
+ required: false,
77
+ description: '<b>createdAt:desc,name:asc'
78
+ }),
79
+ search: Swagger.defaultApiQueryOptions({
80
+ name: 'search',
81
+ required: false,
82
+ description: '<b>name:miau,breed:siamese'
83
+ })
84
+ }
85
+ };
86
+ `
87
+
88
+ module.exports = {
89
+ getModuleSwagger
90
+ }
@@ -0,0 +1,52 @@
1
+
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+
6
+ const getModuleType = (name) => `import { z } from 'zod';
7
+
8
+ import { ${capitalizeFirstLetter(name)}EntitySchema } from '@/core/${name}/entity/${name}';
9
+ import { CreatedModel } from '@/infra/repository';
10
+ import { PaginationInput, PaginationOutput, PaginationSchema } from '@/utils/pagination';
11
+ import { SearchSchema } from '@/utils/search';
12
+ import { SortSchema } from '@/utils/sort';
13
+
14
+ type Schema = z.infer<typeof ${capitalizeFirstLetter(name)}EntitySchema>;
15
+
16
+ export const ${capitalizeFirstLetter(name)}CreateSchema = ${capitalizeFirstLetter(name)}EntitySchema.pick({
17
+ name: true
18
+ });
19
+
20
+ export type ${capitalizeFirstLetter(name)}CreateInput = z.infer<typeof ${capitalizeFirstLetter(name)}CreateSchema>;
21
+ export type ${capitalizeFirstLetter(name)}CreateOutput = Promise<CreatedModel>;
22
+
23
+ export const ${capitalizeFirstLetter(name)}UpdateSchema = ${capitalizeFirstLetter(name)}EntitySchema.pick({
24
+ name: true
25
+ })
26
+ .partial()
27
+ .merge(${capitalizeFirstLetter(name)}EntitySchema.pick({ id: true }));
28
+
29
+ export type ${capitalizeFirstLetter(name)}UpdateInput = z.infer<typeof ${capitalizeFirstLetter(name)}UpdateSchema>;
30
+ export type ${capitalizeFirstLetter(name)}UpdateOutput = Promise<Schema>;
31
+
32
+ export const ${capitalizeFirstLetter(name)}GetByIdSchema = ${capitalizeFirstLetter(name)}EntitySchema.pick({
33
+ id: true
34
+ });
35
+ export type ${capitalizeFirstLetter(name)}GetByIDInput = z.infer<typeof ${capitalizeFirstLetter(name)}GetByIdSchema>;
36
+ export type ${capitalizeFirstLetter(name)}GetByIDOutput = Promise<Schema>;
37
+
38
+ export const ${capitalizeFirstLetter(name)}ListSchema = z.intersection(PaginationSchema, SortSchema.merge(SearchSchema));
39
+
40
+ export type ${capitalizeFirstLetter(name)}ListInput = PaginationInput<Schema>;
41
+ export type ${capitalizeFirstLetter(name)}ListOutput = Promise<{ docs: Schema[] } & PaginationOutput>;
42
+
43
+ export const ${capitalizeFirstLetter(name)}DeleteSchema = ${capitalizeFirstLetter(name)}EntitySchema.pick({
44
+ id: true
45
+ });
46
+ export type ${capitalizeFirstLetter(name)}DeleteInput = z.infer<typeof ${capitalizeFirstLetter(name)}DeleteSchema>;
47
+ export type ${capitalizeFirstLetter(name)}DeleteOutput = Promise<Schema>;
48
+ `
49
+
50
+ module.exports = {
51
+ getModuleType
52
+ }