@mondart/nestjs-common-module 1.0.3
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/.eslintrc.js +25 -0
- package/.github/workflows/npm-publish.yml +34 -0
- package/.prettierrc +4 -0
- package/common/caching/caching.module.ts +28 -0
- package/common/caching/caching.service.ts +34 -0
- package/common/caching/index.ts +2 -0
- package/common/constants/index.ts +1 -0
- package/common/constants/validation-constraints.const.ts +14 -0
- package/common/controllers/core-crud.controller.ts +106 -0
- package/common/controllers/index.ts +1 -0
- package/common/decorators/entity-order.decorator.ts +19 -0
- package/common/decorators/index.ts +2 -0
- package/common/decorators/swagger-api-response.decorator.ts +30 -0
- package/common/decorators/validations/default/array-max-size.decorator.ts +9 -0
- package/common/decorators/validations/default/array-min-size.decorator.ts +9 -0
- package/common/decorators/validations/default/array-not-empty.decorator.ts +7 -0
- package/common/decorators/validations/default/index.ts +20 -0
- package/common/decorators/validations/default/is-array.decorator.ts +7 -0
- package/common/decorators/validations/default/is-boolean.decorator.ts +10 -0
- package/common/decorators/validations/default/is-date.decorator.ts +10 -0
- package/common/decorators/validations/default/is-enum.decorator.ts +9 -0
- package/common/decorators/validations/default/is-int.decorator.ts +10 -0
- package/common/decorators/validations/default/is-not-empty-object.decorator.ts +15 -0
- package/common/decorators/validations/default/is-not-empty.decorator.ts +7 -0
- package/common/decorators/validations/default/is-number-string.decorator.ts +12 -0
- package/common/decorators/validations/default/is-object.decorator.ts +7 -0
- package/common/decorators/validations/default/is-positive.decorator.ts +10 -0
- package/common/decorators/validations/default/is-string.decorator.ts +8 -0
- package/common/decorators/validations/default/is-uuid.decorator.ts +7 -0
- package/common/decorators/validations/default/matches.decorator.ts +13 -0
- package/common/decorators/validations/default/max-length.decorator.ts +9 -0
- package/common/decorators/validations/default/max.decorator.ts +13 -0
- package/common/decorators/validations/default/min-length.decorator.ts +9 -0
- package/common/decorators/validations/default/min.decorator.ts +13 -0
- package/common/decorators/validations/index.ts +3 -0
- package/common/decorators/validations/is-id.decorator.ts +20 -0
- package/common/decorators/validations/is-not-empty-string.decorator.ts +14 -0
- package/common/dto/index.ts +2 -0
- package/common/dto/request/base-request.dto.ts +11 -0
- package/common/dto/request/event.dto.ts +25 -0
- package/common/dto/request/id.dto.ts +10 -0
- package/common/dto/request/ids.dto.ts +13 -0
- package/common/dto/request/index.ts +4 -0
- package/common/dto/response/base-response-with-action-dates.dto.ts +19 -0
- package/common/dto/response/base-response.dto.ts +10 -0
- package/common/dto/response/error-response.dto.ts +22 -0
- package/common/dto/response/index.ts +5 -0
- package/common/dto/response/kafka-success-response.dto.ts +56 -0
- package/common/dto/response/success-response.dto.ts +32 -0
- package/common/dto/response/validation.dto.ts +80 -0
- package/common/entities/action-dates.entity.ts +30 -0
- package/common/entities/base.entity.ts +7 -0
- package/common/entities/index.ts +2 -0
- package/common/entities/parent.entity.ts +35 -0
- package/common/enums/environments.enum.ts +6 -0
- package/common/enums/index.ts +2 -0
- package/common/enums/shared-messages.enum.ts +40 -0
- package/common/filters/http-exception.filter.ts +74 -0
- package/common/filters/index.ts +1 -0
- package/common/filters/rpc-exception.filter.ts +40 -0
- package/common/helper/get-env.helper.ts +3 -0
- package/common/helper/get-env.ts +3 -0
- package/common/helper/index.ts +2 -0
- package/common/helper/message-formatter.helper.ts +30 -0
- package/common/helper/multi-inheritance.helper.ts +12 -0
- package/common/helper/multi-inheritance.util.ts +12 -0
- package/common/index.ts +14 -0
- package/common/interface/core-crud-service.option.ts +8 -0
- package/common/interface/custom-validation-arguments.interface.ts +20 -0
- package/common/interface/index.ts +2 -0
- package/common/interface/kafka-success-response.interfase.ts +9 -0
- package/common/interface/pagination-query.ts +39 -0
- package/common/lib/index.ts +1 -0
- package/common/lib/kafka/constant/consumer.const.ts +13 -0
- package/common/lib/kafka/constant/kafka.const.ts +9 -0
- package/common/lib/kafka/index.ts +5 -0
- package/common/lib/kafka/kafka-admin.service.ts +51 -0
- package/common/lib/kafka/kafka-consumer.service.ts +59 -0
- package/common/lib/kafka/kafka-instance.const.ts +9 -0
- package/common/lib/kafka/kafka-logger.ts +25 -0
- package/common/lib/kafka/kafka-producer.service.ts +97 -0
- package/common/lib/kafka/kafka.interface.ts +22 -0
- package/common/lib/kafka/kafka.module.ts +80 -0
- package/common/lib/kafka/kafka.provider.ts +10 -0
- package/common/lib/kafka/kafka.service.ts +224 -0
- package/common/services/core-crud.service.ts +457 -0
- package/common/services/index.ts +1 -0
- package/common/strategy/index.ts +1 -0
- package/common/strategy/type-orm-naming.strategy.ts +17 -0
- package/common/validators/does-exist.validator.ts +42 -0
- package/common/validators/env.validator.ts +28 -0
- package/common/validators/index.ts +4 -0
- package/common/validators/is-unique.validator.ts +70 -0
- package/common/validators/validation-options.ts +38 -0
- package/dist/caching/caching.module.d.ts +2 -0
- package/dist/caching/caching.module.js +41 -0
- package/dist/caching/caching.module.js.map +1 -0
- package/dist/caching/caching.service.d.ts +9 -0
- package/dist/caching/caching.service.js +45 -0
- package/dist/caching/caching.service.js.map +1 -0
- package/dist/caching/index.d.ts +2 -0
- package/dist/caching/index.js +19 -0
- package/dist/caching/index.js.map +1 -0
- package/dist/constants/index.d.ts +1 -0
- package/dist/constants/index.js +18 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/constants/validation-constraints.const.d.ts +14 -0
- package/dist/constants/validation-constraints.const.js +19 -0
- package/dist/constants/validation-constraints.const.js.map +1 -0
- package/dist/controllers/core-crud.controller.d.ts +19 -0
- package/dist/controllers/core-crud.controller.js +72 -0
- package/dist/controllers/core-crud.controller.js.map +1 -0
- package/dist/controllers/index.d.ts +1 -0
- package/dist/controllers/index.js +18 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/decorators/entity-order.decorator.d.ts +2 -0
- package/dist/decorators/entity-order.decorator.js +18 -0
- package/dist/decorators/entity-order.decorator.js.map +1 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.js +19 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/decorators/swagger-api-response.decorator.d.ts +5 -0
- package/dist/decorators/swagger-api-response.decorator.js +25 -0
- package/dist/decorators/swagger-api-response.decorator.js.map +1 -0
- package/dist/decorators/validations/default/array-max-size.decorator.d.ts +1 -0
- package/dist/decorators/validations/default/array-max-size.decorator.js +12 -0
- package/dist/decorators/validations/default/array-max-size.decorator.js.map +1 -0
- package/dist/decorators/validations/default/array-min-size.decorator.d.ts +1 -0
- package/dist/decorators/validations/default/array-min-size.decorator.js +12 -0
- package/dist/decorators/validations/default/array-min-size.decorator.js.map +1 -0
- package/dist/decorators/validations/default/array-not-empty.decorator.d.ts +1 -0
- package/dist/decorators/validations/default/array-not-empty.decorator.js +10 -0
- package/dist/decorators/validations/default/array-not-empty.decorator.js.map +1 -0
- package/dist/decorators/validations/default/index.d.ts +20 -0
- package/dist/decorators/validations/default/index.js +37 -0
- package/dist/decorators/validations/default/index.js.map +1 -0
- package/dist/decorators/validations/default/is-array.decorator.d.ts +1 -0
- package/dist/decorators/validations/default/is-array.decorator.js +10 -0
- package/dist/decorators/validations/default/is-array.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-boolean.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/is-boolean.decorator.js +10 -0
- package/dist/decorators/validations/default/is-boolean.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-date.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/is-date.decorator.js +10 -0
- package/dist/decorators/validations/default/is-date.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-enum.decorator.d.ts +1 -0
- package/dist/decorators/validations/default/is-enum.decorator.js +12 -0
- package/dist/decorators/validations/default/is-enum.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-int.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/is-int.decorator.js +10 -0
- package/dist/decorators/validations/default/is-int.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-not-empty-object.decorator.d.ts +5 -0
- package/dist/decorators/validations/default/is-not-empty-object.decorator.js +13 -0
- package/dist/decorators/validations/default/is-not-empty-object.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-not-empty.decorator.d.ts +1 -0
- package/dist/decorators/validations/default/is-not-empty.decorator.js +10 -0
- package/dist/decorators/validations/default/is-not-empty.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-number-string.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/is-number-string.decorator.js +10 -0
- package/dist/decorators/validations/default/is-number-string.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-object.decorator.d.ts +2 -0
- package/dist/decorators/validations/default/is-object.decorator.js +10 -0
- package/dist/decorators/validations/default/is-object.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-positive.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/is-positive.decorator.js +10 -0
- package/dist/decorators/validations/default/is-positive.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-string.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/is-string.decorator.js +11 -0
- package/dist/decorators/validations/default/is-string.decorator.js.map +1 -0
- package/dist/decorators/validations/default/is-uuid.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/is-uuid.decorator.js +10 -0
- package/dist/decorators/validations/default/is-uuid.decorator.js.map +1 -0
- package/dist/decorators/validations/default/matches.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/matches.decorator.js +10 -0
- package/dist/decorators/validations/default/matches.decorator.js.map +1 -0
- package/dist/decorators/validations/default/max-length.decorator.d.ts +1 -0
- package/dist/decorators/validations/default/max-length.decorator.js +12 -0
- package/dist/decorators/validations/default/max-length.decorator.js.map +1 -0
- package/dist/decorators/validations/default/max.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/max.decorator.js +10 -0
- package/dist/decorators/validations/default/max.decorator.js.map +1 -0
- package/dist/decorators/validations/default/min-length.decorator.d.ts +1 -0
- package/dist/decorators/validations/default/min-length.decorator.js +12 -0
- package/dist/decorators/validations/default/min-length.decorator.js.map +1 -0
- package/dist/decorators/validations/default/min.decorator.d.ts +3 -0
- package/dist/decorators/validations/default/min.decorator.js +10 -0
- package/dist/decorators/validations/default/min.decorator.js.map +1 -0
- package/dist/decorators/validations/index.d.ts +3 -0
- package/dist/decorators/validations/index.js +20 -0
- package/dist/decorators/validations/index.js.map +1 -0
- package/dist/decorators/validations/is-id.decorator.d.ts +4 -0
- package/dist/decorators/validations/is-id.decorator.js +21 -0
- package/dist/decorators/validations/is-id.decorator.js.map +1 -0
- package/dist/decorators/validations/is-not-empty-string.decorator.d.ts +4 -0
- package/dist/decorators/validations/is-not-empty-string.decorator.js +15 -0
- package/dist/decorators/validations/is-not-empty-string.decorator.js.map +1 -0
- package/dist/dto/index.d.ts +2 -0
- package/dist/dto/index.js +19 -0
- package/dist/dto/index.js.map +1 -0
- package/dist/dto/request/base-request.dto.d.ts +4 -0
- package/dist/dto/request/base-request.dto.js +24 -0
- package/dist/dto/request/base-request.dto.js.map +1 -0
- package/dist/dto/request/event.dto.d.ts +8 -0
- package/dist/dto/request/event.dto.js +45 -0
- package/dist/dto/request/event.dto.js.map +1 -0
- package/dist/dto/request/id.dto.d.ts +3 -0
- package/dist/dto/request/id.dto.js +25 -0
- package/dist/dto/request/id.dto.js.map +1 -0
- package/dist/dto/request/ids.dto.d.ts +3 -0
- package/dist/dto/request/ids.dto.js +28 -0
- package/dist/dto/request/ids.dto.js.map +1 -0
- package/dist/dto/request/index.d.ts +4 -0
- package/dist/dto/request/index.js +21 -0
- package/dist/dto/request/index.js.map +1 -0
- package/dist/dto/response/base-response-with-action-dates.dto.d.ts +7 -0
- package/dist/dto/response/base-response-with-action-dates.dto.js +36 -0
- package/dist/dto/response/base-response-with-action-dates.dto.js.map +1 -0
- package/dist/dto/response/base-response.dto.d.ts +4 -0
- package/dist/dto/response/base-response.dto.js +24 -0
- package/dist/dto/response/base-response.dto.js.map +1 -0
- package/dist/dto/response/error-response.dto.d.ts +8 -0
- package/dist/dto/response/error-response.dto.js +38 -0
- package/dist/dto/response/error-response.dto.js.map +1 -0
- package/dist/dto/response/index.d.ts +5 -0
- package/dist/dto/response/index.js +22 -0
- package/dist/dto/response/index.js.map +1 -0
- package/dist/dto/response/kafka-success-response.dto.d.ts +14 -0
- package/dist/dto/response/kafka-success-response.dto.js +66 -0
- package/dist/dto/response/kafka-success-response.dto.js.map +1 -0
- package/dist/dto/response/success-response.dto.d.ts +9 -0
- package/dist/dto/response/success-response.dto.js +44 -0
- package/dist/dto/response/success-response.dto.js.map +1 -0
- package/dist/dto/response/validation.dto.d.ts +25 -0
- package/dist/dto/response/validation.dto.js +84 -0
- package/dist/dto/response/validation.dto.js.map +1 -0
- package/dist/entities/action-dates.entity.d.ts +8 -0
- package/dist/entities/action-dates.entity.js +43 -0
- package/dist/entities/action-dates.entity.js.map +1 -0
- package/dist/entities/base.entity.d.ts +4 -0
- package/dist/entities/base.entity.js +22 -0
- package/dist/entities/base.entity.js.map +1 -0
- package/dist/entities/index.d.ts +2 -0
- package/dist/entities/index.js +19 -0
- package/dist/entities/index.js.map +1 -0
- package/dist/entities/parent.entity.d.ts +8 -0
- package/dist/entities/parent.entity.js +45 -0
- package/dist/entities/parent.entity.js.map +1 -0
- package/dist/enums/environments.enum.d.ts +6 -0
- package/dist/enums/environments.enum.js +11 -0
- package/dist/enums/environments.enum.js.map +1 -0
- package/dist/enums/index.d.ts +2 -0
- package/dist/enums/index.js +19 -0
- package/dist/enums/index.js.map +1 -0
- package/dist/enums/shared-messages.enum.d.ts +25 -0
- package/dist/enums/shared-messages.enum.js +30 -0
- package/dist/enums/shared-messages.enum.js.map +1 -0
- package/dist/filters/http-exception.filter.d.ts +8 -0
- package/dist/filters/http-exception.filter.js +65 -0
- package/dist/filters/http-exception.filter.js.map +1 -0
- package/dist/filters/index.d.ts +1 -0
- package/dist/filters/index.js +18 -0
- package/dist/filters/index.js.map +1 -0
- package/dist/filters/rpc-exception.filter.d.ts +5 -0
- package/dist/filters/rpc-exception.filter.js +21 -0
- package/dist/filters/rpc-exception.filter.js.map +1 -0
- package/dist/helper/get-env.d.ts +1 -0
- package/dist/helper/get-env.helper.d.ts +1 -0
- package/dist/helper/get-env.helper.js +7 -0
- package/dist/helper/get-env.helper.js.map +1 -0
- package/dist/helper/get-env.js +7 -0
- package/dist/helper/get-env.js.map +1 -0
- package/dist/helper/index.d.ts +2 -0
- package/dist/helper/index.js +19 -0
- package/dist/helper/index.js.map +1 -0
- package/dist/helper/message-formatter.helper.d.ts +3 -0
- package/dist/helper/message-formatter.helper.js +27 -0
- package/dist/helper/message-formatter.helper.js.map +1 -0
- package/dist/helper/multi-inheritance.helper.d.ts +1 -0
- package/dist/helper/multi-inheritance.helper.js +13 -0
- package/dist/helper/multi-inheritance.helper.js.map +1 -0
- package/dist/helper/multi-inheritance.util.d.ts +1 -0
- package/dist/helper/multi-inheritance.util.js +13 -0
- package/dist/helper/multi-inheritance.util.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/interface/core-crud-service.option.d.ts +7 -0
- package/dist/interface/core-crud-service.option.js +10 -0
- package/dist/interface/core-crud-service.option.js.map +1 -0
- package/dist/interface/custom-validation-arguments.interface.d.ts +14 -0
- package/dist/interface/custom-validation-arguments.interface.js +3 -0
- package/dist/interface/custom-validation-arguments.interface.js.map +1 -0
- package/dist/interface/index.d.ts +2 -0
- package/dist/interface/index.js +19 -0
- package/dist/interface/index.js.map +1 -0
- package/dist/interface/kafka-success-response.interfase.d.ts +9 -0
- package/dist/interface/kafka-success-response.interfase.js +3 -0
- package/dist/interface/kafka-success-response.interfase.js.map +1 -0
- package/dist/interface/pagination-query.d.ts +13 -0
- package/dist/interface/pagination-query.js +55 -0
- package/dist/interface/pagination-query.js.map +1 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +18 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/kafka/constant/consumer.const.d.ts +12 -0
- package/dist/lib/kafka/constant/consumer.const.js +18 -0
- package/dist/lib/kafka/constant/consumer.const.js.map +1 -0
- package/dist/lib/kafka/constant/kafka.const.d.ts +9 -0
- package/dist/lib/kafka/constant/kafka.const.js +14 -0
- package/dist/lib/kafka/constant/kafka.const.js.map +1 -0
- package/dist/lib/kafka/index.d.ts +4 -0
- package/dist/lib/kafka/index.js +8 -0
- package/dist/lib/kafka/index.js.map +1 -0
- package/dist/lib/kafka/kafka-admin.service.d.ts +7 -0
- package/dist/lib/kafka/kafka-admin.service.js +47 -0
- package/dist/lib/kafka/kafka-admin.service.js.map +1 -0
- package/dist/lib/kafka/kafka-consumer.service.d.ts +9 -0
- package/dist/lib/kafka/kafka-consumer.service.js +50 -0
- package/dist/lib/kafka/kafka-consumer.service.js.map +1 -0
- package/dist/lib/kafka/kafka-instance.const.d.ts +2 -0
- package/dist/lib/kafka/kafka-instance.const.js +12 -0
- package/dist/lib/kafka/kafka-instance.const.js.map +1 -0
- package/dist/lib/kafka/kafka-logger.d.ts +2 -0
- package/dist/lib/kafka/kafka-logger.js +22 -0
- package/dist/lib/kafka/kafka-logger.js.map +1 -0
- package/dist/lib/kafka/kafka-producer.service.d.ts +10 -0
- package/dist/lib/kafka/kafka-producer.service.js +76 -0
- package/dist/lib/kafka/kafka-producer.service.js.map +1 -0
- package/dist/lib/kafka/kafka.interface.d.ts +15 -0
- package/dist/lib/kafka/kafka.interface.js +5 -0
- package/dist/lib/kafka/kafka.interface.js.map +1 -0
- package/dist/lib/kafka/kafka.module.d.ts +10 -0
- package/dist/lib/kafka/kafka.module.js +77 -0
- package/dist/lib/kafka/kafka.module.js.map +1 -0
- package/dist/lib/kafka/kafka.provider.d.ts +7 -0
- package/dist/lib/kafka/kafka.provider.js +12 -0
- package/dist/lib/kafka/kafka.provider.js.map +1 -0
- package/dist/lib/kafka/kafka.service.d.ts +38 -0
- package/dist/lib/kafka/kafka.service.js +124 -0
- package/dist/lib/kafka/kafka.service.js.map +1 -0
- package/dist/services/core-crud.service.d.ts +31 -0
- package/dist/services/core-crud.service.js +318 -0
- package/dist/services/core-crud.service.js.map +1 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +18 -0
- package/dist/services/index.js.map +1 -0
- package/dist/strategy/index.d.ts +1 -0
- package/dist/strategy/index.js +18 -0
- package/dist/strategy/index.js.map +1 -0
- package/dist/strategy/type-orm-naming.strategy.d.ts +4 -0
- package/dist/strategy/type-orm-naming.strategy.js +12 -0
- package/dist/strategy/type-orm-naming.strategy.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/validators/does-exist.validator.d.ts +8 -0
- package/dist/validators/does-exist.validator.js +48 -0
- package/dist/validators/does-exist.validator.js.map +1 -0
- package/dist/validators/env.validator.d.ts +2 -0
- package/dist/validators/env.validator.js +27 -0
- package/dist/validators/env.validator.js.map +1 -0
- package/dist/validators/index.d.ts +4 -0
- package/dist/validators/index.js +21 -0
- package/dist/validators/index.js.map +1 -0
- package/dist/validators/is-unique.validator.d.ts +7 -0
- package/dist/validators/is-unique.validator.js +58 -0
- package/dist/validators/is-unique.validator.js.map +1 -0
- package/dist/validators/validation-options.d.ts +3 -0
- package/dist/validators/validation-options.js +28 -0
- package/dist/validators/validation-options.js.map +1 -0
- package/nest-cli.json +8 -0
- package/package.json +81 -0
- package/tsconfig.build.json +4 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeepPartial,
|
|
3
|
+
DeleteResult,
|
|
4
|
+
FindManyOptions,
|
|
5
|
+
FindOptionsWhere,
|
|
6
|
+
Repository,
|
|
7
|
+
UpdateResult,
|
|
8
|
+
} from 'typeorm';
|
|
9
|
+
import {
|
|
10
|
+
BadRequestException,
|
|
11
|
+
Injectable,
|
|
12
|
+
NotFoundException,
|
|
13
|
+
} from '@nestjs/common';
|
|
14
|
+
import { ParentEntity } from '../entities/parent.entity';
|
|
15
|
+
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
|
|
16
|
+
import { paginate, Paginated } from 'nestjs-paginate';
|
|
17
|
+
import { PaginateConfig } from 'nestjs-paginate/lib/paginate';
|
|
18
|
+
import { ObjectId } from 'typeorm/driver/mongodb/typings';
|
|
19
|
+
import { PaginationQueryCustom } from '../interface/pagination-query';
|
|
20
|
+
import { SharedMessages } from '../enums/shared-messages.enum';
|
|
21
|
+
import { InsertResult } from 'typeorm/query-builder/result/InsertResult';
|
|
22
|
+
import { CoreCrudServiceOption } from '../interface';
|
|
23
|
+
import { UpsertOptions } from 'typeorm/repository/UpsertOptions';
|
|
24
|
+
import { MessageFormatter } from '../helper/message-formatter.helper';
|
|
25
|
+
|
|
26
|
+
@Injectable()
|
|
27
|
+
export abstract class CoreCrudService<
|
|
28
|
+
T extends ParentEntity,
|
|
29
|
+
CreateDto,
|
|
30
|
+
UpdateDto,
|
|
31
|
+
> {
|
|
32
|
+
private readonly relationsPath: string[];
|
|
33
|
+
|
|
34
|
+
protected constructor(protected readonly repository: Repository<T>) {
|
|
35
|
+
const metadata = this.repository.metadata;
|
|
36
|
+
this.relationsPath = metadata.relations.map(
|
|
37
|
+
(relation) => relation.propertyPath,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async create(
|
|
42
|
+
createDto: CreateDto,
|
|
43
|
+
options?: CoreCrudServiceOption<T>,
|
|
44
|
+
): Promise<T> {
|
|
45
|
+
const relationEntityName = Object.keys(createDto).filter(
|
|
46
|
+
(item) => this.relationsPath.includes(item) && createDto[item],
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
relationEntityName.map((item) => {
|
|
50
|
+
createDto[item] = createDto[item].map((id: number) => {
|
|
51
|
+
return { id };
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const entity = this.repository.create(createDto as DeepPartial<T>);
|
|
56
|
+
|
|
57
|
+
if (options?.entityManager) {
|
|
58
|
+
return await options.entityManager.save(entity);
|
|
59
|
+
} else {
|
|
60
|
+
return await this.repository.save(entity);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async findAllWithPagination(
|
|
65
|
+
query: PaginationQueryCustom,
|
|
66
|
+
paginateConfig: PaginateConfig<T>,
|
|
67
|
+
options?: CoreCrudServiceOption<T>,
|
|
68
|
+
): Promise<Paginated<T>> {
|
|
69
|
+
return await paginate<T>(query, this.repository, {
|
|
70
|
+
...paginateConfig,
|
|
71
|
+
relations: options?.relations ? options?.relations : this.relationsPath,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async findAll(
|
|
76
|
+
query: FindManyOptions<T>,
|
|
77
|
+
options?: CoreCrudServiceOption<T>,
|
|
78
|
+
): Promise<T[]> {
|
|
79
|
+
let result: T[] | PromiseLike<T[]>;
|
|
80
|
+
if (!options?.entityManager)
|
|
81
|
+
result = await this.repository.find({
|
|
82
|
+
relations: this.relationsPath,
|
|
83
|
+
...query,
|
|
84
|
+
});
|
|
85
|
+
else
|
|
86
|
+
result = await options.entityManager.find(this.repository.target, {
|
|
87
|
+
relations: this.relationsPath,
|
|
88
|
+
...query,
|
|
89
|
+
});
|
|
90
|
+
if (options?.existsCheck && !result) {
|
|
91
|
+
throw new NotFoundException(SharedMessages.RESOURCE_NOT_FOUND);
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async findOneById(
|
|
97
|
+
id: number,
|
|
98
|
+
options?: CoreCrudServiceOption<T>,
|
|
99
|
+
): Promise<T> {
|
|
100
|
+
let result: T;
|
|
101
|
+
|
|
102
|
+
if (options?.entityManager) {
|
|
103
|
+
result = await options.entityManager.findOne<T>(this.repository.target, {
|
|
104
|
+
where: {
|
|
105
|
+
id,
|
|
106
|
+
} as FindOptionsWhere<T>,
|
|
107
|
+
relations: options?.relations ? options?.relations : this.relationsPath,
|
|
108
|
+
});
|
|
109
|
+
} else {
|
|
110
|
+
result = await this.repository.findOne({
|
|
111
|
+
where: {
|
|
112
|
+
id,
|
|
113
|
+
} as FindOptionsWhere<T>,
|
|
114
|
+
relations: options?.relations ? options?.relations : this.relationsPath,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (options?.existsCheck && !result) {
|
|
118
|
+
throw new NotFoundException(SharedMessages.RESOURCE_NOT_FOUND);
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async findOne(
|
|
124
|
+
query: FindManyOptions<T>,
|
|
125
|
+
options?: CoreCrudServiceOption<T>,
|
|
126
|
+
): Promise<T> {
|
|
127
|
+
const whereQuery = this.validateWhereQuery(query.where);
|
|
128
|
+
if (!whereQuery) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
query.where = whereQuery;
|
|
132
|
+
let result: T;
|
|
133
|
+
|
|
134
|
+
if (options?.entityManager) {
|
|
135
|
+
result = await options?.entityManager.findOne<T>(this.repository.target, {
|
|
136
|
+
relations: this.relationsPath,
|
|
137
|
+
...query,
|
|
138
|
+
});
|
|
139
|
+
} else {
|
|
140
|
+
result = await this.repository.findOne({
|
|
141
|
+
...query,
|
|
142
|
+
relations: query?.relations ? query?.relations : this.relationsPath,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (options?.existsCheck && !result) {
|
|
147
|
+
throw new NotFoundException(SharedMessages.RESOURCE_NOT_FOUND);
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async isExists(
|
|
153
|
+
query: FindManyOptions<T>,
|
|
154
|
+
options?: CoreCrudServiceOption<T>,
|
|
155
|
+
): Promise<boolean> {
|
|
156
|
+
// const whereQuery = this.validateWhereQuery(query.where);
|
|
157
|
+
// if (!whereQuery) {
|
|
158
|
+
// return undefined;
|
|
159
|
+
// }
|
|
160
|
+
// query.where = whereQuery;
|
|
161
|
+
let result: boolean;
|
|
162
|
+
|
|
163
|
+
if (options?.entityManager) {
|
|
164
|
+
result = await options?.entityManager.exists<T>(this.repository.target, {
|
|
165
|
+
relations: query?.relations ? query?.relations : this.relationsPath,
|
|
166
|
+
...query,
|
|
167
|
+
});
|
|
168
|
+
} else {
|
|
169
|
+
result = await this.repository.exists({
|
|
170
|
+
...query,
|
|
171
|
+
relations: query?.relations ? query?.relations : this.relationsPath,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async count(
|
|
179
|
+
query: FindManyOptions<T>,
|
|
180
|
+
options?: CoreCrudServiceOption<T>,
|
|
181
|
+
): Promise<number> {
|
|
182
|
+
const whereQuery = this.validateWhereQuery(query.where);
|
|
183
|
+
if (!whereQuery) {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
query.where = whereQuery;
|
|
187
|
+
let result: number;
|
|
188
|
+
|
|
189
|
+
if (options?.entityManager) {
|
|
190
|
+
result = await options?.entityManager.count<T>(this.repository.target, {
|
|
191
|
+
relations: query?.relations ? query?.relations : this.relationsPath,
|
|
192
|
+
...query,
|
|
193
|
+
});
|
|
194
|
+
} else {
|
|
195
|
+
result = await this.repository.count({
|
|
196
|
+
...query,
|
|
197
|
+
relations: query?.relations ? query?.relations : this.relationsPath,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async upsert(
|
|
205
|
+
upsertDto: QueryDeepPartialEntity<T>,
|
|
206
|
+
upsertOptions: UpsertOptions<T>,
|
|
207
|
+
options?: CoreCrudServiceOption<T>,
|
|
208
|
+
): Promise<InsertResult> {
|
|
209
|
+
try {
|
|
210
|
+
let conflictPaths: string[] = [];
|
|
211
|
+
|
|
212
|
+
let findWhereQuery: FindOptionsWhere<T> = {};
|
|
213
|
+
if (this.isArray(upsertOptions.conflictPaths)) {
|
|
214
|
+
conflictPaths = (upsertOptions.conflictPaths as string[]) ?? [];
|
|
215
|
+
} else {
|
|
216
|
+
conflictPaths = Object.keys(
|
|
217
|
+
upsertOptions.conflictPaths as { [P in keyof T]?: true },
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
conflictPaths.map((path) => {
|
|
221
|
+
findWhereQuery = {
|
|
222
|
+
...findWhereQuery,
|
|
223
|
+
[path]: upsertDto[path],
|
|
224
|
+
};
|
|
225
|
+
});
|
|
226
|
+
if (!options?.entityManager) {
|
|
227
|
+
const existEntity = await this.repository.findOne({
|
|
228
|
+
where: findWhereQuery,
|
|
229
|
+
select: ['id'],
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
return await this.repository.upsert(
|
|
233
|
+
{ id: existEntity.id, ...upsertDto },
|
|
234
|
+
upsertOptions,
|
|
235
|
+
);
|
|
236
|
+
} else {
|
|
237
|
+
const existEntity = await options?.entityManager.findOne<T>(
|
|
238
|
+
this.repository.target,
|
|
239
|
+
{
|
|
240
|
+
where: findWhereQuery,
|
|
241
|
+
select: ['id'],
|
|
242
|
+
},
|
|
243
|
+
);
|
|
244
|
+
return await options?.entityManager.upsert(
|
|
245
|
+
this.repository.target,
|
|
246
|
+
{ id: existEntity.id, ...upsertDto },
|
|
247
|
+
upsertOptions,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
// Delete Cache
|
|
251
|
+
} catch (error) {
|
|
252
|
+
throw new BadRequestException(
|
|
253
|
+
MessageFormatter.replace(SharedMessages.UPSERT_FAILED, error),
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async update(
|
|
259
|
+
id: number,
|
|
260
|
+
updateDto: Partial<UpdateDto> | DeepPartial<T> | QueryDeepPartialEntity<T>,
|
|
261
|
+
options?: CoreCrudServiceOption<T>,
|
|
262
|
+
): Promise<UpdateResult> {
|
|
263
|
+
if (!options?.entityManager) {
|
|
264
|
+
const fetchedItem = await this.repository.findOne({
|
|
265
|
+
where: { id } as FindOptionsWhere<T>,
|
|
266
|
+
relations: this.relationsPath,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
if (!fetchedItem)
|
|
270
|
+
throw new NotFoundException(SharedMessages.RESOURCE_NOT_FOUND);
|
|
271
|
+
|
|
272
|
+
const relationEntityName = Object.keys(updateDto).filter(
|
|
273
|
+
(item) => this.relationsPath.includes(item) && updateDto[item],
|
|
274
|
+
);
|
|
275
|
+
relationEntityName.map((item) => {
|
|
276
|
+
delete fetchedItem[item];
|
|
277
|
+
updateDto[item] = updateDto[item].map((id: number) => {
|
|
278
|
+
return { id };
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
const merged = this.repository.merge(
|
|
282
|
+
fetchedItem,
|
|
283
|
+
updateDto as unknown as DeepPartial<T>,
|
|
284
|
+
);
|
|
285
|
+
await this.repository.save(merged);
|
|
286
|
+
|
|
287
|
+
return {
|
|
288
|
+
generatedMaps: [],
|
|
289
|
+
raw: merged,
|
|
290
|
+
affected: 1,
|
|
291
|
+
} as UpdateResult;
|
|
292
|
+
} else {
|
|
293
|
+
// Transaction context: use the provided EntityManager
|
|
294
|
+
const fetchedItem = await options?.entityManager.findOne<T>(
|
|
295
|
+
this.repository.target,
|
|
296
|
+
{
|
|
297
|
+
where: { id } as FindOptionsWhere<T>,
|
|
298
|
+
relations: this.relationsPath,
|
|
299
|
+
},
|
|
300
|
+
);
|
|
301
|
+
if (!fetchedItem)
|
|
302
|
+
throw new NotFoundException(SharedMessages.RESOURCE_NOT_FOUND);
|
|
303
|
+
|
|
304
|
+
const relationEntityName = Object.keys(updateDto).filter(
|
|
305
|
+
(item) => this.relationsPath.includes(item) && updateDto[item],
|
|
306
|
+
);
|
|
307
|
+
relationEntityName.map((item) => {
|
|
308
|
+
delete fetchedItem[item];
|
|
309
|
+
updateDto[item] = updateDto[item].map((id: number) => {
|
|
310
|
+
return { id };
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const merged = options?.entityManager.merge(
|
|
315
|
+
this.repository.target,
|
|
316
|
+
fetchedItem,
|
|
317
|
+
updateDto as unknown as DeepPartial<T>,
|
|
318
|
+
);
|
|
319
|
+
await options?.entityManager.save(merged);
|
|
320
|
+
// Delete Cache
|
|
321
|
+
return {
|
|
322
|
+
generatedMaps: [],
|
|
323
|
+
raw: merged,
|
|
324
|
+
affected: 1,
|
|
325
|
+
} as UpdateResult;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async softDelete(
|
|
330
|
+
criteria:
|
|
331
|
+
| string
|
|
332
|
+
| string[]
|
|
333
|
+
| number
|
|
334
|
+
| number[]
|
|
335
|
+
| Date
|
|
336
|
+
| Date[]
|
|
337
|
+
| ObjectId
|
|
338
|
+
| ObjectId[]
|
|
339
|
+
| FindOptionsWhere<T>,
|
|
340
|
+
options?: CoreCrudServiceOption<T>,
|
|
341
|
+
): Promise<UpdateResult> {
|
|
342
|
+
if (options?.entityManager) {
|
|
343
|
+
return await options?.entityManager.softDelete(
|
|
344
|
+
this.repository.target,
|
|
345
|
+
criteria,
|
|
346
|
+
);
|
|
347
|
+
} else {
|
|
348
|
+
return await this.repository.softDelete(criteria);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async softDeleteById(
|
|
353
|
+
id: number,
|
|
354
|
+
options?: CoreCrudServiceOption<T>,
|
|
355
|
+
): Promise<UpdateResult> {
|
|
356
|
+
if (options?.entityManager) {
|
|
357
|
+
return await options?.entityManager.softDelete(
|
|
358
|
+
this.repository.target,
|
|
359
|
+
id,
|
|
360
|
+
);
|
|
361
|
+
} else {
|
|
362
|
+
return await this.repository.softDelete(id);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async delete(
|
|
367
|
+
criteria:
|
|
368
|
+
| string
|
|
369
|
+
| string[]
|
|
370
|
+
| number
|
|
371
|
+
| number[]
|
|
372
|
+
| Date
|
|
373
|
+
| Date[]
|
|
374
|
+
| ObjectId
|
|
375
|
+
| ObjectId[]
|
|
376
|
+
| FindOptionsWhere<T>,
|
|
377
|
+
options?: CoreCrudServiceOption<T>,
|
|
378
|
+
): Promise<DeleteResult> {
|
|
379
|
+
if (options?.entityManager) {
|
|
380
|
+
return await options?.entityManager.delete(
|
|
381
|
+
this.repository.target,
|
|
382
|
+
criteria,
|
|
383
|
+
);
|
|
384
|
+
} else {
|
|
385
|
+
return await this.repository.delete(criteria);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
async deleteById(
|
|
390
|
+
id: number,
|
|
391
|
+
options?: CoreCrudServiceOption<T>,
|
|
392
|
+
): Promise<DeleteResult> {
|
|
393
|
+
if (options?.entityManager) {
|
|
394
|
+
return await options?.entityManager.delete(this.repository.target, id);
|
|
395
|
+
} else {
|
|
396
|
+
return await this.repository.delete(id);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
private validateWhereQuery(
|
|
401
|
+
query: FindOptionsWhere<T> | FindOptionsWhere<T>[],
|
|
402
|
+
) {
|
|
403
|
+
if (this.isArray(query)) {
|
|
404
|
+
const newArrayQuery: FindOptionsWhere<T>[] = [];
|
|
405
|
+
|
|
406
|
+
query = query as FindOptionsWhere<T>[];
|
|
407
|
+
if (query.length !== 0) {
|
|
408
|
+
query.map((key, index) => {
|
|
409
|
+
if (this.isArray(query[index])) {
|
|
410
|
+
return query[index].map((item) => this.validateWhereQuery(item));
|
|
411
|
+
}
|
|
412
|
+
if (this.isObject(query[index])) {
|
|
413
|
+
this.validateWhereQuery(query[index]);
|
|
414
|
+
}
|
|
415
|
+
if (query[index] !== undefined) {
|
|
416
|
+
newArrayQuery.push({
|
|
417
|
+
[index]: query[index],
|
|
418
|
+
} as FindOptionsWhere<T>);
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
if (Object.keys(newArrayQuery).length === 0) return undefined;
|
|
423
|
+
return newArrayQuery;
|
|
424
|
+
} else {
|
|
425
|
+
let newQuery: FindOptionsWhere<T> = {};
|
|
426
|
+
|
|
427
|
+
const queryKeys = Object.keys(query);
|
|
428
|
+
|
|
429
|
+
if (queryKeys.length !== 0) {
|
|
430
|
+
queryKeys.map((key) => {
|
|
431
|
+
if (this.isArray(query[key])) {
|
|
432
|
+
return query[key].map((item) => this.validateWhereQuery(item));
|
|
433
|
+
}
|
|
434
|
+
if (this.isObject(query[key])) {
|
|
435
|
+
this.validateWhereQuery(query[key]);
|
|
436
|
+
}
|
|
437
|
+
if (query[key] !== undefined) {
|
|
438
|
+
newQuery = {
|
|
439
|
+
...newQuery,
|
|
440
|
+
[key]: query[key],
|
|
441
|
+
} as FindOptionsWhere<T>;
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
if (Object.keys(newQuery).length === 0) return undefined;
|
|
446
|
+
return newQuery;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private isObject(value: any): boolean {
|
|
451
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private isArray(value: any): boolean {
|
|
455
|
+
return value !== null && typeof value === 'object' && Array.isArray(value);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './core-crud.service';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './type-orm-naming.strategy';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DefaultNamingStrategy, NamingStrategyInterface } from 'typeorm';
|
|
2
|
+
import { snakeCase } from 'typeorm/util/StringUtils';
|
|
3
|
+
|
|
4
|
+
export class SnakeNamingStrategy
|
|
5
|
+
extends DefaultNamingStrategy
|
|
6
|
+
implements NamingStrategyInterface
|
|
7
|
+
{
|
|
8
|
+
columnName(
|
|
9
|
+
propertyName: string,
|
|
10
|
+
customName: string,
|
|
11
|
+
embeddedPrefixes: string[],
|
|
12
|
+
): string {
|
|
13
|
+
return snakeCase(
|
|
14
|
+
embeddedPrefixes.concat(customName || propertyName).join('_'),
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ValidatorConstraint,
|
|
3
|
+
ValidatorConstraintInterface,
|
|
4
|
+
} from 'class-validator';
|
|
5
|
+
import { DataSource } from 'typeorm';
|
|
6
|
+
import { ValidationArguments } from 'class-validator/types/validation/ValidationArguments';
|
|
7
|
+
import { Injectable } from '@nestjs/common';
|
|
8
|
+
import { InjectDataSource } from '@nestjs/typeorm';
|
|
9
|
+
|
|
10
|
+
type ValidationEntity =
|
|
11
|
+
| {
|
|
12
|
+
id?: number | string;
|
|
13
|
+
}
|
|
14
|
+
| undefined;
|
|
15
|
+
|
|
16
|
+
@Injectable()
|
|
17
|
+
@ValidatorConstraint({ name: 'DoesExist', async: true })
|
|
18
|
+
export class DoesExist implements ValidatorConstraintInterface {
|
|
19
|
+
constructor(
|
|
20
|
+
@InjectDataSource()
|
|
21
|
+
private dataSource: DataSource,
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
async validate(value: string, validationArguments: ValidationArguments) {
|
|
25
|
+
const repository = validationArguments.constraints[0] as string;
|
|
26
|
+
const pathToProperty = validationArguments.constraints[1];
|
|
27
|
+
const targetRepository = this.dataSource.getRepository(repository);
|
|
28
|
+
const entity = (await targetRepository.findOne({
|
|
29
|
+
where: {
|
|
30
|
+
[pathToProperty ? pathToProperty : validationArguments.property]: value
|
|
31
|
+
? value
|
|
32
|
+
: value?.[pathToProperty],
|
|
33
|
+
},
|
|
34
|
+
})) as ValidationEntity;
|
|
35
|
+
|
|
36
|
+
if (entity?.id === value) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { plainToClass } from 'class-transformer';
|
|
2
|
+
import { validateSync } from 'class-validator';
|
|
3
|
+
|
|
4
|
+
export function envValidator(configuration: Record<string, unknown>, EnvironmentSchema: any) {
|
|
5
|
+
const finalConfig: string = plainToClass(EnvironmentSchema, configuration, {
|
|
6
|
+
enableImplicitConversion: true,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const errors = validateSync(finalConfig, { skipMissingProperties: false });
|
|
10
|
+
if (errors.length > 0) {
|
|
11
|
+
throw new Error(errors.toString());
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return finalConfig;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function testEnvValidator(configuration: Record<string, unknown>, EnvironmentTestSchema: any) {
|
|
18
|
+
const finalConfig: string = plainToClass(EnvironmentTestSchema, configuration, {
|
|
19
|
+
enableImplicitConversion: true,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const errors = validateSync(finalConfig, { skipMissingProperties: false });
|
|
23
|
+
if (errors.length > 0) {
|
|
24
|
+
throw new Error(errors.toString());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return finalConfig;
|
|
28
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IsNotIn,
|
|
3
|
+
ValidatorConstraint,
|
|
4
|
+
ValidatorConstraintInterface,
|
|
5
|
+
} from 'class-validator';
|
|
6
|
+
import { DataSource, FindOptionsWhere } from 'typeorm';
|
|
7
|
+
import { ValidationArguments } from 'class-validator/types/validation/ValidationArguments';
|
|
8
|
+
import { Injectable } from '@nestjs/common';
|
|
9
|
+
import { InjectDataSource } from '@nestjs/typeorm';
|
|
10
|
+
import {
|
|
11
|
+
CustomConstraintsInterface,
|
|
12
|
+
CustomValidationArguments,
|
|
13
|
+
CustomValidatorConstraintInterface,
|
|
14
|
+
} from '../interface/custom-validation-arguments.interface';
|
|
15
|
+
|
|
16
|
+
type ValidationEntity =
|
|
17
|
+
| {
|
|
18
|
+
id?: number | string;
|
|
19
|
+
}
|
|
20
|
+
| undefined;
|
|
21
|
+
|
|
22
|
+
@Injectable()
|
|
23
|
+
@ValidatorConstraint({ name: 'IsUnique', async: true })
|
|
24
|
+
export class IsUnique implements CustomValidatorConstraintInterface {
|
|
25
|
+
constructor(
|
|
26
|
+
@InjectDataSource()
|
|
27
|
+
private dataSource: DataSource,
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
async validate(
|
|
31
|
+
value: string,
|
|
32
|
+
validationArguments: CustomValidationArguments,
|
|
33
|
+
) {
|
|
34
|
+
const repository = validationArguments.constraints[0]?.repository;
|
|
35
|
+
const targetRepository = this.dataSource.getRepository(repository);
|
|
36
|
+
|
|
37
|
+
const pathToProperty =
|
|
38
|
+
validationArguments.constraints[0]?.pathToProperty ??
|
|
39
|
+
validationArguments.property;
|
|
40
|
+
const whereQuery =
|
|
41
|
+
(validationArguments?.constraints[0]?.whereQuery as FindOptionsWhere<
|
|
42
|
+
typeof targetRepository
|
|
43
|
+
>) ?? [];
|
|
44
|
+
|
|
45
|
+
let combineQuery = {};
|
|
46
|
+
|
|
47
|
+
validationArguments?.constraints[0]?.stringPropertyQuery?.map(
|
|
48
|
+
(condition: string) => {
|
|
49
|
+
combineQuery = {
|
|
50
|
+
...combineQuery,
|
|
51
|
+
[condition]: validationArguments.object?.[condition],
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
const entity = (await targetRepository.findOne({
|
|
56
|
+
where: {
|
|
57
|
+
[pathToProperty ? pathToProperty : validationArguments.property]: value
|
|
58
|
+
? value
|
|
59
|
+
: value?.[pathToProperty],
|
|
60
|
+
...whereQuery,
|
|
61
|
+
},
|
|
62
|
+
})) as ValidationEntity;
|
|
63
|
+
|
|
64
|
+
if (entity?.id) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HttpException,
|
|
3
|
+
HttpStatus,
|
|
4
|
+
ValidationError,
|
|
5
|
+
ValidationPipeOptions,
|
|
6
|
+
} from '@nestjs/common';
|
|
7
|
+
import { MessageFormatter } from '../helper/message-formatter.helper';
|
|
8
|
+
|
|
9
|
+
function mapValidationErrors(errors: ValidationError[]): any {
|
|
10
|
+
return errors.reduce((result, error) => {
|
|
11
|
+
const constraints = error.constraints
|
|
12
|
+
? Object.values(error.constraints)
|
|
13
|
+
.map((message) => MessageFormatter.replace(message, error.property))
|
|
14
|
+
.join(', ')
|
|
15
|
+
: null;
|
|
16
|
+
|
|
17
|
+
result[error.property] =
|
|
18
|
+
constraints || mapValidationErrors(error.children || []);
|
|
19
|
+
return result;
|
|
20
|
+
}, {});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const validationOptions: ValidationPipeOptions = {
|
|
24
|
+
transform: true,
|
|
25
|
+
whitelist: true,
|
|
26
|
+
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
|
|
27
|
+
exceptionFactory: (errors: ValidationError[]) =>
|
|
28
|
+
new HttpException(
|
|
29
|
+
{
|
|
30
|
+
status: HttpStatus.UNPROCESSABLE_ENTITY,
|
|
31
|
+
message: `Validation failed: ${errors.length} errors found`,
|
|
32
|
+
details: mapValidationErrors(errors),
|
|
33
|
+
},
|
|
34
|
+
HttpStatus.UNPROCESSABLE_ENTITY,
|
|
35
|
+
),
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export default validationOptions;
|