@stryke/prisma-trpc-generator
Version:
A fork of the prisma-trpc-generator code to work in ESM with Prisma v6.
1 lines • 8.47 kB
Source Map (JSON)
{"version":3,"file":"select-helpers.mjs","names":[],"sources":["../../src/zod/select-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 { ArrayValues } from \"@stryke/types/array\";\nimport type { SchemaArgInputTypes, Writeable } from \"../types\";\nimport {\n checkIsModelRelationField,\n checkModelHasManyModelRelation\n} from \"./model-helpers\";\n\nexport function addMissingInputObjectTypesForSelect(\n inputObjectTypes: DMMF.InputType[],\n outputObjectTypes: DMMF.OutputType[],\n models: DMMF.Model[]\n) {\n // generate input object types necessary to support ModelSelect._count\n const modelCountOutputTypes = getModelCountOutputTypes(outputObjectTypes);\n const modelCountOutputTypeSelectInputObjectTypes =\n generateModelCountOutputTypeSelectInputObjectTypes(modelCountOutputTypes);\n const modelCountOutputTypeArgsInputObjectTypes =\n generateModelCountOutputTypeArgsInputObjectTypes(modelCountOutputTypes);\n\n const modelSelectInputObjectTypes =\n generateModelSelectInputObjectTypes(models);\n\n const generatedInputObjectTypes = [\n modelCountOutputTypeSelectInputObjectTypes,\n modelCountOutputTypeArgsInputObjectTypes,\n modelSelectInputObjectTypes\n ].flat();\n\n for (const inputObjectType of generatedInputObjectTypes) {\n inputObjectTypes.push(inputObjectType);\n }\n}\n\nfunction getModelCountOutputTypes(outputObjectTypes: DMMF.OutputType[]) {\n return outputObjectTypes.filter(({ name }) =>\n name.includes(\"CountOutputType\")\n );\n}\n\nfunction generateModelCountOutputTypeSelectInputObjectTypes(\n modelCountOutputTypes: DMMF.OutputType[]\n) {\n const modelCountOutputTypeSelectInputObjectTypes: DMMF.InputType[] = [];\n for (const modelCountOutputType of modelCountOutputTypes) {\n const {\n name: modelCountOutputTypeName,\n fields: modelCountOutputTypeFields\n } = modelCountOutputType;\n const modelCountOutputTypeSelectInputObjectType: DMMF.InputType = {\n name: `${modelCountOutputTypeName}Select`,\n constraints: {\n maxNumFields: null,\n minNumFields: null\n },\n fields: modelCountOutputTypeFields.map(({ name }) => ({\n name,\n isRequired: false,\n isNullable: false,\n inputTypes: [\n {\n isList: false,\n type: `Boolean`,\n location: \"scalar\"\n }\n ]\n }))\n };\n modelCountOutputTypeSelectInputObjectTypes.push(\n modelCountOutputTypeSelectInputObjectType\n );\n }\n\n return modelCountOutputTypeSelectInputObjectTypes;\n}\n\nfunction generateModelCountOutputTypeArgsInputObjectTypes(\n modelCountOutputTypes: DMMF.OutputType[]\n) {\n const modelCountOutputTypeArgsInputObjectTypes: DMMF.InputType[] = [];\n for (const modelCountOutputType of modelCountOutputTypes) {\n const { name: modelCountOutputTypeName } = modelCountOutputType;\n const modelCountOutputTypeArgsInputObjectType: DMMF.InputType = {\n name: `${modelCountOutputTypeName}DefaultArgs`,\n constraints: {\n maxNumFields: null,\n minNumFields: null\n },\n fields: [\n {\n name: \"select\",\n isRequired: false,\n isNullable: false,\n inputTypes: [\n {\n isList: false,\n type: `${modelCountOutputTypeName}Select`,\n location: \"inputObjectTypes\",\n namespace: \"prisma\"\n }\n ]\n }\n ]\n };\n modelCountOutputTypeArgsInputObjectTypes.push(\n modelCountOutputTypeArgsInputObjectType\n );\n }\n\n return modelCountOutputTypeArgsInputObjectTypes;\n}\n\nfunction generateModelSelectInputObjectTypes(models: DMMF.Model[]) {\n const modelSelectInputObjectTypes: 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 const field: Writeable<DMMF.SchemaArg> = {\n name: modelFieldName,\n isRequired: false,\n isNullable: false,\n inputTypes: [\n { isList: false, type: \"Boolean\", location: \"scalar\" }\n ] as Writeable<DMMF.SchemaArg[\"inputTypes\"]>\n };\n\n if (isRelationField) {\n const schemaArgInputType: ArrayValues<SchemaArgInputTypes> = {\n isList: false,\n type: isList ? `${type}FindManyArgs` : `${type}DefaultArgs`,\n location: \"inputObjectTypes\",\n namespace: \"prisma\"\n };\n field.inputTypes.push(schemaArgInputType);\n }\n\n fields.push(field);\n }\n\n const hasManyRelationToAnotherModel = checkModelHasManyModelRelation(model);\n\n const shouldAddCountField = hasManyRelationToAnotherModel;\n if (shouldAddCountField) {\n const _countField: DMMF.SchemaArg = {\n name: \"_count\",\n isRequired: false,\n isNullable: false,\n inputTypes: [\n { isList: false, type: \"Boolean\", location: \"scalar\" },\n {\n isList: false,\n type: `${modelName}CountOutputTypeDefaultArgs`,\n location: \"inputObjectTypes\",\n namespace: \"prisma\"\n }\n ]\n };\n fields.push(_countField);\n }\n\n const modelSelectInputObjectType: DMMF.InputType = {\n name: `${modelName}Select`,\n constraints: {\n maxNumFields: null,\n minNumFields: null\n },\n fields\n };\n modelSelectInputObjectTypes.push(modelSelectInputObjectType);\n }\n\n return modelSelectInputObjectTypes;\n}\n"],"mappings":";;;AA0BA,SAAgB,oCACd,kBACA,mBACA,QACA;CAEA,MAAM,wBAAwB,yBAAyB,kBAAkB;CASzE,MAAM,4BAA4B;EAPhC,mDAAmD,sBAQT;EAN1C,iDAAiD,sBAOT;EAJxC,oCAAoC,OAKT;EAC5B,CAAC,MAAM;AAER,MAAK,MAAM,mBAAmB,0BAC5B,kBAAiB,KAAK,gBAAgB;;AAI1C,SAAS,yBAAyB,mBAAsC;AACtE,QAAO,kBAAkB,QAAQ,EAAE,WACjC,KAAK,SAAS,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