Skip to content

Fix(JSON): Allow JSON types to be skipped through Prisma.skip #2064

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

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,21 @@ export type Enhanced<Client> =
};

const replacePrismaJson = (source: string, field: DataModelField) => {
return source.replace(
new RegExp(`(${field.name}\\??\\s*):[^\\n]+`),
`$1: ${field.type.reference!.$refText}${field.type.array ? '[]' : ''}${
field.type.optional ? ' | null' : ''
}`
);
let replaceValue = `$1: ${field.type.reference!.$refText}`;
if (field.type.array) {
replaceValue += '[]';
}
if (field.type.optional) {
replaceValue += ' | null';
}

// Check if the field in the source is optional (has a `?`)
const isOptionalInSource = new RegExp(`(${field.name}\\?\\s*):`).test(source);
if (isOptionalInSource) {
replaceValue += ' | $Types.Skip';
}

return source.replace(new RegExp(`(${field.name}\\??\\s*):[^\\n]+`), replaceValue);
};

// fix "$[Model]Payload" type
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/tests/enhancements/json/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,66 @@ describe('Json field CRUD', () => {

await expect(post.content.content[0].content[0].text).toBe('hello');
});

it('works with Prisma.skip', async () => {
const params = await loadSchema(
`
type Profile {
foo Int
bar String
}

model User {
id Int @id @default(autoincrement())
name String
profile Profile @json
@@allow('all', true)
}
`,
{
provider: 'postgresql',
dbUrl,
compile: true,
extraSourceFiles: [
{
name: 'main.ts',
content: `
import { enhance } from '.zenstack/enhance';
import { Prisma, PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const db = enhance(prisma);

async function main() {
// @ts-expect-error Non optional JSON fields should not be skippable in the create call
db.user.create({ data: { name: 'test', profile: Prisma.skip } });

const u = await db.user.create({ data: { name: 'test', profile: { foo: 18, bar: 'test' } } });
await db.user.update({ where: { id: u.id }, data: { profile: Prisma.skip } });
}
`,
},
],
}
);

prisma = params.prisma;
const skip = params.prismaModule.Prisma.skip;
const db = params.enhance();

const user = await db.user.create({ data: { name: 'test', profile: { foo: 18, bar: 'test' } } });

await expect(
db.user.update({
where: { id: user.id },
data: { profile: skip },
})
).resolves.toMatchObject({
id: user.id,
name: 'test',
profile: {
foo: 18,
bar: 'test',
},
});
});
});
Loading