UNPKG

nestjs-prisma-base

Version:

A comprehensive NestJS package providing base classes, utilities, and decorators for building CRUD APIs with Prisma ORM integration, featuring pagination, search, filtering, relation loading, configurable DTOs, and modular composition capabilities.

179 lines 8.69 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 relation_validator_1 = require("./relation-validator"); 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; } parseSearchOptions(query) { const options = {}; if (query.search && typeof query.search === 'string') { options.search = query.search; } if (query.searchFields && typeof query.searchFields === 'string') { options.searchFields = query.searchFields .split(',') .map((field) => field.trim()) .filter(Boolean); } if (query.sortBy && typeof query.sortBy === 'string') { const sortOrder = query.sortOrder === 'desc' ? 'desc' : 'asc'; options.orderBy = { [query.sortBy]: sortOrder }; } if (query.include && typeof query.include === 'string') { options.requestedIncludes = relation_validator_1.RelationValidator.parseIncludeString(query.include); } const excludeParams = ['page', 'limit', 'search', 'searchFields', 'sortBy', 'sortOrder', 'include']; const filters = {}; Object.entries(query).forEach(([key, value]) => { if (!excludeParams.includes(key) && value !== undefined && value !== null && value !== '') { filters[key] = value; } }); if (Object.keys(filters).length > 0) { options.filters = filters; } return options; } findAll(page, limit, query) { if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.FIND_ALL)) { throw new common_1.NotFoundException('Endpoint not available'); } const searchOptions = this.parseSearchOptions(query || {}); if (searchOptions.requestedIncludes && searchOptions.requestedIncludes.length > 0) { return this.service.findAllAdvanced(page ? parseInt(page, 10) : 1, limit ? parseInt(limit, 10) : undefined, searchOptions); } return this.service.findAll(page ? parseInt(page, 10) : 1, limit ? parseInt(limit, 10) : undefined, searchOptions); } findOne(id, query) { if (!this.isEndpointEnabled(endpoint_decorator_1.EndpointType.FIND_ONE)) { throw new common_1.NotFoundException('Endpoint not available'); } if (query?.include && typeof query.include === 'string') { const searchOptions = { requestedIncludes: relation_validator_1.RelationValidator.parseIncludeString(query.include), filters: { id }, }; return this.service.findAllAdvanced(1, 1, searchOptions).then((result) => { if (result.data.length === 0) { throw new common_1.NotFoundException(`${this.service['modelName']} with ID "${id}" not found`); } return result.data[0]; }); } 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')), __param(2, (0, common_1.Query)()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String, Object]), __metadata("design:returntype", Promise) ], BaseController.prototype, "findAll", null); __decorate([ (0, common_1.Get)(':id'), __param(0, (0, common_1.Param)('id')), __param(1, (0, common_1.Query)()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Object]), __metadata("design:returntype", Promise) ], 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", Promise) ], 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", Promise) ], 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", Promise) ], BaseController.prototype, "remove", null); //# sourceMappingURL=base.controller.js.map