Skip to content

Commit 42e5dfe

Browse files
committed
Minor tweaks
- Change the file name containing json types from "json-fields.ts" to "json-types.ts" - Fixed the import path of `DynamicClientExtensionThis` etc. to "@prisma/client/runtime/library" - Removed an unnecessary async modifier - Added a regression test case
1 parent e3421bc commit 42e5dfe

2 files changed

Lines changed: 54 additions & 8 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class EnhancerGenerator {
122122

123123
// reexport PrismaClient types (original or fixed)
124124
const modelsTsContent = `export * from '${resultPrismaTypeImport}';${
125-
this.isNewPrismaClientGenerator ? "\nexport * from './json-fields';" : ''
125+
this.isNewPrismaClientGenerator ? "\nexport * from './json-types';" : ''
126126
}`;
127127

128128
const modelsTs = this.project.createSourceFile(path.join(this.outDir, 'models.ts'), modelsTsContent, {
@@ -243,7 +243,7 @@ export function enhance<DbClient extends object>(prisma: DbClient, context?: Enh
243243
private createLogicalPrismaImports(prismaImport: string, prismaClientImport: string, target: string) {
244244
const prismaTargetImport = target === 'edge' ? `${prismaImport}/edge` : prismaImport;
245245
return `import { Prisma as _Prisma, PrismaClient as _PrismaClient } from '${prismaTargetImport}';
246-
import type { InternalArgs, DynamicClientExtensionThis } from '${prismaImport}/runtime/library';
246+
import type { InternalArgs, DynamicClientExtensionThis } from '@prisma/client/runtime/library';
247247
import type * as _P from '${prismaClientImport}';
248248
import type { Prisma, PrismaClient } from '${prismaClientImport}';
249249
export type { PrismaClient };
@@ -493,12 +493,12 @@ export type Enhanced<Client> =
493493
const project = new Project();
494494

495495
// Create a shared file for all JSON fields type definitions
496-
const jsonFieldsFile = project.createSourceFile(path.join(this.outDir, 'json-fields.ts'), undefined, {
496+
const jsonFieldsFile = project.createSourceFile(path.join(this.outDir, 'json-types.ts'), undefined, {
497497
overwrite: true,
498498
});
499499

500-
await this.generateExtraTypes(jsonFieldsFile);
501-
await jsonFieldsFile.save();
500+
this.generateExtraTypes(jsonFieldsFile);
501+
await saveSourceFile(jsonFieldsFile);
502502

503503
for (const d of this.model.declarations.filter(isDataModel)) {
504504
const fileName = `${prismaClientDir}/models/${d.name}.ts`;
@@ -514,7 +514,7 @@ export type Enhanced<Client> =
514514

515515
sfNew.addStatements('import $Types = runtime.Types;');
516516

517-
// Add import for json-fields if this model has JSON type fields
517+
// Add import for json-types if this model has JSON type fields
518518
const modelWithJsonFields = this.modelsWithJsonTypeFields.find((m) => m.name === d.name);
519519
if (modelWithJsonFields) {
520520
// Get the specific types that are used in this model
@@ -525,7 +525,7 @@ export type Enhanced<Client> =
525525
const typeNames = [...new Set(jsonFieldTypes.map((field) => field.type.reference!.$refText))];
526526

527527
if (typeNames.length > 0) {
528-
sfNew.addStatements(`import type { ${typeNames.join(', ')} } from "../../json-fields";`);
528+
sfNew.addStatements(`import type { ${typeNames.join(', ')} } from "../../json-types";`);
529529
}
530530
}
531531

@@ -940,7 +940,7 @@ export type Enhanced<Client> =
940940
return source;
941941
}
942942

943-
private async generateExtraTypes(sf: SourceFile) {
943+
private generateExtraTypes(sf: SourceFile) {
944944
for (const decl of this.model.declarations) {
945945
if (isTypeDef(decl)) {
946946
generateTypeDefType(sf, decl);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue 2168', () => {
4+
it('regression', async () => {
5+
await loadSchema(
6+
`
7+
datasource db {
8+
provider = "sqlite"
9+
url = "file:./test.db"
10+
11+
}
12+
13+
generator client {
14+
provider = "prisma-client"
15+
output = "../generated/prisma"
16+
moduleFormat = "cjs"
17+
}
18+
19+
model User {
20+
id Int @id
21+
profile Profile @json
22+
}
23+
24+
type Profile {
25+
age Int
26+
}
27+
`,
28+
{
29+
compile: true,
30+
addPrelude: false,
31+
output: './generated/zenstack',
32+
prismaLoadPath: './generated/prisma/client',
33+
extraSourceFiles: [
34+
{
35+
name: 'main.ts',
36+
content: `
37+
import type { Profile } from './generated/zenstack/models';
38+
const profile: Profile = { age: 18 };
39+
console.log(profile);
40+
`,
41+
},
42+
],
43+
}
44+
);
45+
});
46+
});

0 commit comments

Comments
 (0)