Skip to content

Commit 9ed6a92

Browse files
authored
Fix(JSON): Allow JSON types to be skipped through Prisma.skip (#2064)
1 parent 700553f commit 9ed6a92

File tree

2 files changed

+77
-6
lines changed
  • packages/schema/src/plugins/enhancer/enhance
  • tests/integration/tests/enhancements/json

2 files changed

+77
-6
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,21 @@ export type Enhanced<Client> =
827827
};
828828

829829
const replacePrismaJson = (source: string, field: DataModelField) => {
830-
return source.replace(
831-
new RegExp(`(${field.name}\\??\\s*):[^\\n]+`),
832-
`$1: ${field.type.reference!.$refText}${field.type.array ? '[]' : ''}${
833-
field.type.optional ? ' | null' : ''
834-
}`
835-
);
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);
836845
};
837846

838847
// fix "$[Model]Payload" type

tests/integration/tests/enhancements/json/crud.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,66 @@ describe('Json field CRUD', () => {
429429

430430
await expect(post.content.content[0].content[0].text).toBe('hello');
431431
});
432+
433+
it('works with Prisma.skip', async () => {
434+
const params = await loadSchema(
435+
`
436+
type Profile {
437+
foo Int
438+
bar String
439+
}
440+
441+
model User {
442+
id Int @id @default(autoincrement())
443+
name String
444+
profile Profile @json
445+
@@allow('all', true)
446+
}
447+
`,
448+
{
449+
provider: 'postgresql',
450+
dbUrl,
451+
compile: true,
452+
extraSourceFiles: [
453+
{
454+
name: 'main.ts',
455+
content: `
456+
import { enhance } from '.zenstack/enhance';
457+
import { Prisma, PrismaClient } from '@prisma/client';
458+
const prisma = new PrismaClient();
459+
const db = enhance(prisma);
460+
461+
async function main() {
462+
// @ts-expect-error Non optional JSON fields should not be skippable in the create call
463+
db.user.create({ data: { name: 'test', profile: Prisma.skip } });
464+
465+
const u = await db.user.create({ data: { name: 'test', profile: { foo: 18, bar: 'test' } } });
466+
await db.user.update({ where: { id: u.id }, data: { profile: Prisma.skip } });
467+
}
468+
`,
469+
},
470+
],
471+
}
472+
);
473+
474+
prisma = params.prisma;
475+
const skip = params.prismaModule.Prisma.skip;
476+
const db = params.enhance();
477+
478+
const user = await db.user.create({ data: { name: 'test', profile: { foo: 18, bar: 'test' } } });
479+
480+
await expect(
481+
db.user.update({
482+
where: { id: user.id },
483+
data: { profile: skip },
484+
})
485+
).resolves.toMatchObject({
486+
id: user.id,
487+
name: 'test',
488+
profile: {
489+
foo: 18,
490+
bar: 'test',
491+
},
492+
});
493+
});
432494
});

0 commit comments

Comments
 (0)