@koalarx/nest 1.19.0 → 3.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.
- package/CHANGELOG.md +15 -0
- package/package.json +5 -37
- package/src/core/backgroud-services/cron-service/cron-job.handler.base.ts +66 -0
- package/src/core/backgroud-services/cron-service/cron-job.handler.spec.ts +38 -0
- package/src/core/backgroud-services/event-service/event-class.ts +5 -0
- package/src/core/backgroud-services/event-service/event-handler.base.ts +17 -0
- package/src/core/backgroud-services/event-service/event-is-trigger.ts +3 -0
- package/src/core/backgroud-services/event-service/event-job.ts +28 -0
- package/src/core/backgroud-services/event-service/event-queue.spec.ts +47 -0
- package/src/core/backgroud-services/event-service/event-queue.ts +107 -0
- package/src/core/constants/query-params.ts +7 -0
- package/src/core/controllers/base.controller.ts +9 -0
- package/src/core/controllers/controller.decorator.ts +10 -0
- package/src/core/controllers/created-registre-response.base.ts +17 -0
- package/src/core/controllers/list-response.base.ts +8 -0
- package/src/core/controllers/pagination.request.ts +41 -0
- package/src/core/controllers/router-config.base.ts +14 -0
- package/src/core/controllers/schemas/boolean.schema.ts +10 -0
- package/src/core/controllers/schemas/document-number.schema.ts +23 -0
- package/src/core/controllers/schemas/email.schema.ts +13 -0
- package/src/core/controllers/schemas/list-query.schema.ts +17 -0
- package/src/core/controllers/schemas/native-enum.schema.ts +34 -0
- package/src/core/controllers/schemas/set-mask-document-number.schema.ts +13 -0
- package/src/core/database/entity.base.ts +95 -0
- package/src/core/database/entity.decorator.spec.ts +71 -0
- package/src/core/database/entity.decorator.ts +39 -0
- package/src/core/database/prisma-client-with-custom-transaction.interface.ts +13 -0
- package/src/core/database/prisma-resolver.ts +99 -0
- package/src/core/database/prisma-transactional-client.ts +43 -0
- package/src/core/database/prisma.service.ts +136 -0
- package/src/core/database/repository.base.ts +548 -0
- package/src/core/dtos/pagination.dto.ts +35 -0
- package/src/core/errors/bad-request.error.ts +8 -0
- package/src/core/errors/conflict.error.ts +7 -0
- package/src/core/errors/error.base.ts +5 -0
- package/src/core/errors/no-content.error.ts +8 -0
- package/src/core/errors/not-allowed.error.ts +8 -0
- package/src/core/errors/resource-not-found.error.ts +8 -0
- package/{core/errors/use-case-error.d.ts → src/core/errors/use-case-error.ts} +1 -1
- package/src/core/errors/user-already-exist.error.ts +7 -0
- package/src/core/errors/wrong-credentials.error.ts +7 -0
- package/src/core/health-check/health-check.controller.ts +13 -0
- package/src/core/health-check/health-check.module.ts +7 -0
- package/src/core/index.ts +56 -0
- package/src/core/koala-app.ts +379 -0
- package/src/core/koala-global-vars.ts +8 -0
- package/src/core/koala-nest-database.module.ts +65 -0
- package/src/core/koala-nest-http.module.ts +44 -0
- package/src/core/koala-nest.module.ts +67 -0
- package/src/core/mapping/auto-mapping-class-context.ts +28 -0
- package/src/core/mapping/auto-mapping-context.ts +26 -0
- package/src/core/mapping/auto-mapping-list.ts +154 -0
- package/src/core/mapping/auto-mapping-profile.ts +3 -0
- package/src/core/mapping/auto-mapping.decorator.ts +54 -0
- package/src/core/mapping/auto-mapping.module.ts +17 -0
- package/src/core/mapping/auto-mapping.service.ts +187 -0
- package/src/core/mapping/create-map.ts +11 -0
- package/src/core/mapping/for-member.ts +16 -0
- package/src/core/request-overflow/request-handler.base.ts +8 -0
- package/src/core/request-overflow/request-result.spec.ts +23 -0
- package/src/core/request-overflow/request-result.ts +41 -0
- package/src/core/request-overflow/request-validator.base.ts +33 -0
- package/src/core/security/strategies/api-key.strategy.ts +45 -0
- package/src/core/utils/assing-object.ts +9 -0
- package/src/core/utils/env.config.ts +17 -0
- package/src/core/utils/filter-request-params.ts +23 -0
- package/src/core/utils/find-on-list.ts +18 -0
- package/src/core/utils/get-type-by-prop.ts +9 -0
- package/src/core/utils/instanciate-class-with-dependencies-injection.ts +12 -0
- package/src/core/utils/interfaces/icomparable.ts +6 -0
- package/src/core/utils/list.spec.ts +81 -0
- package/src/core/utils/list.ts +223 -0
- package/src/core/utils/promise-all.ts +24 -0
- package/src/core/utils/set-mask-document-number.ts +13 -0
- package/src/core/validators/file-validator.ts +113 -0
- package/src/decorators/api-exclude-endpoint-diff-develop.decorator.ts +15 -0
- package/src/decorators/api-property-enum.decorator.ts +58 -0
- package/src/decorators/api-property-only-develop.decorator.ts +6 -0
- package/src/decorators/cookies.decorator.ts +8 -0
- package/src/decorators/is-public.decorator.ts +5 -0
- package/src/decorators/upload.decorator.ts +31 -0
- package/src/env/env.module.ts +8 -0
- package/src/env/env.service.ts +12 -0
- package/src/env/env.ts +14 -0
- package/src/filters/domain-errors.filter.ts +97 -0
- package/src/filters/global-exception.filter.ts +60 -0
- package/src/filters/prisma-validation-exception.filter.ts +73 -0
- package/src/filters/zod-errors.filter.ts +48 -0
- package/src/services/logging/ilogging.service.ts +17 -0
- package/src/services/logging/logging.service.ts +10 -0
- package/src/services/redis/iredis.service.ts +11 -0
- package/src/services/redis/redis.service.ts +70 -0
- package/src/services/redlock/ired-lock.service.ts +4 -0
- package/src/services/redlock/red-lock.service.ts +36 -0
- package/src/test/koala-app-test-dependencies.ts +15 -0
- package/src/test/koala-app-test.ts +103 -0
- package/src/test/repositories/in-memory-base.repository.ts +90 -0
- package/src/test/services/fake-logging.service.ts +7 -0
- package/src/test/services/fake-red-lock.service.ts +11 -0
- package/src/test/utils/create-e2e-database.ts +55 -0
- package/src/test/utils/drop-e2e-database.ts +36 -0
- package/src/test/utils/wait-for.ts +31 -0
- package/tsconfig.lib.json +11 -0
- package/LICENSE +0 -21
- package/README.md +0 -499
- package/core/backgroud-services/cron-service/cron-job.handler.base.d.ts +0 -16
- package/core/backgroud-services/cron-service/cron-job.handler.base.js +0 -49
- package/core/backgroud-services/event-service/event-class.d.ts +0 -5
- package/core/backgroud-services/event-service/event-class.js +0 -11
- package/core/backgroud-services/event-service/event-handler.base.d.ts +0 -8
- package/core/backgroud-services/event-service/event-handler.base.js +0 -14
- package/core/backgroud-services/event-service/event-is-trigger.d.ts +0 -3
- package/core/backgroud-services/event-service/event-is-trigger.js +0 -7
- package/core/backgroud-services/event-service/event-job.d.ts +0 -13
- package/core/backgroud-services/event-service/event-job.js +0 -21
- package/core/backgroud-services/event-service/event-queue.d.ts +0 -17
- package/core/backgroud-services/event-service/event-queue.js +0 -62
- package/core/constants/query-params.d.ts +0 -6
- package/core/constants/query-params.js +0 -8
- package/core/controllers/base.controller.d.ts +0 -4
- package/core/controllers/base.controller.js +0 -6
- package/core/controllers/controller.decorator.d.ts +0 -2
- package/core/controllers/controller.decorator.js +0 -11
- package/core/controllers/created-registre-response.base.d.ts +0 -10
- package/core/controllers/created-registre-response.base.js +0 -35
- package/core/controllers/list-response.base.d.ts +0 -4
- package/core/controllers/list-response.base.js +0 -21
- package/core/controllers/pagination.request.d.ts +0 -10
- package/core/controllers/pagination.request.js +0 -56
- package/core/controllers/router-config.base.d.ts +0 -7
- package/core/controllers/router-config.base.js +0 -18
- package/core/controllers/schemas/boolean.schema.d.ts +0 -2
- package/core/controllers/schemas/boolean.schema.js +0 -12
- package/core/controllers/schemas/document-number.schema.d.ts +0 -1
- package/core/controllers/schemas/document-number.schema.js +0 -26
- package/core/controllers/schemas/email.schema.d.ts +0 -1
- package/core/controllers/schemas/email.schema.js +0 -13
- package/core/controllers/schemas/list-query.schema.d.ts +0 -17
- package/core/controllers/schemas/list-query.schema.js +0 -19
- package/core/controllers/schemas/native-enum.schema.d.ts +0 -7
- package/core/controllers/schemas/native-enum.schema.js +0 -28
- package/core/controllers/schemas/set-mask-document-number.schema.d.ts +0 -1
- package/core/controllers/schemas/set-mask-document-number.schema.js +0 -13
- package/core/database/entity.base.d.ts +0 -20
- package/core/database/entity.base.js +0 -71
- package/core/database/entity.decorator.d.ts +0 -13
- package/core/database/entity.decorator.js +0 -23
- package/core/database/prisma-client-with-custom-transaction.interface.d.ts +0 -8
- package/core/database/prisma-client-with-custom-transaction.interface.js +0 -2
- package/core/database/prisma-resolver.d.ts +0 -2
- package/core/database/prisma-resolver.js +0 -74
- package/core/database/prisma-transactional-client.d.ts +0 -11
- package/core/database/prisma-transactional-client.js +0 -25
- package/core/database/prisma.service.d.ts +0 -24
- package/core/database/prisma.service.js +0 -104
- package/core/database/repository.base.d.ts +0 -44
- package/core/database/repository.base.js +0 -360
- package/core/dtos/pagination.dto.d.ts +0 -9
- package/core/dtos/pagination.dto.js +0 -49
- package/core/errors/bad-request.error.d.ts +0 -5
- package/core/errors/bad-request.error.js +0 -10
- package/core/errors/conflict.error.d.ts +0 -4
- package/core/errors/conflict.error.js +0 -10
- package/core/errors/error.base.d.ts +0 -4
- package/core/errors/error.base.js +0 -11
- package/core/errors/no-content.error.d.ts +0 -5
- package/core/errors/no-content.error.js +0 -10
- package/core/errors/not-allowed.error.d.ts +0 -5
- package/core/errors/not-allowed.error.js +0 -10
- package/core/errors/resource-not-found.error.d.ts +0 -5
- package/core/errors/resource-not-found.error.js +0 -10
- package/core/errors/use-case-error.js +0 -2
- package/core/errors/user-already-exist.error.d.ts +0 -4
- package/core/errors/user-already-exist.error.js +0 -10
- package/core/errors/wrong-credentials.error.d.ts +0 -4
- package/core/errors/wrong-credentials.error.js +0 -10
- package/core/health-check/health-check.controller.d.ts +0 -5
- package/core/health-check/health-check.controller.js +0 -32
- package/core/health-check/health-check.module.d.ts +0 -2
- package/core/health-check/health-check.module.js +0 -19
- package/core/index.d.ts +0 -18
- package/core/index.js +0 -7
- package/core/koala-app.d.ts +0 -64
- package/core/koala-app.js +0 -252
- package/core/koala-global-vars.d.ts +0 -7
- package/core/koala-global-vars.js +0 -9
- package/core/koala-nest-database.module.d.ts +0 -16
- package/core/koala-nest-database.module.js +0 -52
- package/core/koala-nest-http.module.d.ts +0 -13
- package/core/koala-nest-http.module.js +0 -37
- package/core/koala-nest.module.d.ts +0 -17
- package/core/koala-nest.module.js +0 -66
- package/core/mapping/auto-mapping-class-context.d.ts +0 -16
- package/core/mapping/auto-mapping-class-context.js +0 -18
- package/core/mapping/auto-mapping-context.d.ts +0 -11
- package/core/mapping/auto-mapping-context.js +0 -24
- package/core/mapping/auto-mapping-list.d.ts +0 -27
- package/core/mapping/auto-mapping-list.js +0 -94
- package/core/mapping/auto-mapping-profile.d.ts +0 -3
- package/core/mapping/auto-mapping-profile.js +0 -6
- package/core/mapping/auto-mapping.decorator.d.ts +0 -9
- package/core/mapping/auto-mapping.decorator.js +0 -27
- package/core/mapping/auto-mapping.module.d.ts +0 -5
- package/core/mapping/auto-mapping.module.js +0 -29
- package/core/mapping/auto-mapping.service.d.ts +0 -14
- package/core/mapping/auto-mapping.service.js +0 -140
- package/core/mapping/create-map.d.ts +0 -3
- package/core/mapping/create-map.js +0 -7
- package/core/mapping/for-member.d.ts +0 -5
- package/core/mapping/for-member.js +0 -8
- package/core/request-overflow/request-handler.base.d.ts +0 -4
- package/core/request-overflow/request-handler.base.js +0 -6
- package/core/request-overflow/request-result.d.ts +0 -15
- package/core/request-overflow/request-result.js +0 -37
- package/core/request-overflow/request-validator.base.d.ts +0 -7
- package/core/request-overflow/request-validator.base.js +0 -26
- package/core/security/strategies/api-key.strategy.d.ts +0 -16
- package/core/security/strategies/api-key.strategy.js +0 -31
- package/core/utils/assing-object.d.ts +0 -5
- package/core/utils/assing-object.js +0 -6
- package/core/utils/env.config.d.ts +0 -6
- package/core/utils/env.config.js +0 -18
- package/core/utils/filter-request-params.d.ts +0 -13
- package/core/utils/filter-request-params.js +0 -22
- package/core/utils/find-on-list.d.ts +0 -2
- package/core/utils/find-on-list.js +0 -13
- package/core/utils/get-type-by-prop.d.ts +0 -2
- package/core/utils/get-type-by-prop.js +0 -11
- package/core/utils/instanciate-class-with-dependencies-injection.d.ts +0 -2
- package/core/utils/instanciate-class-with-dependencies-injection.js +0 -10
- package/core/utils/interfaces/icomparable.d.ts +0 -5
- package/core/utils/interfaces/icomparable.js +0 -6
- package/core/utils/list.d.ts +0 -39
- package/core/utils/list.js +0 -168
- package/core/utils/promise-all.d.ts +0 -7
- package/core/utils/promise-all.js +0 -19
- package/core/utils/set-mask-document-number.d.ts +0 -1
- package/core/utils/set-mask-document-number.js +0 -13
- package/core/validators/file-validator.d.ts +0 -27
- package/core/validators/file-validator.js +0 -94
- package/decorators/api-exclude-endpoint-diff-develop.decorator.d.ts +0 -1
- package/decorators/api-exclude-endpoint-diff-develop.decorator.js +0 -9
- package/decorators/api-property-enum.decorator.d.ts +0 -8
- package/decorators/api-property-enum.decorator.js +0 -21
- package/decorators/api-property-only-develop.decorator.d.ts +0 -2
- package/decorators/api-property-only-develop.decorator.js +0 -9
- package/decorators/cookies.decorator.d.ts +0 -1
- package/decorators/cookies.decorator.js +0 -8
- package/decorators/is-public.decorator.d.ts +0 -2
- package/decorators/is-public.decorator.js +0 -7
- package/decorators/upload.decorator.d.ts +0 -1
- package/decorators/upload.decorator.js +0 -18
- package/docs/00-cli-reference.md +0 -201
- package/docs/01-guia-instalacao.md +0 -113
- package/docs/02-configuracao-inicial.md +0 -176
- package/docs/04-tratamento-erros.md +0 -303
- package/docs/05-features-avancadas.md +0 -969
- package/docs/06-decoradores.md +0 -220
- package/docs/07-guia-bun.md +0 -176
- package/docs/08-prisma-client.md +0 -487
- package/docs/09-mcp-vscode-extension.md +0 -437
- package/docs/EXAMPLE.md +0 -1671
- package/docs/README.md +0 -59
- package/env/env.d.ts +0 -25
- package/env/env.js +0 -14
- package/env/env.module.d.ts +0 -2
- package/env/env.module.js +0 -20
- package/env/env.service.d.ts +0 -7
- package/env/env.service.js +0 -28
- package/filters/domain-errors.filter.d.ts +0 -18
- package/filters/domain-errors.filter.js +0 -92
- package/filters/global-exception.filter.d.ts +0 -8
- package/filters/global-exception.filter.js +0 -68
- package/filters/prisma-validation-exception.filter.d.ts +0 -10
- package/filters/prisma-validation-exception.filter.js +0 -82
- package/filters/zod-errors.filter.d.ts +0 -9
- package/filters/zod-errors.filter.js +0 -60
- package/mcp-server/mcp.json.example +0 -27
- package/mcp-server/server.d.ts +0 -2
- package/mcp-server/server.d.ts.map +0 -1
- package/mcp-server/server.js +0 -248
- package/mcp-server/server.js.map +0 -1
- package/services/logging/ilogging.service.d.ts +0 -16
- package/services/logging/ilogging.service.js +0 -6
- package/services/logging/logging.service.d.ts +0 -4
- package/services/logging/logging.service.js +0 -20
- package/services/redis/iredis.service.d.ts +0 -6
- package/services/redis/iredis.service.js +0 -6
- package/services/redis/redis.service.d.ts +0 -14
- package/services/redis/redis.service.js +0 -65
- package/services/redlock/ired-lock.service.d.ts +0 -4
- package/services/redlock/ired-lock.service.js +0 -6
- package/services/redlock/red-lock.service.d.ts +0 -9
- package/services/redlock/red-lock.service.js +0 -46
- package/test/koala-app-test-dependencies.d.ts +0 -10
- package/test/koala-app-test-dependencies.js +0 -13
- package/test/koala-app-test.d.ts +0 -22
- package/test/koala-app-test.js +0 -77
- package/test/repositories/in-memory-base.repository.d.ts +0 -17
- package/test/repositories/in-memory-base.repository.js +0 -65
- package/test/services/fake-logging.service.d.ts +0 -4
- package/test/services/fake-logging.service.js +0 -9
- package/test/services/fake-red-lock.service.d.ts +0 -5
- package/test/services/fake-red-lock.service.js +0 -11
- package/test/utils/create-e2e-database.d.ts +0 -2
- package/test/utils/create-e2e-database.js +0 -47
- package/test/utils/drop-e2e-database.d.ts +0 -2
- package/test/utils/drop-e2e-database.js +0 -33
- package/test/utils/wait-for.d.ts +0 -1
- package/test/utils/wait-for.js +0 -21
- package/tsconfig.lib.tsbuildinfo +0 -1
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
import { toCamelCase } from '@koalarx/utils/KlString'
|
|
2
|
+
import { Type } from '@nestjs/common'
|
|
3
|
+
import { ListResponse } from '..'
|
|
4
|
+
import { PaginationDto } from '../dtos/pagination.dto'
|
|
5
|
+
import { KoalaGlobalVars } from '../koala-global-vars'
|
|
6
|
+
import { IComparableId } from '../utils/interfaces/icomparable'
|
|
7
|
+
import { List } from '../utils/list'
|
|
8
|
+
import { EntityActionType, EntityBase } from './entity.base'
|
|
9
|
+
import { PrismaTransactionalClient } from './prisma-transactional-client'
|
|
10
|
+
import { AutoMappingList } from '../mapping/auto-mapping-list'
|
|
11
|
+
|
|
12
|
+
type RepositoryInclude<TEntity> = Omit<
|
|
13
|
+
{
|
|
14
|
+
[K in keyof TEntity as TEntity[K] extends Function ? never : K]?:
|
|
15
|
+
| boolean
|
|
16
|
+
| (TEntity[K] extends List<infer U>
|
|
17
|
+
? RepositoryInclude<U>
|
|
18
|
+
: RepositoryInclude<TEntity[K]>)
|
|
19
|
+
},
|
|
20
|
+
'_id' | '_action'
|
|
21
|
+
>
|
|
22
|
+
|
|
23
|
+
interface RepositoryInitProps<
|
|
24
|
+
TEntity extends EntityBase<TEntity>,
|
|
25
|
+
TContext extends PrismaTransactionalClient,
|
|
26
|
+
> {
|
|
27
|
+
context: TContext
|
|
28
|
+
modelName: Type<TEntity>
|
|
29
|
+
transactionContext?: Type<TContext>
|
|
30
|
+
include?: RepositoryInclude<TEntity>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export abstract class RepositoryBase<
|
|
34
|
+
TEntity extends EntityBase<TEntity>,
|
|
35
|
+
TContext extends PrismaTransactionalClient = PrismaTransactionalClient,
|
|
36
|
+
TModelKey extends keyof TContext = keyof TContext,
|
|
37
|
+
> {
|
|
38
|
+
protected _context: TContext
|
|
39
|
+
private readonly _modelName: Type<TEntity>
|
|
40
|
+
private readonly _include?: RepositoryInclude<TEntity>
|
|
41
|
+
|
|
42
|
+
constructor({
|
|
43
|
+
context,
|
|
44
|
+
modelName,
|
|
45
|
+
include,
|
|
46
|
+
}: RepositoryInitProps<TEntity, TContext>) {
|
|
47
|
+
this._context = context
|
|
48
|
+
this._modelName = modelName
|
|
49
|
+
this._include = include
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private listRelationEntities(entity: TEntity) {
|
|
53
|
+
const relationEntities: TEntity[] = []
|
|
54
|
+
|
|
55
|
+
Object.keys(entity).forEach((key) => {
|
|
56
|
+
if (entity[key] instanceof List) {
|
|
57
|
+
const list = entity[key]
|
|
58
|
+
|
|
59
|
+
list.toArray('added').forEach((item) => {
|
|
60
|
+
relationEntities.push(item)
|
|
61
|
+
relationEntities.push(...this.listRelationEntities(item))
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
list.toArray('updated').forEach((item) => {
|
|
65
|
+
relationEntities.push(item)
|
|
66
|
+
relationEntities.push(...this.listRelationEntities(item))
|
|
67
|
+
})
|
|
68
|
+
} else if (entity[key] instanceof EntityBase) {
|
|
69
|
+
relationEntities.push(entity[key] as any)
|
|
70
|
+
relationEntities.push(...this.listRelationEntities(entity[key] as any))
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
return relationEntities
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private listToRelationActionList(entity: TEntity) {
|
|
78
|
+
type RelationActionList = Array<{
|
|
79
|
+
modelName: string
|
|
80
|
+
entityInstance: Type<TEntity>
|
|
81
|
+
schema: any
|
|
82
|
+
relations: TEntity[]
|
|
83
|
+
}>
|
|
84
|
+
|
|
85
|
+
const relationCreates: RelationActionList = []
|
|
86
|
+
const relationUpdates: RelationActionList = []
|
|
87
|
+
const relationDeletes: RelationActionList = []
|
|
88
|
+
|
|
89
|
+
Object.keys(entity).forEach((key) => {
|
|
90
|
+
if (entity[key] instanceof List) {
|
|
91
|
+
const list = entity[key]
|
|
92
|
+
const entityInstance = list.entityType! as any
|
|
93
|
+
const modelName = entityInstance.name
|
|
94
|
+
const parentModelName = entity.constructor.name
|
|
95
|
+
|
|
96
|
+
if (modelName) {
|
|
97
|
+
list.toArray('removed').forEach((item) => {
|
|
98
|
+
relationDeletes.push({
|
|
99
|
+
modelName: toCamelCase(modelName),
|
|
100
|
+
entityInstance,
|
|
101
|
+
schema: { where: { id: item._id } },
|
|
102
|
+
relations: [],
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
list.toArray('added').forEach((item) => {
|
|
107
|
+
relationCreates.push({
|
|
108
|
+
modelName: toCamelCase(modelName),
|
|
109
|
+
entityInstance,
|
|
110
|
+
schema: {
|
|
111
|
+
data: {
|
|
112
|
+
...this.entityToPrisma(item),
|
|
113
|
+
[toCamelCase(parentModelName)]: {
|
|
114
|
+
connect: {
|
|
115
|
+
[this.getIdPropName(entity)]:
|
|
116
|
+
entity[this.getIdPropName(entity)],
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
relations: this.listRelationEntities(item),
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
list.toArray('updated').forEach((item) => {
|
|
126
|
+
relationUpdates.push({
|
|
127
|
+
modelName: toCamelCase(modelName),
|
|
128
|
+
entityInstance,
|
|
129
|
+
schema: {
|
|
130
|
+
where: { id: item._id },
|
|
131
|
+
data: this.entityToPrisma(item),
|
|
132
|
+
},
|
|
133
|
+
relations: this.listRelationEntities(item),
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
return { relationCreates, relationUpdates, relationDeletes }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private entityToPrisma(entity: TEntity) {
|
|
144
|
+
const prismaSchema = {}
|
|
145
|
+
|
|
146
|
+
Object.keys(entity)
|
|
147
|
+
.filter((key) => !['id', '_id', '_action'].includes(key))
|
|
148
|
+
.filter(
|
|
149
|
+
(key) =>
|
|
150
|
+
!(entity[key] instanceof Function || entity[key] instanceof List),
|
|
151
|
+
)
|
|
152
|
+
.forEach((key) => {
|
|
153
|
+
if (entity[key] instanceof EntityBase) {
|
|
154
|
+
if (entity[key]._action === EntityActionType.create) {
|
|
155
|
+
if (entity[key][this.getIdPropName(entity)]) {
|
|
156
|
+
prismaSchema[key] = {
|
|
157
|
+
connectOrCreate: {
|
|
158
|
+
where: {
|
|
159
|
+
[this.getIdPropName(entity)]:
|
|
160
|
+
entity[key][this.getIdPropName(entity)],
|
|
161
|
+
},
|
|
162
|
+
create: this.entityToPrisma(entity[key] as any),
|
|
163
|
+
},
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
prismaSchema[key] = {
|
|
167
|
+
create: this.entityToPrisma(entity[key] as any),
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
} else {
|
|
171
|
+
prismaSchema[key] = {
|
|
172
|
+
update: this.entityToPrisma(entity[key] as any),
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
prismaSchema[key] = entity[key]
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
return prismaSchema
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private findManySchema<T>(where: T, pagination?: PaginationDto) {
|
|
184
|
+
return {
|
|
185
|
+
include: this.getInclude(),
|
|
186
|
+
where,
|
|
187
|
+
orderBy: pagination?.generateOrderBy(),
|
|
188
|
+
skip: pagination?.skip(),
|
|
189
|
+
take: (pagination?.limit ?? 0) > 0 ? pagination?.limit : undefined,
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private createEntity(data: any, entityClass?: Type<TEntity>) {
|
|
194
|
+
const entity = new (entityClass || this._modelName)()
|
|
195
|
+
entity._action = EntityActionType.update
|
|
196
|
+
entity.automap(data)
|
|
197
|
+
|
|
198
|
+
return entity
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private orphanRemoval(
|
|
202
|
+
client: PrismaTransactionalClient,
|
|
203
|
+
entity: EntityBase<TEntity>,
|
|
204
|
+
) {
|
|
205
|
+
const where = {}
|
|
206
|
+
|
|
207
|
+
Object.keys(entity)
|
|
208
|
+
.filter((key: string) => key === 'id' || key.includes('Id'))
|
|
209
|
+
.forEach((key) => (where[key] = entity[key]))
|
|
210
|
+
|
|
211
|
+
return client[toCamelCase(entity.constructor.name)].delete({ where })
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private getIdPropName(entity?: TEntity) {
|
|
215
|
+
return (
|
|
216
|
+
Reflect.getMetadata(
|
|
217
|
+
'entity:id',
|
|
218
|
+
entity ? entity.constructor.prototype : this._modelName.prototype,
|
|
219
|
+
) ?? 'id'
|
|
220
|
+
)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private getInclude(include?: RepositoryInclude<TEntity>) {
|
|
224
|
+
include = include ?? this._include ?? {}
|
|
225
|
+
|
|
226
|
+
const result = {}
|
|
227
|
+
|
|
228
|
+
Object.keys(include).forEach((key) => {
|
|
229
|
+
if (typeof include[key] === 'boolean') {
|
|
230
|
+
result[key] = include[key]
|
|
231
|
+
} else {
|
|
232
|
+
result[key] = {
|
|
233
|
+
include: this.getInclude(include[key]),
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
return result
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private getPropNameFromEntitySource(source: TEntity, entity: Type<TEntity>) {
|
|
242
|
+
return Object.keys(source).find((key) => {
|
|
243
|
+
const propDefinitions = AutoMappingList.getPropDefinitions(
|
|
244
|
+
source.constructor as any,
|
|
245
|
+
key,
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
if (propDefinitions) {
|
|
249
|
+
if (propDefinitions.type === entity.name) {
|
|
250
|
+
return true
|
|
251
|
+
} else if (source[key] instanceof List) {
|
|
252
|
+
const list = source[key] as List<any>
|
|
253
|
+
return list.entityType?.name === entity.name
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return false
|
|
258
|
+
})
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private persistRelations(
|
|
262
|
+
transaction: PrismaTransactionalClient,
|
|
263
|
+
entity: TEntity,
|
|
264
|
+
) {
|
|
265
|
+
const { relationCreates, relationUpdates, relationDeletes } =
|
|
266
|
+
this.listToRelationActionList(entity)
|
|
267
|
+
|
|
268
|
+
return Promise.all([
|
|
269
|
+
...relationCreates.map((relationCreate) =>
|
|
270
|
+
transaction[relationCreate.modelName]
|
|
271
|
+
.create(relationCreate.schema)
|
|
272
|
+
.then((response) => {
|
|
273
|
+
return Promise.all(
|
|
274
|
+
relationCreate.relations.map((relation) => {
|
|
275
|
+
const relationPropName = this.getPropNameFromEntitySource(
|
|
276
|
+
relation,
|
|
277
|
+
relationCreate.entityInstance,
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
if (!relationPropName) {
|
|
281
|
+
throw new Error(
|
|
282
|
+
`Propname not found for relation entity ${relation.constructor.name} on entity ${relationCreate.entityInstance.constructor.name}`,
|
|
283
|
+
)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const relationEntity = this.createEntity(
|
|
287
|
+
response,
|
|
288
|
+
relationCreate.entityInstance as any,
|
|
289
|
+
)
|
|
290
|
+
relationEntity._action = EntityActionType.create
|
|
291
|
+
|
|
292
|
+
relation[relationPropName] = relationEntity
|
|
293
|
+
|
|
294
|
+
return transaction[toCamelCase(relation.constructor.name)]
|
|
295
|
+
.create({
|
|
296
|
+
data: this.entityToPrisma(relation),
|
|
297
|
+
})
|
|
298
|
+
.then((response: TEntity) => {
|
|
299
|
+
entity[this.getIdPropName(relation)] =
|
|
300
|
+
response[this.getIdPropName(relation)]
|
|
301
|
+
return this.persistRelations(transaction, relation)
|
|
302
|
+
})
|
|
303
|
+
}),
|
|
304
|
+
)
|
|
305
|
+
}),
|
|
306
|
+
),
|
|
307
|
+
...relationUpdates.map((relation) =>
|
|
308
|
+
transaction[relation.modelName].update(relation.schema),
|
|
309
|
+
),
|
|
310
|
+
...relationDeletes.map((relation) => this.removeMany(relation.schema)),
|
|
311
|
+
])
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
protected context(transactionalClient?: TContext): TContext[TModelKey] {
|
|
315
|
+
const modelName = this._modelName.name
|
|
316
|
+
|
|
317
|
+
if (!modelName)
|
|
318
|
+
throw new Error('modelName não informado no contrutor do repositorio')
|
|
319
|
+
|
|
320
|
+
const contextKey = toCamelCase(modelName) as TModelKey
|
|
321
|
+
|
|
322
|
+
if (transactionalClient) {
|
|
323
|
+
return transactionalClient[contextKey] as any
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return this._context[contextKey] as any
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
protected async findById(id: IComparableId): Promise<TEntity | null> {
|
|
330
|
+
return (this.context() as any)
|
|
331
|
+
.findFirst({
|
|
332
|
+
include: this.getInclude(),
|
|
333
|
+
where: { [this.getIdPropName()]: id },
|
|
334
|
+
})
|
|
335
|
+
.then((response: TEntity) => {
|
|
336
|
+
if (response) {
|
|
337
|
+
return this.createEntity(response)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return null
|
|
341
|
+
})
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
protected async findFirst<T>(where: T): Promise<TEntity | null> {
|
|
345
|
+
return (this.context() as any)
|
|
346
|
+
.findFirst({
|
|
347
|
+
include: this.getInclude(),
|
|
348
|
+
where,
|
|
349
|
+
})
|
|
350
|
+
.then((response: TEntity) => {
|
|
351
|
+
if (response) {
|
|
352
|
+
return this.createEntity(response)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return null
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
protected async findUnique<T>(where: T): Promise<TEntity | null> {
|
|
360
|
+
return (this.context() as any)
|
|
361
|
+
.findUnique({
|
|
362
|
+
include: this.getInclude(),
|
|
363
|
+
where,
|
|
364
|
+
})
|
|
365
|
+
.then((response: TEntity) => {
|
|
366
|
+
if (response) {
|
|
367
|
+
return this.createEntity(response)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return null
|
|
371
|
+
})
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
protected async findMany<T>(
|
|
375
|
+
where: T,
|
|
376
|
+
pagination?: PaginationDto,
|
|
377
|
+
): Promise<TEntity[]> {
|
|
378
|
+
return (this.context() as any)
|
|
379
|
+
.findMany(this.findManySchema(where, pagination))
|
|
380
|
+
.then((result: TEntity[]) =>
|
|
381
|
+
result.map((response) => this.createEntity(response)),
|
|
382
|
+
)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
protected async findManyAndCount<T>(
|
|
386
|
+
where: T,
|
|
387
|
+
pagination?: PaginationDto,
|
|
388
|
+
): Promise<ListResponse<TEntity>> {
|
|
389
|
+
const count = await (this.context() as any).count({ where })
|
|
390
|
+
|
|
391
|
+
if (count > 0) {
|
|
392
|
+
const items = await this.findMany(
|
|
393
|
+
where,
|
|
394
|
+
Object.assign(new PaginationDto(), pagination),
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
return { items, count }
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return { items: [], count }
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
protected async saveChanges<TWhere = any>(
|
|
404
|
+
entity: TEntity,
|
|
405
|
+
updateWhere?: TWhere,
|
|
406
|
+
): Promise<TEntity> {
|
|
407
|
+
const prismaEntity = this.entityToPrisma(entity)
|
|
408
|
+
|
|
409
|
+
if (entity._action === EntityActionType.create) {
|
|
410
|
+
return this.withTransaction((client) =>
|
|
411
|
+
(this.context(client) as any)
|
|
412
|
+
.create({
|
|
413
|
+
data: prismaEntity,
|
|
414
|
+
include: this.getInclude(),
|
|
415
|
+
})
|
|
416
|
+
.then((response: TEntity) => {
|
|
417
|
+
entity[this.getIdPropName()] = response[this.getIdPropName()]
|
|
418
|
+
return this.persistRelations(client, entity).then(() => entity)
|
|
419
|
+
}),
|
|
420
|
+
)
|
|
421
|
+
} else {
|
|
422
|
+
const where = updateWhere ?? { id: entity._id }
|
|
423
|
+
|
|
424
|
+
return this.withTransaction((client) =>
|
|
425
|
+
(this.context(client) as any)
|
|
426
|
+
.update({
|
|
427
|
+
where,
|
|
428
|
+
data: prismaEntity,
|
|
429
|
+
})
|
|
430
|
+
.then(() => this.persistRelations(client, entity)),
|
|
431
|
+
).then(() => this.findUnique(where) as Promise<TEntity>)
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
protected async saveMany<TWhere = any>(
|
|
436
|
+
entities: TEntity[],
|
|
437
|
+
updateWhere?: TWhere,
|
|
438
|
+
): Promise<void> {
|
|
439
|
+
await this.withTransaction(async (client) => {
|
|
440
|
+
const prismaEntities = entities.map((entity) =>
|
|
441
|
+
this.entityToPrisma(entity),
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
return Promise.all([
|
|
445
|
+
(this.context(client) as any).createMany({
|
|
446
|
+
data: prismaEntities,
|
|
447
|
+
skipDuplicates: true,
|
|
448
|
+
}),
|
|
449
|
+
...entities
|
|
450
|
+
.filter((entity) => !!entity._id)
|
|
451
|
+
.map((entity) =>
|
|
452
|
+
(this.context(client) as any).update({
|
|
453
|
+
data: entity,
|
|
454
|
+
where: updateWhere ?? { id: entity._id },
|
|
455
|
+
}),
|
|
456
|
+
),
|
|
457
|
+
])
|
|
458
|
+
})
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
protected async remove<TWhere = any>(
|
|
462
|
+
where: TWhere,
|
|
463
|
+
notCascadeEntityProps?: Array<keyof TEntity>,
|
|
464
|
+
externalServices?: Promise<any>,
|
|
465
|
+
) {
|
|
466
|
+
const entity = await this.findUnique(where)
|
|
467
|
+
|
|
468
|
+
if (!entity) {
|
|
469
|
+
throw new Error(`Entity not found for where: ${JSON.stringify(where)}`)
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const relationEntity: EntityBase<TEntity>[] = []
|
|
473
|
+
|
|
474
|
+
Object.keys(entity).forEach((key) => {
|
|
475
|
+
if (
|
|
476
|
+
entity[key] instanceof EntityBase &&
|
|
477
|
+
!notCascadeEntityProps?.includes(key as keyof TEntity)
|
|
478
|
+
) {
|
|
479
|
+
relationEntity.push(entity[key])
|
|
480
|
+
}
|
|
481
|
+
})
|
|
482
|
+
|
|
483
|
+
return this.withTransaction((client) =>
|
|
484
|
+
(externalServices
|
|
485
|
+
? externalServices.then(() => client)
|
|
486
|
+
: Promise.resolve(client)
|
|
487
|
+
).then((client) =>
|
|
488
|
+
(this.context(client) as any)
|
|
489
|
+
.delete({ where })
|
|
490
|
+
.then((response) =>
|
|
491
|
+
Promise.all(
|
|
492
|
+
relationEntity.map((entity) =>
|
|
493
|
+
this.orphanRemoval(client, entity),
|
|
494
|
+
),
|
|
495
|
+
).then(() => response),
|
|
496
|
+
),
|
|
497
|
+
),
|
|
498
|
+
)
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
protected async removeMany<TWhere = any>(
|
|
502
|
+
where: TWhere,
|
|
503
|
+
notCascadeEntityProps?: Array<keyof TEntity>,
|
|
504
|
+
externalServices?: Promise<any>,
|
|
505
|
+
): Promise<void> {
|
|
506
|
+
const entities = await this.findMany(where)
|
|
507
|
+
|
|
508
|
+
if (entities.length === 0) {
|
|
509
|
+
throw new Error(`Entities not found for where: ${JSON.stringify(where)}`)
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
return this.withTransaction((client) =>
|
|
513
|
+
(externalServices
|
|
514
|
+
? externalServices.then(() => client)
|
|
515
|
+
: Promise.resolve(client)
|
|
516
|
+
).then(async (client) => {
|
|
517
|
+
const relationEntity: EntityBase<TEntity>[] = []
|
|
518
|
+
|
|
519
|
+
for (const entity of entities) {
|
|
520
|
+
Object.keys(entity).forEach((key) => {
|
|
521
|
+
if (
|
|
522
|
+
entity[key] instanceof EntityBase &&
|
|
523
|
+
!notCascadeEntityProps?.includes(key as keyof TEntity)
|
|
524
|
+
) {
|
|
525
|
+
relationEntity.push(entity[key])
|
|
526
|
+
}
|
|
527
|
+
})
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
return (this.context(client) as any)
|
|
531
|
+
.deleteMany({ where })
|
|
532
|
+
.then((response) =>
|
|
533
|
+
Promise.all(
|
|
534
|
+
relationEntity.map((entity) =>
|
|
535
|
+
this.orphanRemoval(client, entity),
|
|
536
|
+
),
|
|
537
|
+
).then(() => response),
|
|
538
|
+
)
|
|
539
|
+
}),
|
|
540
|
+
)
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
withTransaction(fn: (prisma: TContext) => Promise<any>) {
|
|
544
|
+
return this._context.withTransaction(async (client) => {
|
|
545
|
+
return fn(new (KoalaGlobalVars.dbTransactionContext as any)(client))
|
|
546
|
+
})
|
|
547
|
+
}
|
|
548
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { QueryDirectionType } from '..'
|
|
2
|
+
import { QUERY_FILTER_PARAMS } from '../constants/query-params'
|
|
3
|
+
import { AutoMap } from '../mapping/auto-mapping.decorator'
|
|
4
|
+
|
|
5
|
+
export class PaginationDto {
|
|
6
|
+
@AutoMap()
|
|
7
|
+
page?: number = 0
|
|
8
|
+
|
|
9
|
+
@AutoMap()
|
|
10
|
+
limit?: number = QUERY_FILTER_PARAMS.limit
|
|
11
|
+
|
|
12
|
+
@AutoMap()
|
|
13
|
+
orderBy?: string = ''
|
|
14
|
+
|
|
15
|
+
@AutoMap()
|
|
16
|
+
direction?: QueryDirectionType = 'asc'
|
|
17
|
+
|
|
18
|
+
skip() {
|
|
19
|
+
return (this.limit ?? 0) * (this.page ?? QUERY_FILTER_PARAMS.page)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
generateOrderBy() {
|
|
23
|
+
if (this.orderBy) {
|
|
24
|
+
const orderByField = this.orderBy.split('.')
|
|
25
|
+
return orderByField.reduceRight(
|
|
26
|
+
(acc, item, index) => ({
|
|
27
|
+
[item]: index === orderByField.length - 1 ? this.direction : acc,
|
|
28
|
+
}),
|
|
29
|
+
{},
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return undefined
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ErrorBase } from './error.base'
|
|
2
|
+
import { UseCaseError } from './use-case-error'
|
|
3
|
+
|
|
4
|
+
export class NotAllowedError extends ErrorBase implements UseCaseError {
|
|
5
|
+
constructor(message?: string, data?: any) {
|
|
6
|
+
super(message ?? 'Você não tem permissão para utilizar esse recurso.', data)
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ErrorBase } from './error.base'
|
|
2
|
+
import { UseCaseError } from './use-case-error'
|
|
3
|
+
|
|
4
|
+
export class ResourceNotFoundError extends ErrorBase implements UseCaseError {
|
|
5
|
+
constructor(name = 'Recurso', data?: any) {
|
|
6
|
+
super(`${name} não encontrado(a).`, data)
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IsPublic } from '@koalarx/nest/decorators/is-public.decorator'
|
|
2
|
+
import { Controller, Get } from '@nestjs/common'
|
|
3
|
+
import { ApiExcludeEndpoint } from '@nestjs/swagger'
|
|
4
|
+
|
|
5
|
+
@Controller('health')
|
|
6
|
+
export class HealthCheckController {
|
|
7
|
+
@Get()
|
|
8
|
+
@IsPublic()
|
|
9
|
+
@ApiExcludeEndpoint()
|
|
10
|
+
healthCheck() {
|
|
11
|
+
return { status: 'ok' }
|
|
12
|
+
}
|
|
13
|
+
}
|