Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve enhance API code generation #1150

Merged
merged 4 commits into from
Mar 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 62 additions & 24 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ export class EnhancerGenerator {
let logicalPrismaClientDir: string | undefined;
let dmmf: DMMF.Document | undefined;

const withLogicalClient = this.needsLogicalClient();
const prismaImport = getPrismaClientImportSpec(this.outDir, this.options);

if (withLogicalClient) {
if (this.needsLogicalClient()) {
// schema contains delegate models, need to generate a logical prisma schema
const result = await this.generateLogicalPrisma();

Expand Down Expand Up @@ -90,22 +89,67 @@ export class EnhancerGenerator {
const enhanceTs = this.project.createSourceFile(
path.join(this.outDir, 'enhance.ts'),
`import { createEnhancement, type EnhancementContext, type EnhancementOptions, type ZodSchemas, type AuthUser } from '@zenstackhq/runtime';
import modelMeta from './model-meta';
import policy from './policy';
import { Prisma as _Prisma, PrismaClient as _PrismaClient } from '${prismaImport}';
import type { InternalArgs, TypeMapDef, TypeMapCbDef, DynamicClientExtensionThis } from '${prismaImport}/runtime/library';
${
withLogicalClient
? `import type * as _P from '${logicalPrismaClientDir}/index-fixed';
import type { Prisma, PrismaClient } from '${logicalPrismaClientDir}/index-fixed';
`
: `import type * as _P from '${prismaImport}';
import type { Prisma, PrismaClient } from '${prismaImport}';
`
import modelMeta from './model-meta';
import policy from './policy';
${this.options.withZodSchemas ? "import * as zodSchemas from './zod';" : 'const zodSchemas = undefined;'}

${
logicalPrismaClientDir
? this.createLogicalPrismaImports(prismaImport, logicalPrismaClientDir)
: this.createSimplePrismaImports(prismaImport)
}

${authTypes}

${
logicalPrismaClientDir
? this.createLogicalPrismaEnhanceFunction(authTypeParam)
: this.createSimplePrismaEnhanceFunction(authTypeParam)
}
`,
{ overwrite: true }
);

await this.saveSourceFile(enhanceTs);

return { dmmf };
}
${this.options.withZodSchemas ? "import * as zodSchemas from './zod';" : 'const zodSchemas = undefined;'}

${authTypes}

private createSimplePrismaImports(prismaImport: string) {
return `import { Prisma } from '${prismaImport}';
import type * as _P from '${prismaImport}';
`;
}

private createSimplePrismaEnhanceFunction(authTypeParam: string) {
return `
export function enhance<DbClient extends object>(prisma: DbClient, context?: EnhancementContext<${authTypeParam}>, options?: EnhancementOptions) {
return createEnhancement(prisma, {
modelMeta,
policy,
zodSchemas: zodSchemas as unknown as (ZodSchemas | undefined),
prismaModule: Prisma,
...options
}, context);
}
`;
}

private createLogicalPrismaImports(prismaImport: string, logicalPrismaClientDir: string) {
return `import { Prisma as _Prisma, PrismaClient as _PrismaClient } from '${prismaImport}';
import type {
InternalArgs,
TypeMapDef,
TypeMapCbDef,
DynamicClientExtensionThis,
} from '${prismaImport}/runtime/library';
import type * as _P from '${logicalPrismaClientDir}/index-fixed';
import type { Prisma, PrismaClient } from '${logicalPrismaClientDir}/index-fixed';
`;
}

private createLogicalPrismaEnhanceFunction(authTypeParam: string) {
return `
// overload for plain PrismaClient
export function enhance<ExtArgs extends Record<string, any> & InternalArgs>(
prisma: _PrismaClient<any, any, ExtArgs>,
Expand All @@ -125,13 +169,7 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
...options
}, context);
}
`,
{ overwrite: true }
);

await this.saveSourceFile(enhanceTs);

return { dmmf };
`;
}

private needsLogicalClient() {
Expand Down
Loading