From 4a5abbc14b0ea78a5f4143059c50bb46c25db42f Mon Sep 17 00:00:00 2001 From: Matt Hodgson Date: Fri, 25 Jul 2025 07:58:22 -0400 Subject: [PATCH] fix: Restore processing of prismaNamespace.ts for prisma-client generator --- .../src/plugins/enhancer/enhance/index.ts | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/schema/src/plugins/enhancer/enhance/index.ts b/packages/schema/src/plugins/enhancer/enhance/index.ts index f99bc58d2..313674569 100644 --- a/packages/schema/src/plugins/enhancer/enhance/index.ts +++ b/packages/schema/src/plugins/enhancer/enhance/index.ts @@ -1,5 +1,5 @@ import { DELEGATE_AUX_RELATION_PREFIX } from '@zenstackhq/runtime'; -import { upperCaseFirst } from '@zenstackhq/runtime/local-helpers'; +import { invariant, upperCaseFirst } from '@zenstackhq/runtime/local-helpers'; import { PluginError, getAttribute, @@ -569,6 +569,32 @@ export type Enhanced = private async processClientTypesNewPrismaGenerator(prismaClientDir: string, delegateInfo: DelegateInfo) { const project = new Project(); + // remove delegate_aux_* fields from the prismaNamespace.ts + const internalFilename = `${prismaClientDir}/internal/prismaNamespace.ts` + const internalFilenameFixed = `${prismaClientDir}/internal/prismaNamespace-fixed.ts` + const internalSf = project.addSourceFileAtPath(internalFilename); + const syntaxList = internalSf.getChildren()[0]; + if (!Node.isSyntaxList(syntaxList)) { + throw new PluginError(name, `Unexpected syntax list structure in ${internalFilename}`); + } + const statements: (string | StatementStructures)[] = []; + + syntaxList.getChildren().forEach((node) => { + if (Node.isVariableStatement(node)) { + statements.push(this.transformVariableStatementProps(node)); + } else { + statements.push(node.getText()); + } + }); + const structure = internalSf.getStructure(); + structure.statements = statements; + + const internalSfNew = project.createSourceFile(internalFilenameFixed, structure, { + overwrite: true, + }); + await internalSfNew.save(); + fs.renameSync(internalFilenameFixed, internalFilename); + // Create a shared file for all JSON fields type definitions const jsonFieldsFile = project.createSourceFile(path.join(this.outDir, 'json-types.ts'), undefined, { overwrite: true, @@ -727,6 +753,27 @@ export type Enhanced = return structure; } + private transformVariableStatementProps(variable: VariableStatement) { + const structure = variable.getStructure(); + + // remove `delegate_aux_*` fields from the variable's initializer + const auxFields = this.findAuxProps(variable); + if (auxFields.length > 0) { + structure.declarations.forEach((variable) => { + if (variable.initializer) { + let source = variable.initializer; + auxFields.forEach((f) => { + invariant(typeof source === 'string'); + source = this.removeFromSource(source, f.getText()); + }); + variable.initializer = source; + } + }); + } + + return structure; + } + private transformInterface(iface: InterfaceDeclaration, delegateInfo: DelegateInfo) { const structure = iface.getStructure();