UNPKG

prisma-zod-generator

Version:

Prisma 2+ generator to emit Zod schemas from your Prisma schema

108 lines 4.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addMissingInputObjectTypesForInclude = addMissingInputObjectTypesForInclude; exports.shouldGenerateIncludeForModel = shouldGenerateIncludeForModel; const transformer_1 = __importDefault(require("../transformer")); const model_helpers_1 = require("./model-helpers"); function addMissingInputObjectTypesForInclude(inputObjectTypes, models, isGenerateSelect) { // In minimal mode, do not generate Include types at all const cfg = transformer_1.default.getGeneratorConfig(); if ((cfg === null || cfg === void 0 ? void 0 : cfg.mode) === 'minimal') { return; } // Filter models to only include enabled ones const enabledModels = (0, model_helpers_1.getEnabledModels)(models); // generate input object types necessary to support ModelInclude with relation support const generatedIncludeInputObjectTypes = generateModelIncludeInputObjectTypes(enabledModels, isGenerateSelect); for (const includeInputObjectType of generatedIncludeInputObjectTypes) { inputObjectTypes.push(includeInputObjectType); } } function generateModelIncludeInputObjectTypes(models, isGenerateSelect) { const modelIncludeInputObjectTypes = []; for (const model of models) { const { name: modelName } = model; // Skip if model doesn't support include/select operations if (!shouldGenerateIncludeForModel(modelName)) { continue; } const fields = []; // Only include enabled relation fields const enabledRelationFields = (0, model_helpers_1.getEnabledRelationFields)(model); for (const modelField of enabledRelationFields) { const { name: modelFieldName, isList, type } = modelField; // Check if the target model has findMany enabled for list relations const targetArgsType = isList && (0, model_helpers_1.isOperationEnabledForModel)(type, 'findMany') ? `${type}FindManyArgs` : `${type}Args`; const field = { name: modelFieldName, isRequired: false, isNullable: false, inputTypes: [ { isList: false, type: 'Boolean', location: 'scalar' }, { isList: false, type: targetArgsType, location: 'inputObjectTypes', namespace: 'prisma', }, ], }; fields.push(field); } /** * include is not generated for models that do not have enabled relations with other models * -> continue onto the next model */ const hasEnabledRelationToAnotherModel = (0, model_helpers_1.checkModelHasEnabledModelRelation)(model); if (!hasEnabledRelationToAnotherModel) { continue; } const hasEnabledManyRelationToAnotherModel = (0, model_helpers_1.checkModelHasEnabledManyModelRelation)(model); const shouldAddCountField = hasEnabledManyRelationToAnotherModel; if (shouldAddCountField) { const inputTypes = [ { isList: false, type: 'Boolean', location: 'scalar' }, ]; if (isGenerateSelect) { inputTypes.push({ isList: false, type: `${modelName.charAt(0).toUpperCase() + modelName.slice(1)}CountOutputTypeArgs`, location: 'inputObjectTypes', namespace: 'prisma', }); } const _countField = { name: '_count', isRequired: false, isNullable: false, inputTypes, }; fields.push(_countField); } const modelIncludeInputObjectType = { name: `${modelName}Include`, constraints: { maxNumFields: null, minNumFields: null, }, fields, }; modelIncludeInputObjectTypes.push(modelIncludeInputObjectType); } return modelIncludeInputObjectTypes; } /** * Check if include schema should be generated for a model */ function shouldGenerateIncludeForModel(modelName) { return (transformer_1.default.isModelEnabled(modelName) && ((0, model_helpers_1.isOperationEnabledForModel)(modelName, 'findUnique') || (0, model_helpers_1.isOperationEnabledForModel)(modelName, 'findFirst') || (0, model_helpers_1.isOperationEnabledForModel)(modelName, 'findMany'))); } //# sourceMappingURL=include-helpers.js.map