UNPKG

@eleven-am/nestjs-graphql-crud

Version:

nestjs-graphql-crud is a library that aims to reduce the boilerplate code needed to create a GraphQL CRUD API.

176 lines 7.42 kB
"use strict"; /** * @module providers/prisma/prismaDataProvider * @description Prisma implementation of the DataProvider interface */ 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrismaDataProvider = PrismaDataProvider; const common_1 = require("@nestjs/common"); const authorizer_1 = require("@eleven-am/authorizer"); const prisma_1 = require("@casl/prisma"); const core_1 = require("@nestjs/core"); const decorators_1 = require("../../decorators"); function PrismaDataProvider(Service) { /** * Prisma implementation of the DataProvider interface * Handles CRUD operations using Prisma */ let PrismaDataProvider = class PrismaDataProvider { constructor(moduleRef) { this.moduleRef = moduleRef; } onModuleInit() { this.prisma = this.moduleRef.get(Service, { strict: false }); } /** * Find a single entity by criteria */ async findOne(modelName, ability, where, select) { return this.prisma[modelName].findFirst(this.buildArgs(ability, authorizer_1.Action.Read, modelName, where, select)); } /** * Find multiple entities matching criteria */ async findMany(modelName, ability, args, select) { const { pagination, where } = args; return this.prisma[modelName].findMany({ take: pagination?.take, skip: pagination?.skip, where: this.buildWhereArgs(ability, authorizer_1.Action.Read, modelName, where || {}).where, select, }); } /** * Create a new entity */ async create(modelName, data, select) { return this.prisma[modelName].create({ data, select }); } /** * Update a single entity by ID */ async update(modelName, ability, data, whereId, select) { return this.prisma[modelName].update(this.buildUpdateArgs(ability, authorizer_1.Action.Update, modelName, data, whereId, select)); } /** * Update multiple entities matching criteria */ async updateMany(modelName, ability, data, where, select) { const entitiesToUpdate = await this.prisma[modelName].findMany(this.buildArgs(ability, authorizer_1.Action.Update, modelName, where, select)); await this.prisma[modelName].updateMany(this.buildUpdateArgs(ability, authorizer_1.Action.Update, modelName, data, where, {})); return entitiesToUpdate; } /** * Delete a single entity by ID */ async delete(modelName, ability, whereId, select) { return this.prisma[modelName].delete(this.buildArgs(ability, authorizer_1.Action.Delete, modelName, whereId, select)); } /** * Delete multiple entities matching criteria */ async deleteMany(modelName, ability, where, select) { const entitiesToDelete = await this.prisma[modelName].findMany(this.buildArgs(ability, authorizer_1.Action.Delete, modelName, where, select)); await this.prisma[modelName].deleteMany(this.buildArgs(ability, authorizer_1.Action.Delete, modelName, where, {})); return entitiesToDelete; } /** * Find multiple entities without authorization */ findManyWithoutAbility(modelName, { where }, select) { return this.prisma[modelName].findMany({ where, select, }); } /** * Build query arguments with authorization * * @private * @param {AppAbilityType} ability - The user's ability for authorization * @param {Action} action - The action being performed * @param {string} modelName - The name of the model * @param {any} where - Filter criteria * @param {any} select - Fields to select * @returns {any} The built query arguments */ buildArgs(ability, action, modelName, where, select) { return { where: this.buildWhereArgs(ability, action, modelName, where).where, select, }; } /** * Build update query arguments with authorization * * @private * @param {AppAbilityType} ability - The user's ability for authorization * @param {Action} action - The action being performed * @param {string} modelName - The name of the model * @param {any} data - The data to update * @param {any} where - Filter criteria * @param {any} select - Fields to select * @returns {any} The built update query arguments */ buildUpdateArgs(ability, action, modelName, data, where, select) { return { where: this.buildWhereArgs(ability, action, modelName, where).where, select, data, }; } /** * Build where clause with authorization * * @private * @param {AppAbilityType} ability - The user's ability for authorization * @param {Action} action - The action being performed * @param {string} modelName - The name of the model * @param {any} where - Filter criteria * @returns {{ where: object }} The built where clause */ buildWhereArgs(ability, action, modelName, where) { if (!ability) { if (typeof where === 'string') { return { where: { id: where, } }; } return { where }; } if (typeof where === 'string') { return { where: { id: where, AND: [(0, prisma_1.accessibleBy)(ability, action)[(0, decorators_1.firstLetterUppercase)(modelName)]] } }; } return { where: { AND: [ (0, prisma_1.accessibleBy)(ability, action)[(0, decorators_1.firstLetterUppercase)(modelName)], where, ] } }; } }; PrismaDataProvider = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [core_1.ModuleRef]) ], PrismaDataProvider); return PrismaDataProvider; } //# sourceMappingURL=prismaDataProvider.js.map