prisma-json-types-generator
Advanced tools
Comparing version
@@ -10,3 +10,3 @@ "use strict"; | ||
/** Handles the prisma namespace module. */ | ||
function handlePrismaModule(child, writer, models, config) { | ||
function handlePrismaModule(child, writer, modelMap, knownNoOps, typeToNameMap, config) { | ||
const name = child | ||
@@ -28,3 +28,3 @@ .getChildren() | ||
try { | ||
(0, statement_1.handleStatement)(statement, writer, models, config); | ||
(0, statement_1.handleStatement)(statement, writer, modelMap, typeToNameMap, knownNoOps, config); | ||
} | ||
@@ -31,0 +31,0 @@ catch (error) { |
@@ -6,2 +6,3 @@ "use strict"; | ||
const typescript_1 = tslib_1.__importDefault(require("typescript")); | ||
const regex_1 = require("../helpers/regex"); | ||
const model_payload_1 = require("./model-payload"); | ||
@@ -13,3 +14,3 @@ const replace_object_1 = require("./replace-object"); | ||
*/ | ||
function handleStatement(statement, writer, models, config) { | ||
function handleStatement(statement, writer, modelMap, typeToNameMap, knownNoOps, config) { | ||
if (statement.kind !== typescript_1.default.SyntaxKind.TypeAliasDeclaration) { | ||
@@ -23,18 +24,29 @@ return; | ||
} | ||
const name = type.name.getText(); | ||
// Goes through each model and checks if the type name matches any of the regexps | ||
for (const model of models) { | ||
// If this is the main model payload type | ||
if (name === `$${model.name}Payload`) { | ||
return (0, model_payload_1.handleModelPayload)(type, writer, model, config); | ||
// Skip the type if it belongs to a model without Json or a type comment | ||
if (knownNoOps.has(type.name.getText())) { | ||
return; | ||
} | ||
const typeName = type.name.getText(); | ||
const modelName = typeToNameMap.get(typeName); | ||
// Extract the name of the model from the type name | ||
if (modelName) { | ||
const model = modelMap.get(modelName); | ||
if (model) { | ||
if (typeName === `$${modelName}Payload`) { | ||
return (0, model_payload_1.handleModelPayload)(type, writer, model, config); | ||
} | ||
return (0, replace_object_1.replaceObject)(type.type, writer, model, config); | ||
} | ||
// If this statement matches some create/update/where input/output type | ||
for (const regexp of model.regexps) { | ||
if (regexp.test(name)) { | ||
} | ||
else { | ||
// If the type name isn't constant, match the model name using a regex, then do the lookup | ||
const baseName = (0, regex_1.extractBaseNameFromRelationType)(typeName); | ||
if (baseName) { | ||
const model = modelMap.get(baseName); | ||
if (model) { | ||
return (0, replace_object_1.replaceObject)(type.type, writer, model, config); | ||
} | ||
} | ||
// No model found for this statement, just ignore this type. | ||
} | ||
} | ||
//# sourceMappingURL=statement.js.map |
@@ -5,2 +5,9 @@ "use strict"; | ||
const regex_1 = require("./regex"); | ||
const type_parser_1 = require("./type-parser"); | ||
const isTransformable = (field) => { | ||
if (field.type === 'Json') { | ||
return true; | ||
} | ||
return (0, type_parser_1.parseTypeSyntax)(field.documentation); | ||
}; | ||
/** | ||
@@ -18,5 +25,3 @@ * Parses the DMMF document and returns a list of models that have at least one field with | ||
})); | ||
const types = dmmf.datamodel.types | ||
// Define the regexes for each model | ||
.map((model) => ({ | ||
const types = dmmf.datamodel.types.map((model) => ({ | ||
...model, | ||
@@ -26,4 +31,25 @@ type: 'type', | ||
})); | ||
return models.concat(types); | ||
const allModels = []; | ||
const knownNoOps = new Set(); | ||
for (const m of models.concat(types)) { | ||
if (m.fields.some((f) => isTransformable(f))) { | ||
allModels.push(m); | ||
} | ||
else { | ||
knownNoOps.add(m.name); | ||
} | ||
} | ||
const typeToNameMap = new Map(); | ||
for (const m of allModels) { | ||
const operations = (0, regex_1.generateTypeNamesFromName)(m.name); | ||
for (const o of operations) { | ||
typeToNameMap.set(o, m.name); | ||
} | ||
} | ||
const modelMap = new Map(); | ||
for (const m of allModels) { | ||
modelMap.set(m.name, m); | ||
} | ||
return { typeToNameMap, modelMap, knownNoOps }; | ||
} | ||
//# sourceMappingURL=dmmf.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRegexForType = createRegexForType; | ||
exports.extractBaseNameFromRelationType = extractBaseNameFromRelationType; | ||
exports.isUpdateOneType = isUpdateOneType; | ||
exports.generateTypeNamesFromName = generateTypeNamesFromName; | ||
/** | ||
@@ -11,23 +13,41 @@ * A list of regexes to match all types and subtypes generated by prisma for a specific | ||
return [ | ||
new RegExp(`^${name}CountAggregate$`, 'm'), | ||
// 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}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}(?: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)?UpdateInput$`, 'm'), | ||
// new RegExp(`^${name}(?:Unchecked)?UpdateManyInput$`, 'm'), | ||
// new RegExp(`^${name}(?:Unchecked)?UpdateManyMutationInput$`, 'm'), | ||
new RegExp(`^${name}(?:Unchecked)?UpdateWithout(?:\\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; | ||
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]; | ||
} | ||
return null; | ||
} | ||
/** If the provided type is a update one variant */ | ||
@@ -37,2 +57,31 @@ function isUpdateOneType(type) { | ||
} | ||
/** | ||
* 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 = []; | ||
types.push(`$${name}Payload`); | ||
// CountAggregate | ||
types.push(`${name}CountAggregate`); | ||
// GroupBy | ||
types.push(`${name}Group`); | ||
types.push(`${name}GroupByOutputType`); | ||
// Where types | ||
types.push(`${name}Where`, `${name}ScalarWhere`); | ||
types.push(`${name}WhereInput`, `${name}ScalarWhereInput`); | ||
types.push(`${name}WhereWithAggregatesInput`, `${name}ScalarWhereWithAggregatesInput`); | ||
// Create types (excluding relation-specific ones) | ||
types.push(`${name}CreateInput`, `${name}UncheckedCreateInput`); | ||
types.push(`${name}CreateManyInput`, `${name}UncheckedCreateManyInput`); | ||
// Update types (excluding relation-specific ones) | ||
types.push(`${name}UpdateInput`, `${name}UncheckedUpdateInput`); | ||
types.push(`${name}UpdateManyInput`, `${name}UncheckedUpdateManyInput`); | ||
types.push(`${name}UpdateManyMutationInput`, `${name}UncheckedUpdateManyMutationInput`); | ||
return types; | ||
} | ||
//# sourceMappingURL=regex.js.map |
@@ -26,3 +26,3 @@ "use strict"; | ||
const tsSource = typescript_1.default.createSourceFile(writer.filepath, writer.content, typescript_1.default.ScriptTarget.ESNext, true, typescript_1.default.ScriptKind.TS); | ||
const prismaModels = (0, dmmf_1.extractPrismaModels)(options.dmmf); | ||
const { typeToNameMap, modelMap, knownNoOps } = (0, dmmf_1.extractPrismaModels)(options.dmmf); | ||
// Handles the prisma namespace. | ||
@@ -32,3 +32,3 @@ tsSource.forEachChild((child) => { | ||
if (child.kind === typescript_1.default.SyntaxKind.ModuleDeclaration) { | ||
(0, module_1.handlePrismaModule)(child, writer, prismaModels, config); | ||
(0, module_1.handlePrismaModule)(child, writer, modelMap, knownNoOps, typeToNameMap, config); | ||
} | ||
@@ -35,0 +35,0 @@ } |
{ | ||
"name": "prisma-json-types-generator", | ||
"version": "3.3.0", | ||
"version": "3.3.1", | ||
"description": "Changes JsonValues to your custom typescript type", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71548
9.28%993
9.6%