-
-
Notifications
You must be signed in to change notification settings - Fork 127
perf: improve polymorphism code generation speed #1073
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import { | |
| InvocationExpr, | ||
| isArrayExpr, | ||
| isDataModel, | ||
| isDataSource, | ||
| isInvocationExpr, | ||
| isLiteralExpr, | ||
| isNullExpr, | ||
|
Comment on lines
17
to
23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Potential security issue detected: user input is used in - return path.resolve(path.dirname(pkgJsonPath), pkgJson.zenstack.prisma);
+ // Ensure pkgJson.zenstack.prisma is sanitized or validated before use
+ return path.resolve(path.dirname(pkgJsonPath), sanitizedInput); |
||
|
|
@@ -79,6 +80,7 @@ import { | |
|
|
||
| const MODEL_PASSTHROUGH_ATTR = '@@prisma.passthrough'; | ||
| const FIELD_PASSTHROUGH_ATTR = '@prisma.passthrough'; | ||
| const PROVIDERS_SUPPORTING_NAMED_CONSTRAINTS = ['postgresql', 'mysql', 'cockroachdb']; | ||
|
|
||
| /** | ||
| * Generates Prisma schema file | ||
|
|
@@ -95,7 +97,9 @@ export class PrismaSchemaGenerator { | |
|
|
||
| private mode: 'logical' | 'physical' = 'physical'; | ||
|
|
||
| async generate(model: Model, options: PluginOptions) { | ||
| constructor(private readonly zmodel: Model) {} | ||
|
|
||
| async generate(options: PluginOptions) { | ||
| const warnings: string[] = []; | ||
| if (options.mode) { | ||
| this.mode = options.mode as 'logical' | 'physical'; | ||
|
|
@@ -110,7 +114,7 @@ export class PrismaSchemaGenerator { | |
|
|
||
| const prisma = new PrismaModel(); | ||
|
|
||
| for (const decl of model.declarations) { | ||
| for (const decl of this.zmodel.declarations) { | ||
| switch (decl.$type) { | ||
| case DataSource: | ||
| this.generateDataSource(prisma, decl as DataSource); | ||
|
|
@@ -151,7 +155,7 @@ export class PrismaSchemaGenerator { | |
| const generateClient = options.generateClient !== false; | ||
|
|
||
| if (generateClient) { | ||
| let generateCmd = `prisma generate --schema "${outFile}"`; | ||
| let generateCmd = `prisma generate --schema "${outFile}"${this.mode === 'logical' ? ' --no-engine' : ''}`; | ||
| if (typeof options.generateArgs === 'string') { | ||
| generateCmd += ` ${options.generateArgs}`; | ||
| } | ||
|
|
@@ -452,17 +456,23 @@ export class PrismaSchemaGenerator { | |
| new AttributeArgValue('FieldReference', new PrismaFieldReference(idField.name)) | ||
| ) | ||
| ); | ||
| relationField.attributes.push( | ||
| new PrismaFieldAttribute('@relation', [ | ||
| new PrismaAttributeArg('fields', args), | ||
| new PrismaAttributeArg('references', args), | ||
|
|
||
| const addedRel = new PrismaFieldAttribute('@relation', [ | ||
| new PrismaAttributeArg('fields', args), | ||
| new PrismaAttributeArg('references', args), | ||
| ]); | ||
|
|
||
| if (this.supportNamedConstraints) { | ||
| addedRel.args.push( | ||
| // generate a `map` argument for foreign key constraint disambiguation | ||
| new PrismaAttributeArg( | ||
| 'map', | ||
| new PrismaAttributeArgValue('String', `${relationField.name}_fk`) | ||
| ), | ||
| ]) | ||
| ); | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| relationField.attributes.push(addedRel); | ||
| } else { | ||
| relationField.attributes.push(this.makeFieldAttribute(relAttr as DataModelFieldAttribute)); | ||
| } | ||
|
|
@@ -471,6 +481,21 @@ export class PrismaSchemaGenerator { | |
| }); | ||
| } | ||
|
|
||
| private get supportNamedConstraints() { | ||
| const ds = this.zmodel.declarations.find(isDataSource); | ||
| if (!ds) { | ||
| return false; | ||
| } | ||
|
|
||
| const provider = ds.fields.find((f) => f.name === 'provider'); | ||
| if (!provider) { | ||
| return false; | ||
| } | ||
|
|
||
| const value = getStringLiteral(provider.value); | ||
| return value && PROVIDERS_SUPPORTING_NAMED_CONSTRAINTS.includes(value); | ||
| } | ||
|
|
||
| private isPrismaAttribute(attr: DataModelAttribute | DataModelFieldAttribute) { | ||
| if (!attr.decl.ref) { | ||
| return false; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
path.joinwithoptions.schemaPathandoutDircould potentially lead to path traversal vulnerabilities if user input is not properly sanitized or validated. Ensure that any user-supplied paths are rigorously checked to prevent unauthorized file system access.Consider implementing a
sanitizePathfunction that checks for and mitigates any path traversal patterns.