UNPKG

nestjs-prisma-base

Version:

A reusable NestJS module for Prisma ORM with base classes for controller, service and DTOs

130 lines 6.28 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseController = void 0; const common_1 = require("@nestjs/common"); const swagger_1 = require("@nestjs/swagger"); const endpoint_decorator_1 = require("../decorators/endpoint.decorator"); class BaseController { constructor(service) { this.service = service; this.applySwaggerExclusions(); } applySwaggerExclusions() { const proto = this.constructor.prototype; const findAllDesc = Object.getOwnPropertyDescriptor(proto, 'findAll') || { value: proto.findAll }; const findOneDesc = Object.getOwnPropertyDescriptor(proto, 'findOne') || { value: proto.findOne }; const createDesc = Object.getOwnPropertyDescriptor(proto, 'create') || { value: proto.create }; const updateDesc = Object.getOwnPropertyDescriptor(proto, 'update') || { value: proto.update }; const removeDesc = Object.getOwnPropertyDescriptor(proto, 'remove') || { value: proto.remove }; if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.FIND_ALL)) { (0, swagger_1.ApiExcludeEndpoint)()(proto, 'findAll', findAllDesc); } if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.FIND_ONE)) { (0, swagger_1.ApiExcludeEndpoint)()(proto, 'findOne', findOneDesc); } if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.CREATE)) { (0, swagger_1.ApiExcludeEndpoint)()(proto, 'create', createDesc); } if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.UPDATE)) { (0, swagger_1.ApiExcludeEndpoint)()(proto, 'update', updateDesc); } if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.REMOVE)) { (0, swagger_1.ApiExcludeEndpoint)()(proto, 'remove', removeDesc); } } isEndpointEnabled(endpointName) { if (Reflect.getMetadata(endpoint_decorator_1.ENDPOINT_DISABLED_KEY, this, endpointName)) { return false; } if (Reflect.getMetadata(endpoint_decorator_1.ENDPOINT_ENABLED_KEY, this, endpointName)) { return true; } const enabledEndpoints = Reflect.getMetadata(endpoint_decorator_1.ENABLED_ENDPOINTS_KEY, this.constructor) || []; if (enabledEndpoints.includes(endpointName) || enabledEndpoints.includes('*')) { const disabledEndpoints = Reflect.getMetadata(endpoint_decorator_1.DISABLED_ENDPOINTS_KEY, this.constructor) || []; return !disabledEndpoints.includes(endpointName); } return false; } findAll(page, limit) { if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.FIND_ALL)) { throw new common_1.NotFoundException('Endpoint not available'); } return this.service.findAll(page ? parseInt(page, 10) : 1, limit ? parseInt(limit, 10) : 10); } findOne(id) { if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.FIND_ONE)) { throw new common_1.NotFoundException('Endpoint not available'); } return this.service.findOne(id); } create(createDto) { if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.CREATE)) { throw new common_1.NotFoundException('Endpoint not available'); } return this.service.create(createDto); } update(id, updateDto) { if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.UPDATE)) { throw new common_1.NotFoundException('Endpoint not available'); } return this.service.update(id, updateDto); } remove(id) { if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.REMOVE)) { throw new common_1.NotFoundException('Endpoint not available'); } return this.service.remove(id); } } exports.BaseController = BaseController; __decorate([ (0, common_1.Get)(), __param(0, (0, common_1.Query)('page')), __param(1, (0, common_1.Query)('limit')), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String]), __metadata("design:returntype", void 0) ], BaseController.prototype, "findAll", null); __decorate([ (0, common_1.Get)(':id'), __param(0, (0, common_1.Param)('id')), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], BaseController.prototype, "findOne", null); __decorate([ (0, common_1.Post)(), __param(0, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], BaseController.prototype, "create", null); __decorate([ (0, common_1.Patch)(':id'), __param(0, (0, common_1.Param)('id')), __param(1, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Object]), __metadata("design:returntype", void 0) ], BaseController.prototype, "update", null); __decorate([ (0, common_1.Delete)(':id'), __param(0, (0, common_1.Param)('id')), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], BaseController.prototype, "remove", null); //# sourceMappingURL=base.controller.js.map