UNPKG

prisma-json-types-generator

Version:
107 lines 5.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRegexForType = createRegexForType; exports.extractBaseNameFromRelationType = extractBaseNameFromRelationType; exports.isUpdateOneType = isUpdateOneType; exports.generateTypeNamesFromName = generateTypeNamesFromName; /** * A list of regexes to match all types and subtypes generated by prisma for a specific * model */ function createRegexForType(name) { return [ // new RegExp(`^${name}CountAggregate$`, 'm'), // new RegExp(`^${name}CountAggregateOutputType$`, 'm'), `number` fields // new RegExp(`^${name}CountOrderByAggregateInput$`, 'm'), `SortOrder` fields // new RegExp(`^${name}CountAggregateInputType$`, 'm'), `true` fields // new RegExp(`^${name}Group$`, 'm'), // new RegExp(`^${name}GroupByOutputType$`, 'm'), // new RegExp(`^${name}OrderByWithRelationInput$`, 'm'), `SortOrder` fields // new RegExp(`^${name}OrderByWithAggregationInput$`, 'm'), `SortOrder` fields // new RegExp(`^${name}(?:Scalar)?Where$`, 'm'), // new RegExp(`^${name}(?:Scalar)?WhereInput$`, 'm'), // new RegExp(`^${name}(?:Scalar)?WhereWithAggregatesInput$`, 'm'), // new RegExp(`^${name}(?:Unchecked)?CreateInput$`, 'm'), // new RegExp(`^${name}(?:Unchecked)?CreateManyInput$`, 'm'), new RegExp(`^${name}(?:Unchecked)?CreateWithout(?:\\w+?)Input$`, 'm'), new RegExp(`^${name}(?:Unchecked)?CreateMany(?:\\w+?)Input$`, 'm'), // new RegExp(`^${name}(?:Unchecked)?UpdateInput$`, 'm'), // new RegExp(`^${name}(?:Unchecked)?UpdateManyInput$`, 'm'), // new RegExp(`^${name}(?:Unchecked)?UpdateManyMutationInput$`, 'm'), new RegExp(`^${name}(?:Unchecked)?UpdateWithout(?:\\w+?)Input$`, 'm'), new RegExp(`^${name}(?:Unchecked)?UpdateManyWithout(?:\\w+?)Input$`, 'm') ]; } function extractBaseNameFromRelationType(typeName) { const createWithoutRegex = /^(.+?)(?:Unchecked)?CreateWithout(?:\w+?)Input$/m; const createManyRegex = /^(.+?)(?:Unchecked)?CreateMany(?:\w+?)Input$/m; const updateWithoutRegex = /^(.+?)(?:Unchecked)?UpdateWithout(?:\w+?)Input$/m; const updateManyWithoutRegex = /^(.+?)(?:Unchecked)?UpdateManyWithout(?:\w+?)Input$/m; let match = typeName.match(createWithoutRegex); if (match?.[1]) { return match[1]; } match = typeName.match(createManyRegex); if (match?.[1]) { return match[1]; } match = typeName.match(updateWithoutRegex); if (match?.[1]) { return match[1]; } match = typeName.match(updateManyWithoutRegex); if (match?.[1]) { return match[1]; } return null; } /** If the provided type is a update one variant */ function isUpdateOneType(type) { return (type.match(/UpdateInput$/m) || type.match(/UpdateWithout(?:\w+?)Input$/m) || type.match(/UpdateManyWithout(?:\w+?)Input$/m)); } /** * Capitalizes the first letter of a string */ function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); } /** * Generates a list of potential type names based on the regex patterns * used in `createRegexForType`. Note that types involving relation names * (e.g., `CreateWithout[Relation]Input`) are not generated by this function. * * @param name The base name (e.g., model name) * @returns An array of generated type names */ function generateTypeNamesFromName(name) { const types = []; // Prisma capitalizes the model name for output types const capitalizedName = capitalize(name); types.push(`$${name}Payload`); // CountAggregate - Output type (capitalized) types.push(`${capitalizedName}CountAggregate`); // GroupBy - Output types (capitalized) types.push(`${capitalizedName}Group`); types.push(`${capitalizedName}GroupByOutputType`); // Aggregate output types (capitalized) types.push(`${capitalizedName}AvgAggregateOutputType`); types.push(`${capitalizedName}SumAggregateOutputType`); types.push(`${capitalizedName}MinAggregateOutputType`); types.push(`${capitalizedName}MaxAggregateOutputType`); types.push(`${capitalizedName}CountAggregateOutputType`); // Where types - Input types (preserve original casing) types.push(`${name}Where`, `${name}ScalarWhere`); types.push(`${name}WhereInput`, `${name}ScalarWhereInput`); types.push(`${name}WhereWithAggregatesInput`, `${name}ScalarWhereWithAggregatesInput`); // Create types - Input types (preserve original casing) types.push(`${name}CreateInput`, `${name}UncheckedCreateInput`); types.push(`${name}CreateManyInput`, `${name}UncheckedCreateManyInput`); // Update types - Input types (preserve original casing) types.push(`${name}UpdateInput`, `${name}UncheckedUpdateInput`); types.push(`${name}UpdateManyInput`, `${name}UncheckedUpdateManyInput`); types.push(`${name}UpdateManyMutationInput`, `${name}UncheckedUpdateManyMutationInput`); return types; } //# sourceMappingURL=regex.js.map