-
-
Notifications
You must be signed in to change notification settings - Fork 106
fix: clean up generation of logical prisma client #1082
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import type { DMMF } from '@prisma/generator-helper'; | ||
import type { PluginOptions } from '@zenstackhq/sdk'; | ||
import type { Model } from '@zenstackhq/sdk/ast'; | ||
import type { PluginFunction } from '@zenstackhq/sdk'; | ||
import { generate } from './generator'; | ||
|
||
export const name = 'SWR'; | ||
|
||
export default async function run(model: Model, options: PluginOptions, dmmf: DMMF.Document) { | ||
const run: PluginFunction = async (model, options, dmmf) => { | ||
if (!dmmf) { | ||
throw new Error('DMMF is required'); | ||
} | ||
return generate(model, options, dmmf); | ||
} | ||
}; | ||
|
||
export default run; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,11 +55,11 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF. | |
warnings.push(`Unable to find mapping for model ${dataModel.name}`); | ||
return; | ||
} | ||
generateModelHooks(target, version, project, outDir, dataModel, mapping); | ||
generateModelHooks(target, version, project, outDir, dataModel, mapping, options); | ||
}); | ||
|
||
await saveProject(project); | ||
return warnings; | ||
return { warnings }; | ||
} | ||
|
||
function generateQueryHook( | ||
Comment on lines
55
to
65
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 risk: Ensure that user input passed to
Potential security risk: Ensure that user input passed to
Potential security risk: Ensure that user input passed to |
||
|
@@ -286,7 +286,8 @@ function generateModelHooks( | |
project: Project, | ||
outDir: string, | ||
model: DataModel, | ||
mapping: DMMF.ModelMapping | ||
mapping: DMMF.ModelMapping, | ||
options: PluginOptions | ||
) { | ||
const modelNameCap = upperCaseFirst(model.name); | ||
const prismaVersion = getPrismaVersion(); | ||
|
@@ -295,7 +296,7 @@ function generateModelHooks( | |
|
||
sf.addStatements('/* eslint-disable */'); | ||
|
||
const prismaImport = getPrismaClientImportSpec(model.$container, outDir); | ||
const prismaImport = getPrismaClientImportSpec(outDir, options); | ||
sf.addImportDeclaration({ | ||
namedImports: ['Prisma', model.name], | ||
isTypeOnly: true, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import type { DMMF } from '@prisma/generator-helper'; | ||
import type { PluginOptions } from '@zenstackhq/sdk'; | ||
import type { Model } from '@zenstackhq/sdk/ast'; | ||
import type { PluginFunction } from '@zenstackhq/sdk'; | ||
import { generate } from './generator'; | ||
|
||
export const name = 'Tanstack Query'; | ||
|
||
export default async function run(model: Model, options: PluginOptions, dmmf: DMMF.Document) { | ||
const run: PluginFunction = async (model, options, dmmf) => { | ||
if (!dmmf) { | ||
throw new Error('DMMF is required'); | ||
} | ||
return generate(model, options, dmmf); | ||
} | ||
}; | ||
|
||
export default run; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,8 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF. | |
generateModelActions, | ||
generateClientHelpers, | ||
model, | ||
zodSchemasImport | ||
zodSchemasImport, | ||
options | ||
); | ||
createHelper(outDir); | ||
|
||
|
@@ -86,7 +87,8 @@ function createAppRouter( | |
generateModelActions: string[] | undefined, | ||
generateClientHelpers: string[] | undefined, | ||
zmodel: Model, | ||
zodSchemasImport: string | ||
zodSchemasImport: string, | ||
options: PluginOptions | ||
) { | ||
const indexFile = path.resolve(outDir, 'routers', `index.ts`); | ||
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. Detected potential security risk related to path traversal vulnerabilities. Ensure that user input influencing file paths, such as Also applies to: 213-213, 222-222, 230-230, 247-247, 327-327 |
||
const appRouter = project.createSourceFile(indexFile, undefined, { | ||
|
@@ -95,7 +97,7 @@ function createAppRouter( | |
|
||
appRouter.addStatements('/* eslint-disable */'); | ||
|
||
const prismaImport = getPrismaClientImportSpec(zmodel, path.dirname(indexFile)); | ||
const prismaImport = getPrismaClientImportSpec(path.dirname(indexFile), options); | ||
appRouter.addImportDeclarations([ | ||
{ | ||
namedImports: [ | ||
|
@@ -169,8 +171,8 @@ function createAppRouter( | |
outDir, | ||
generateModelActions, | ||
generateClientHelpers, | ||
zmodel, | ||
zodSchemasImport | ||
zodSchemasImport, | ||
options | ||
); | ||
|
||
appRouter.addImportDeclaration({ | ||
|
@@ -239,8 +241,8 @@ function generateModelCreateRouter( | |
outputDir: string, | ||
generateModelActions: string[] | undefined, | ||
generateClientHelpers: string[] | undefined, | ||
zmodel: Model, | ||
zodSchemasImport: string | ||
zodSchemasImport: string, | ||
options: PluginOptions | ||
) { | ||
const modelRouter = project.createSourceFile(path.resolve(outputDir, 'routers', `${model}.router.ts`), undefined, { | ||
overwrite: true, | ||
|
@@ -258,7 +260,7 @@ function generateModelCreateRouter( | |
generateRouterSchemaImport(modelRouter, zodSchemasImport); | ||
generateHelperImport(modelRouter); | ||
if (generateClientHelpers) { | ||
generateRouterTypingImports(modelRouter, zmodel); | ||
generateRouterTypingImports(modelRouter, options); | ||
} | ||
|
||
const createRouterFunc = modelRouter.addFunction({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import type { DMMF } from '@prisma/generator-helper'; | ||
import { PluginOptions } from '@zenstackhq/sdk'; | ||
import { Model } from '@zenstackhq/sdk/ast'; | ||
import type { PluginFunction } from '@zenstackhq/sdk'; | ||
import { generate } from './generator'; | ||
|
||
export const name = 'tRPC'; | ||
export const dependencies = ['@core/zod']; | ||
|
||
export default async function run(model: Model, options: PluginOptions, dmmf: DMMF.Document) { | ||
const run: PluginFunction = async (model, options, dmmf) => { | ||
if (!dmmf) { | ||
throw new Error('DMMF is required'); | ||
} | ||
return generate(model, options, dmmf); | ||
} | ||
}; | ||
|
||
export default run; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type * from '.zenstack/prisma'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @ts-expect-error stub for re-exporting PrismaClient | ||
export type * from '.zenstack/prisma'; |
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.
Detected potential security risk related to path traversal vulnerabilities due to user input being used in
path.join
orpath.resolve
functions. Ensure that theoutDir
and other path-related inputs are properly sanitized or validated to prevent unauthorized file system access.Consider implementing a
sanitizePath
function that checks and cleans the input path to mitigate this risk.Also applies to: 68-68, 279-279