Skip to content

Commit 6e7993a

Browse files
authored
fix: clean up generation of logical prisma client (#1082)
1 parent 430ab62 commit 6e7993a

37 files changed

+961
-757
lines changed

packages/plugins/openapi/src/generator-base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DMMF } from '@prisma/generator-helper';
2-
import { PluginError, PluginOptions, getDataModels, hasAttribute } from '@zenstackhq/sdk';
2+
import { PluginError, PluginOptions, PluginResult, getDataModels, hasAttribute } from '@zenstackhq/sdk';
33
import { Model } from '@zenstackhq/sdk/ast';
44
import type { OpenAPIV3_1 as OAPI } from 'openapi-types';
55
import semver from 'semver';
@@ -12,7 +12,7 @@ export abstract class OpenAPIGeneratorBase {
1212

1313
constructor(protected model: Model, protected options: PluginOptions, protected dmmf: DMMF.Document) {}
1414

15-
abstract generate(): string[];
15+
abstract generate(): PluginResult;
1616

1717
protected get includedModels() {
1818
return getDataModels(this.model).filter((d) => !hasAttribute(d, '@@openapi.ignore'));

packages/plugins/openapi/src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import type { DMMF } from '@prisma/generator-helper';
2-
import { PluginError, PluginOptions } from '@zenstackhq/sdk';
3-
import { Model } from '@zenstackhq/sdk/ast';
1+
import { PluginError, PluginFunction } from '@zenstackhq/sdk';
42
import { RESTfulOpenAPIGenerator } from './rest-generator';
53
import { RPCOpenAPIGenerator } from './rpc-generator';
64

75
export const name = 'OpenAPI';
86

9-
export default async function run(model: Model, options: PluginOptions, dmmf: DMMF.Document) {
7+
const run: PluginFunction = async (model, options, dmmf) => {
8+
if (!dmmf) {
9+
throw new Error('DMMF is required');
10+
}
11+
1012
const flavor = options.flavor ? (options.flavor as string) : 'rpc';
1113

1214
switch (flavor) {
@@ -17,4 +19,6 @@ export default async function run(model: Model, options: PluginOptions, dmmf: DM
1719
default:
1820
throw new PluginError(name, `Unknown flavor: ${flavor}`);
1921
}
20-
}
22+
};
23+
24+
export default run;

packages/plugins/openapi/src/rest-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
7676
fs.writeFileSync(output, JSON.stringify(openapi, undefined, 2));
7777
}
7878

79-
return this.warnings;
79+
return { warnings: this.warnings };
8080
}
8181

8282
private generatePaths(): OAPI.PathsObject {

packages/plugins/openapi/src/rpc-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
8989
fs.writeFileSync(output, JSON.stringify(openapi, undefined, 2));
9090
}
9191

92-
return this.warnings;
92+
return { warnings: this.warnings };
9393
}
9494

9595
private generatePaths(components: OAPI.ComponentsObject): OAPI.PathsObject {

packages/plugins/swr/src/generator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,27 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.
4949
warnings.push(`Unable to find mapping for model ${dataModel.name}`);
5050
return;
5151
}
52-
generateModelHooks(project, outDir, dataModel, mapping, legacyMutations);
52+
generateModelHooks(project, outDir, dataModel, mapping, legacyMutations, options);
5353
});
5454

5555
await saveProject(project);
56-
return warnings;
56+
return { warnings };
5757
}
5858

5959
function generateModelHooks(
6060
project: Project,
6161
outDir: string,
6262
model: DataModel,
6363
mapping: DMMF.ModelMapping,
64-
legacyMutations: boolean
64+
legacyMutations: boolean,
65+
options: PluginOptions
6566
) {
6667
const fileName = paramCase(model.name);
6768
const sf = project.createSourceFile(path.join(outDir, `${fileName}.ts`), undefined, { overwrite: true });
6869

6970
sf.addStatements('/* eslint-disable */');
7071

71-
const prismaImport = getPrismaClientImportSpec(model.$container, outDir);
72+
const prismaImport = getPrismaClientImportSpec(outDir, options);
7273
sf.addImportDeclaration({
7374
namedImports: ['Prisma'],
7475
isTypeOnly: true,

packages/plugins/swr/src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import type { DMMF } from '@prisma/generator-helper';
2-
import type { PluginOptions } from '@zenstackhq/sdk';
3-
import type { Model } from '@zenstackhq/sdk/ast';
1+
import type { PluginFunction } from '@zenstackhq/sdk';
42
import { generate } from './generator';
53

64
export const name = 'SWR';
75

8-
export default async function run(model: Model, options: PluginOptions, dmmf: DMMF.Document) {
6+
const run: PluginFunction = async (model, options, dmmf) => {
7+
if (!dmmf) {
8+
throw new Error('DMMF is required');
9+
}
910
return generate(model, options, dmmf);
10-
}
11+
};
12+
13+
export default run;

packages/plugins/tanstack-query/src/generator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.
5555
warnings.push(`Unable to find mapping for model ${dataModel.name}`);
5656
return;
5757
}
58-
generateModelHooks(target, version, project, outDir, dataModel, mapping);
58+
generateModelHooks(target, version, project, outDir, dataModel, mapping, options);
5959
});
6060

6161
await saveProject(project);
62-
return warnings;
62+
return { warnings };
6363
}
6464

6565
function generateQueryHook(
@@ -286,7 +286,8 @@ function generateModelHooks(
286286
project: Project,
287287
outDir: string,
288288
model: DataModel,
289-
mapping: DMMF.ModelMapping
289+
mapping: DMMF.ModelMapping,
290+
options: PluginOptions
290291
) {
291292
const modelNameCap = upperCaseFirst(model.name);
292293
const prismaVersion = getPrismaVersion();
@@ -295,7 +296,7 @@ function generateModelHooks(
295296

296297
sf.addStatements('/* eslint-disable */');
297298

298-
const prismaImport = getPrismaClientImportSpec(model.$container, outDir);
299+
const prismaImport = getPrismaClientImportSpec(outDir, options);
299300
sf.addImportDeclaration({
300301
namedImports: ['Prisma', model.name],
301302
isTypeOnly: true,
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import type { DMMF } from '@prisma/generator-helper';
2-
import type { PluginOptions } from '@zenstackhq/sdk';
3-
import type { Model } from '@zenstackhq/sdk/ast';
1+
import type { PluginFunction } from '@zenstackhq/sdk';
42
import { generate } from './generator';
53

64
export const name = 'Tanstack Query';
75

8-
export default async function run(model: Model, options: PluginOptions, dmmf: DMMF.Document) {
6+
const run: PluginFunction = async (model, options, dmmf) => {
7+
if (!dmmf) {
8+
throw new Error('DMMF is required');
9+
}
910
return generate(model, options, dmmf);
10-
}
11+
};
12+
13+
export default run;

packages/plugins/trpc/src/generator.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.
7272
generateModelActions,
7373
generateClientHelpers,
7474
model,
75-
zodSchemasImport
75+
zodSchemasImport,
76+
options
7677
);
7778
createHelper(outDir);
7879

@@ -86,7 +87,8 @@ function createAppRouter(
8687
generateModelActions: string[] | undefined,
8788
generateClientHelpers: string[] | undefined,
8889
zmodel: Model,
89-
zodSchemasImport: string
90+
zodSchemasImport: string,
91+
options: PluginOptions
9092
) {
9193
const indexFile = path.resolve(outDir, 'routers', `index.ts`);
9294
const appRouter = project.createSourceFile(indexFile, undefined, {
@@ -95,7 +97,7 @@ function createAppRouter(
9597

9698
appRouter.addStatements('/* eslint-disable */');
9799

98-
const prismaImport = getPrismaClientImportSpec(zmodel, path.dirname(indexFile));
100+
const prismaImport = getPrismaClientImportSpec(path.dirname(indexFile), options);
99101
appRouter.addImportDeclarations([
100102
{
101103
namedImports: [
@@ -169,8 +171,8 @@ function createAppRouter(
169171
outDir,
170172
generateModelActions,
171173
generateClientHelpers,
172-
zmodel,
173-
zodSchemasImport
174+
zodSchemasImport,
175+
options
174176
);
175177

176178
appRouter.addImportDeclaration({
@@ -239,8 +241,8 @@ function generateModelCreateRouter(
239241
outputDir: string,
240242
generateModelActions: string[] | undefined,
241243
generateClientHelpers: string[] | undefined,
242-
zmodel: Model,
243-
zodSchemasImport: string
244+
zodSchemasImport: string,
245+
options: PluginOptions
244246
) {
245247
const modelRouter = project.createSourceFile(path.resolve(outputDir, 'routers', `${model}.router.ts`), undefined, {
246248
overwrite: true,
@@ -258,7 +260,7 @@ function generateModelCreateRouter(
258260
generateRouterSchemaImport(modelRouter, zodSchemasImport);
259261
generateHelperImport(modelRouter);
260262
if (generateClientHelpers) {
261-
generateRouterTypingImports(modelRouter, zmodel);
263+
generateRouterTypingImports(modelRouter, options);
262264
}
263265

264266
const createRouterFunc = modelRouter.addFunction({

packages/plugins/trpc/src/helpers.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { DMMF } from '@prisma/generator-helper';
2-
import { PluginError, getPrismaClientImportSpec } from '@zenstackhq/sdk';
3-
import { Model } from '@zenstackhq/sdk/ast';
2+
import { PluginError, getPrismaClientImportSpec, type PluginOptions } from '@zenstackhq/sdk';
43
import { lowerCaseFirst } from 'lower-case-first';
54
import { CodeBlockWriter, SourceFile } from 'ts-morph';
65
import { upperCaseFirst } from 'upper-case-first';
@@ -225,9 +224,9 @@ export function generateRouterTyping(writer: CodeBlockWriter, opType: string, mo
225224
});
226225
}
227226

228-
export function generateRouterTypingImports(sourceFile: SourceFile, model: Model) {
227+
export function generateRouterTypingImports(sourceFile: SourceFile, options: PluginOptions) {
229228
const importingDir = sourceFile.getDirectoryPath();
230-
const prismaImport = getPrismaClientImportSpec(model, importingDir);
229+
const prismaImport = getPrismaClientImportSpec(importingDir, options);
231230
sourceFile.addStatements([
232231
`import type { Prisma } from '${prismaImport}';`,
233232
`import type { UseTRPCMutationOptions, UseTRPCMutationResult, UseTRPCQueryOptions, UseTRPCQueryResult, UseTRPCInfiniteQueryOptions, UseTRPCInfiniteQueryResult } from '@trpc/react-query/shared';`,

packages/plugins/trpc/src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import type { DMMF } from '@prisma/generator-helper';
2-
import { PluginOptions } from '@zenstackhq/sdk';
3-
import { Model } from '@zenstackhq/sdk/ast';
1+
import type { PluginFunction } from '@zenstackhq/sdk';
42
import { generate } from './generator';
53

64
export const name = 'tRPC';
75
export const dependencies = ['@core/zod'];
86

9-
export default async function run(model: Model, options: PluginOptions, dmmf: DMMF.Document) {
7+
const run: PluginFunction = async (model, options, dmmf) => {
8+
if (!dmmf) {
9+
throw new Error('DMMF is required');
10+
}
1011
return generate(model, options, dmmf);
11-
}
12+
};
13+
14+
export default run;

packages/runtime/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
"import": "./cross/index.mjs",
4747
"require": "./cross/index.js",
4848
"default": "./cross/index.js"
49+
},
50+
"./prisma": {
51+
"types": "./prisma.d.ts"
4952
}
5053
},
5154
"publishConfig": {

packages/runtime/res/prisma.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type * from '.zenstack/prisma';

packages/runtime/src/prisma.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @ts-expect-error stub for re-exporting PrismaClient
2+
export type * from '.zenstack/prisma';

0 commit comments

Comments
 (0)