UNPKG

prisma-json-types-generator

Version:
218 lines 8.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findNewSignature = findNewSignature; const constants_1 = require("../util/constants"); const error_1 = require("../util/error"); const regex_1 = require("./regex"); /** Handles and replaces the signature of a typed field. */ function findNewSignature(signature, typeToChange, model, field, throwOnNotFound = true, shouldReplaceStrings = true, libNamespace = '') { // Updates should leave optional fields if ((0, regex_1.isUpdateOneType)(model)) { typeToChange = `${libNamespace}UpdateInput<${typeToChange}>`; } let result; let skipVar; for (const skipVariant of constants_1.PRISMA_SKIP) { const hasSkip = signature.indexOf(skipVariant); // removes skip from the search if (hasSkip !== -1) { signature = (signature.slice(0, hasSkip) + signature.slice(hasSkip + skipVariant.length)).trim(); skipVar = skipVariant; } } // Removes Prisma. namespace for easier matching const hasPrismaNamespace = signature.startsWith('Prisma.'); if (hasPrismaNamespace) signature = signature.slice('Prisma.'.length); // Removes all runtime.x to simplify matching signature = signature.replace(/( ?)runtime\./g, '$1'); switch (signature) { // // Normal // case 'JsonValue': case 'InputJsonValue': case 'InputJsonValue | InputJsonValue': case 'JsonNullValueInput | InputJsonValue': result = typeToChange; break; // Super complex type that strictly typing will lose functionality case `JsonWithAggregatesFilter<"${model}">`: case `JsonFilter<"${model}">`: case `IntWithAggregatesFilter<"${model}"> | number`: case `FloatWithAggregatesFilter<"${model}"> | number`: case `IntNullableWithAggregatesFilter<"${model}"> | number | null`: case `FloatNullableWithAggregatesFilter<"${model}"> | number | null`: break; // // Int/Float filters with type annotations // Transforms: IntFilter<"Model"> | number -> IntFilter<"Model"> | CustomType // Preserves filter functionality while constraining literal values to the custom type // case `IntFilter<"${model}"> | number`: case `FloatFilter<"${model}"> | number`: if (!shouldReplaceStrings) { break; } result = `${hasPrismaNamespace ? 'Prisma.' : ''}${signature.replace(/ \| number$/, ` | ${typeToChange}`)}`; break; case `IntNullableFilter<"${model}"> | number | null`: case `FloatNullableFilter<"${model}"> | number | null`: if (!shouldReplaceStrings) { break; } // Transforms: IntNullableFilter<"Model"> | number | null -> IntNullableFilter<"Model"> | CustomType | null result = `${hasPrismaNamespace ? 'Prisma.' : ''}${signature.replace(/ \| number \| null$/, ` | ${typeToChange} | null`)}`; break; // // String // case 'string': case 'number': if (!shouldReplaceStrings) { break; } result = typeToChange; break; case 'string[]': case 'number[]': if (!shouldReplaceStrings) { break; } result = `(${typeToChange})[]`; break; case 'string | null': case 'number | null': if (!shouldReplaceStrings) { break; } result = `${typeToChange} | null`; break; case `StringFilter<"${model}"> | string`: if (!shouldReplaceStrings) { break; } result = `${libNamespace}TypedStringFilter<${typeToChange}> | ${typeToChange}`; break; case `StringNullableFilter<"${model}"> | string | null`: if (!shouldReplaceStrings) { break; } result = `${libNamespace}TypedStringNullableFilter<${typeToChange}> | ${typeToChange} | null`; break; case `StringNullableListFilter<"${model}">`: if (!shouldReplaceStrings) { break; } result = `${libNamespace}TypedStringNullableListFilter<${typeToChange}>`; break; case `StringWithAggregatesFilter<"${model}"> | string`: if (!shouldReplaceStrings) { break; } result = `${libNamespace}TypedStringWithAggregatesFilter<${typeToChange}> | ${typeToChange}`; break; case `StringNullableWithAggregatesFilter<"${model}"> | string | null`: if (!shouldReplaceStrings) { break; } result = `${libNamespace}TypedStringNullableWithAggregatesFilter<${typeToChange}> | ${typeToChange}`; break; case 'StringFieldUpdateOperationsInput | string': if (!shouldReplaceStrings) { break; } result = `${libNamespace}TypedStringFieldUpdateOperationsInput<${typeToChange}> | ${typeToChange}`; break; case 'IntFieldUpdateOperationsInput | number': case 'FloatFieldUpdateOperationsInput | number': if (!shouldReplaceStrings) { break; } result = typeToChange; break; case 'NullableStringFieldUpdateOperationsInput | string | null': if (!shouldReplaceStrings) { break; } result = `${libNamespace}TypedNullableStringFieldUpdateOperationsInput<${typeToChange}> | ${typeToChange} | null`; break; case 'NullableIntFieldUpdateOperationsInput | number | null': case 'NullableFloatFieldUpdateOperationsInput | number | null': if (!shouldReplaceStrings) { break; } result = `${typeToChange} | null`; break; case `${model}Create${field}Input | string[]`: case `${model}Create${field}Input | number[]`: if (!shouldReplaceStrings) { break; } result = `${libNamespace}CreateStringArrayInput<${typeToChange}> | ${typeToChange}[]`; break; case `${model}Update${field}Input | string[]`: case `${model}Update${field}Input | number[]`: if (!shouldReplaceStrings) { break; } result = `${libNamespace}CreateStringArrayInput<${typeToChange}> | ${typeToChange}[]`; break; // // Nullable // case 'JsonValue | null': case 'runtime.JsonValue | null': case 'InputJsonValue | null': case 'InputJsonValue | InputJsonValue | null': result = `${typeToChange} | null`; break; case 'NullableJsonNullValueInput | InputJsonValue': case 'NullableJsonNullValueInput | runtime.InputJsonValue': // differentiates null in column or a json null value // Use hasPrismaNamespace which was captured before stripping the prefix result = `${typeToChange} | ${hasPrismaNamespace ? 'Prisma.' : ''}NullableJsonNullValueInput`; break; // Super complex type that strictly typing will lose functionality case `JsonNullableWithAggregatesFilter<"${model}">`: case `JsonNullableFilter<"${model}">`: break; // // Array // case 'JsonValue[]': case 'InputJsonValue[]': case `${model}CreatelistInput | InputJsonValue[]`: result = `${typeToChange}[]`; break; case `JsonNullableListFilter<"${model}">`: result = `${libNamespace}NullableListFilter<${typeToChange}>`; break; case `${model}Update${field}Input | InputJsonValue[]`: result = `${libNamespace}UpdateManyInput<${typeToChange}>`; break; case `${model}Create${field}Input | InputJsonValue[]`: result = `${libNamespace}CreateManyInput<${typeToChange}>`; break; // // Unknown types, its safe to throw an error here because each field does not conflict with other fields generation. // default: if (throwOnNotFound) { throw new error_1.PrismaJsonTypesGeneratorError('Found unsupported required field type', { signature, typeToChange, type: model, fieldName: field, throwOnNotFound }); } break; } // Add it back later if (result && skipVar) { result += skipVar; } return result; } //# sourceMappingURL=find-signature.js.map