@stryke/prisma-trpc-generator
Version:
A fork of the prisma-trpc-generator code to work in ESM with Prisma v6.
1 lines • 5.06 kB
Source Map (JSON)
{"version":3,"file":"include-helpers.mjs","names":[],"sources":["../../src/zod/include-helpers.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { DMMF } from \"@prisma/generator-helper\";\nimport type { SchemaArgInputTypes } from \"../types\";\nimport {\n checkIsModelRelationField,\n checkModelHasManyModelRelation,\n checkModelHasModelRelation\n} from \"./model-helpers\";\n\nexport function addMissingInputObjectTypesForInclude(\n inputObjectTypes: DMMF.InputType[],\n models: DMMF.Model[],\n isGenerateSelect: boolean\n) {\n // generate input object types necessary to support ModelInclude with relation support\n const generatedIncludeInputObjectTypes = generateModelIncludeInputObjectTypes(\n models,\n isGenerateSelect\n );\n\n for (const includeInputObjectType of generatedIncludeInputObjectTypes) {\n inputObjectTypes.push(includeInputObjectType);\n }\n}\n\nfunction generateModelIncludeInputObjectTypes(\n models: DMMF.Model[],\n isGenerateSelect: boolean\n) {\n const modelIncludeInputObjectTypes: DMMF.InputType[] = [];\n for (const model of models) {\n const { name: modelName, fields: modelFields } = model;\n const fields: DMMF.SchemaArg[] = [];\n\n for (const modelField of modelFields) {\n const { name: modelFieldName, isList, type } = modelField;\n\n const isRelationField = checkIsModelRelationField(modelField);\n\n if (isRelationField) {\n const field: DMMF.SchemaArg = {\n name: modelFieldName,\n isRequired: false,\n isNullable: false,\n inputTypes: [\n { isList: false, type: \"Boolean\", location: \"scalar\" },\n {\n isList: false,\n type: isList ? `${type}FindManyArgs` : `${type}DefaultArgs`,\n location: \"inputObjectTypes\",\n namespace: \"prisma\"\n }\n ]\n };\n fields.push(field);\n }\n }\n\n /**\n * include is not generated for models that do not have a relation with any other models continue onto the next model\n */\n const hasRelationToAnotherModel = checkModelHasModelRelation(model);\n if (!hasRelationToAnotherModel) {\n continue;\n }\n\n const hasManyRelationToAnotherModel = checkModelHasManyModelRelation(model);\n\n const shouldAddCountField = hasManyRelationToAnotherModel;\n if (shouldAddCountField) {\n const inputTypes: SchemaArgInputTypes = [\n { isList: false, type: \"Boolean\", location: \"scalar\" }\n ];\n if (isGenerateSelect) {\n inputTypes.push({\n isList: false,\n type: `${modelName}CountOutputTypeDefaultArgs`,\n location: \"inputObjectTypes\",\n namespace: \"prisma\"\n });\n }\n const _countField: DMMF.SchemaArg = {\n name: \"_count\",\n isRequired: false,\n isNullable: false,\n inputTypes\n };\n fields.push(_countField);\n }\n\n const modelIncludeInputObjectType: DMMF.InputType = {\n name: `${modelName}Include`,\n constraints: {\n maxNumFields: null,\n minNumFields: null\n },\n fields\n };\n modelIncludeInputObjectTypes.push(modelIncludeInputObjectType);\n }\n return modelIncludeInputObjectTypes;\n}\n"],"mappings":";;;AA0BA,SAAgB,qCACd,kBACA,QACA,kBACA;CAEA,MAAM,mCAAmC,qCACvC,QACA,iBACD;AAED,MAAK,MAAM,0BAA0B,iCACnC,kBAAiB,KAAK,uBAAuB;;AAIjD,SAAS,qCACP,QACA,kBACA;CACA,MAAM,+BAAiD,EAAE;AACzD,MAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,EAAE,MAAM,WAAW,QAAQ,gBAAgB;EACjD,MAAM,SAA2B,EAAE;AAEnC,OAAK,MAAM,cAAc,aAAa;GACpC,MAAM,EAAE,MAAM,gBAAgB,QAAQ,SAAS;AAI/C,OAFwB,0BAA0B,WAE/B,EAAE;IACnB,MAAM,QAAwB;KAC5B,MAAM;KACN,YAAY;KACZ,YAAY;KACZ,YAAY,CACV;MAAE,QAAQ;MAAO,MAAM;MAAW,UAAU;MAAU,EACtD;MACE,QAAQ;MACR,MAAM,SAAS,GAAG,KAAK,gBAAgB,GAAG,KAAK;MAC/C,UAAU;MACV,WAAW;MACZ,CACF;KACF;AACD,WAAO,KAAK,MAAM;;;AAQtB,MAAI,CAD8B,2BAA2B,MAC/B,CAC5B;AAMF,MAHsC,+BAA+B,MAG9C,EAAE;GACvB,MAAM,aAAkC,CACtC;IAAE,QAAQ;IAAO,MAAM;IAAW,UAAU;IAAU,CACvD;AACD,OAAI,iBACF,YAAW,KAAK;IACd,QAAQ;IACR,MAAM,GAAG,UAAU;IACnB,UAAU;IACV,WAAW;IACZ,CAAC;GAEJ,MAAM,cAA8B;IAClC,MAAM;IACN,YAAY;IACZ,YAAY;IACZ;IACD;AACD,UAAO,KAAK,YAAY;;EAG1B,MAAM,8BAA8C;GAClD,MAAM,GAAG,UAAU;GACnB,aAAa;IACX,cAAc;IACd,cAAc;IACf;GACD;GACD;AACD,+BAA6B,KAAK,4BAA4B;;AAEhE,QAAO"}