|
151 | 151 |
|
152 | 152 | ${
|
153 | 153 | prismaTypesFixed
|
154 |
| - ? this.createLogicalPrismaImports(prismaImport, resultPrismaImport) |
155 |
| - : this.createSimplePrismaImports(prismaImport) |
| 154 | + ? this.createLogicalPrismaImports(prismaImport, resultPrismaImport, target) |
| 155 | + : this.createSimplePrismaImports(prismaImport, target) |
156 | 156 | }
|
157 | 157 |
|
158 | 158 | ${authTypes}
|
|
206 | 206 | return normalizedRelative(this.outDir, zodAbsPath);
|
207 | 207 | }
|
208 | 208 |
|
209 |
| - private createSimplePrismaImports(prismaImport: string) { |
210 |
| - return `import { Prisma, type PrismaClient } from '${prismaImport}'; |
| 209 | + private createSimplePrismaImports(prismaImport: string, target: string) { |
| 210 | + const prismaTargetImport = target === 'edge' ? `${prismaImport}/edge` : prismaImport; |
| 211 | + |
| 212 | + return `import { Prisma, type PrismaClient } from '${prismaTargetImport}'; |
211 | 213 | import type * as _P from '${prismaImport}';
|
212 | 214 | export type { PrismaClient };
|
213 | 215 |
|
@@ -235,8 +237,9 @@ export function enhance<DbClient extends object>(prisma: DbClient, context?: Enh
|
235 | 237 | `;
|
236 | 238 | }
|
237 | 239 |
|
238 |
| - private createLogicalPrismaImports(prismaImport: string, prismaClientImport: string) { |
239 |
| - return `import { Prisma as _Prisma, PrismaClient as _PrismaClient } from '${prismaImport}'; |
| 240 | + private createLogicalPrismaImports(prismaImport: string, prismaClientImport: string, target: string) { |
| 241 | + const prismaTargetImport = target === 'edge' ? `${prismaImport}/edge` : prismaImport; |
| 242 | + return `import { Prisma as _Prisma, PrismaClient as _PrismaClient } from '${prismaTargetImport}'; |
240 | 243 | import type { InternalArgs, DynamicClientExtensionThis } from '${prismaImport}/runtime/library';
|
241 | 244 | import type * as _P from '${prismaClientImport}';
|
242 | 245 | import type { Prisma, PrismaClient } from '${prismaClientImport}';
|
@@ -824,12 +827,21 @@ export type Enhanced<Client> =
|
824 | 827 | };
|
825 | 828 |
|
826 | 829 | const replacePrismaJson = (source: string, field: DataModelField) => {
|
827 |
| - return source.replace( |
828 |
| - new RegExp(`(${field.name}\\??\\s*):[^\\n]+`), |
829 |
| - `$1: ${field.type.reference!.$refText}${field.type.array ? '[]' : ''}${ |
830 |
| - field.type.optional ? ' | null' : '' |
831 |
| - }` |
832 |
| - ); |
| 830 | + let replaceValue = `$1: ${field.type.reference!.$refText}`; |
| 831 | + if (field.type.array) { |
| 832 | + replaceValue += '[]'; |
| 833 | + } |
| 834 | + if (field.type.optional) { |
| 835 | + replaceValue += ' | null'; |
| 836 | + } |
| 837 | + |
| 838 | + // Check if the field in the source is optional (has a `?`) |
| 839 | + const isOptionalInSource = new RegExp(`(${field.name}\\?\\s*):`).test(source); |
| 840 | + if (isOptionalInSource) { |
| 841 | + replaceValue += ' | $Types.Skip'; |
| 842 | + } |
| 843 | + |
| 844 | + return source.replace(new RegExp(`(${field.name}\\??\\s*):[^\\n]+`), replaceValue); |
833 | 845 | };
|
834 | 846 |
|
835 | 847 | // fix "$[Model]Payload" type
|
|
0 commit comments