Skip to content

Commit 5fafb0d

Browse files
authored
fix: Restore processing of prismaNamespace.ts (#2202)
1 parent f1d3026 commit 5fafb0d

File tree

1 file changed

+48
-1
lines changed
  • packages/schema/src/plugins/enhancer/enhance

1 file changed

+48
-1
lines changed

packages/schema/src/plugins/enhancer/enhance/index.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DELEGATE_AUX_RELATION_PREFIX } from '@zenstackhq/runtime';
2-
import { upperCaseFirst } from '@zenstackhq/runtime/local-helpers';
2+
import { invariant, upperCaseFirst } from '@zenstackhq/runtime/local-helpers';
33
import {
44
PluginError,
55
getAttribute,
@@ -569,6 +569,32 @@ export type Enhanced<Client> =
569569
private async processClientTypesNewPrismaGenerator(prismaClientDir: string, delegateInfo: DelegateInfo) {
570570
const project = new Project();
571571

572+
// remove delegate_aux_* fields from the prismaNamespace.ts
573+
const internalFilename = `${prismaClientDir}/internal/prismaNamespace.ts`
574+
const internalFilenameFixed = `${prismaClientDir}/internal/prismaNamespace-fixed.ts`
575+
const internalSf = project.addSourceFileAtPath(internalFilename);
576+
const syntaxList = internalSf.getChildren()[0];
577+
if (!Node.isSyntaxList(syntaxList)) {
578+
throw new PluginError(name, `Unexpected syntax list structure in ${internalFilename}`);
579+
}
580+
const statements: (string | StatementStructures)[] = [];
581+
582+
syntaxList.getChildren().forEach((node) => {
583+
if (Node.isVariableStatement(node)) {
584+
statements.push(this.transformVariableStatementProps(node));
585+
} else {
586+
statements.push(node.getText());
587+
}
588+
});
589+
const structure = internalSf.getStructure();
590+
structure.statements = statements;
591+
592+
const internalSfNew = project.createSourceFile(internalFilenameFixed, structure, {
593+
overwrite: true,
594+
});
595+
await internalSfNew.save();
596+
fs.renameSync(internalFilenameFixed, internalFilename);
597+
572598
// Create a shared file for all JSON fields type definitions
573599
const jsonFieldsFile = project.createSourceFile(path.join(this.outDir, 'json-types.ts'), undefined, {
574600
overwrite: true,
@@ -727,6 +753,27 @@ export type Enhanced<Client> =
727753
return structure;
728754
}
729755

756+
private transformVariableStatementProps(variable: VariableStatement) {
757+
const structure = variable.getStructure();
758+
759+
// remove `delegate_aux_*` fields from the variable's initializer
760+
const auxFields = this.findAuxProps(variable);
761+
if (auxFields.length > 0) {
762+
structure.declarations.forEach((variable) => {
763+
if (variable.initializer) {
764+
let source = variable.initializer;
765+
auxFields.forEach((f) => {
766+
invariant(typeof source === 'string');
767+
source = this.removeFromSource(source, f.getText());
768+
});
769+
variable.initializer = source;
770+
}
771+
});
772+
}
773+
774+
return structure;
775+
}
776+
730777
private transformInterface(iface: InterfaceDeclaration, delegateInfo: DelegateInfo) {
731778
const structure = iface.getStructure();
732779

0 commit comments

Comments
 (0)